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

5.0.0 - SerializeAsV2 - Invalid URI: The format of the URI could not be determined. #1509

Closed
danburgener opened this issue Jan 29, 2020 · 2 comments

Comments

@danburgener
Copy link

danburgener commented Jan 29, 2020

When I upgraded to use version 5.0.0 and use SerializeAsV2, I get an error saying "Invalid URI: The format of the URI could not be determined." If I don't use SerializeAsV2, I don't get the error. Unfortunately, I need to use SerializeAsV2. How can I fix this? Also, this only occurs when using IIS, not IISExpress.

  1. Include package:
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />

  2. Set swagger to use v2

applicationBuilder.UseSwagger(c => c.SerializeAsV2 = true)
                    .UseSwaggerUI(c =>
                    {
                        c.SwaggerEndpoint($"{pathPrefix}/swagger/{applicationVersion}/swagger.json", $"{applicationName} {applicationVersion}");
                    });
  1. Try to get json from url, will result in the following error
{
"Message": "Internal Server Error.",
"StackTrace": "   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)\r\n   at System.Uri..ctor(String uriString)\r\n   at Microsoft.OpenApi.Models.OpenApiDocument.WriteHostInfoV2(IOpenApiWriter writer, IList`1 servers)\r\n   at Microsoft.OpenApi.Models.OpenApiDocument.SerializeAsV2(IOpenApiWriter writer)\r\n   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson(HttpResponse response, OpenApiDocument swagger)\r\n   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)\r\n   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\r\n   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)",
"ErrorMessage": "Invalid URI: The format of the URI could not be determined.",
}
@danburgener
Copy link
Author

I was able to get it working by adding PreSerializeFilters. Is this the preferred way?

.UseSwagger(c =>
  {
    c.SerializeAsV2 = true;
    c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
    {
      swaggerDoc.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}{pathPrefix}" } };
    });
  })

@domaindrivendev
Copy link
Owner

Dup of #1495

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