Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [aspnetcore] Swagger XML comments causes issues for test project #19592

Closed
5 of 6 tasks
david-marconis opened this issue Sep 16, 2024 · 0 comments
Closed
5 of 6 tasks

Comments

@david-marconis
Copy link
Contributor

david-marconis commented Sep 16, 2024

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The generated code for aspnetcore with swagger generation (Swashbuckle) enabled uses the GetEntryAssembly method to specify the path to the XML comments. This causes issues if you were to test this API from a different project (assembly) as the entry assembly will be the test project and not the main project.

There is an easy workaround to this and that is to use GetExecutingAssembly instead of GetEntryAssebly which is also what Microsoft recommends:

builder.Services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo
    {
    ...
    // using System.Reflection;
    var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
openapi-generator version

7.8.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Minimal API
  version: 1.0.0
paths:
  /example:
    get:
      summary: A minimal GET endpoint
      responses:
        '200':
          description: "Success"
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
Generation Details
openapi-generator-cli generate -i openapi.yaml -g aspnetcore
Steps to reproduce
cat <<EOF > openapi.yaml
openapi: 3.0.0
info:
  title: Minimal API
  version: 1.0.0
paths:
  /example:
    get:
      summary: A minimal GET endpoint
      responses:
        '200':
          description: "Success"
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
EOF
openapi-generator-cli generate -i openapi.yaml -g aspnetcore
dotnet new xunit -n Tests -o src/Org.OpenAPITools.Tests
dotnet add src/Org.OpenAPITools.Tests reference src/Org.OpenAPITools
dotnet add src/Org.OpenAPITools.Tests package Microsoft.AspNetCore.Mvc.Testing
cat <<EOF > src/Org.OpenAPITools.Tests/UnitTest1.cs
using System.Net;
using Microsoft.AspNetCore.Mvc.Testing;
using Org.OpenAPITools.Controllers;

namespace Tests;

public class ApiTest(WebApplicationFactory<DefaultApiController> factory)
    : IClassFixture<WebApplicationFactory<DefaultApiController>>
{
    private readonly HttpClient _httpClient = factory.CreateClient();

    [Fact]
    public async void TestApi()
    {
        var response = await _httpClient.GetAsync("example/");
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
    }
}
EOF
dotnet test src/Org.OpenAPITools.Tests

Observe test failure:

[xUnit.net 00:00:00.27] Tests.ApiTest.TestApi [FAIL]
Failed Tests.ApiTest.TestApi [171 ms]
Error Message:
Assert.Equal() Failure: Values differ
Expected: OK
Actual: InternalServerError

Apply simple fix, using GetExecutingAssembly instead of GetEntryAssembly, and rerun test:

sed 's/GetEntryAssembly/GetExecutingAssembly/' -i src/Org.OpenAPITools/Startup.cs
dotnet test src/Org.OpenAPITools.Tests

Observe test passing:

Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1, Duration: < 1 ms - Tests.dll (net8.0)

Related issues/PRs
Suggest a fix

Use GetExecutingAssembly instead.
Fixed by merging #19593

david-marconis added a commit to david-marconis/openapi-generator that referenced this issue Sep 16, 2024
david-marconis added a commit to david-marconis/openapi-generator that referenced this issue Sep 16, 2024
david-marconis added a commit to david-marconis/openapi-generator that referenced this issue Sep 16, 2024
david-marconis added a commit to david-marconis/openapi-generator that referenced this issue Sep 16, 2024
@wing328 wing328 closed this as completed Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants