Microsoft.OpenApi.Models.OpenApiDocument.SerializeAsV2 Invalid URI: The format of the URI could not be determined

一个人想着一个人 提交于 2020-03-25 18:23:11

问题


I am upgrading my api from .netcore2.1 to 3.1

The api uses SwagerUI and Autorest to generate the client.

From this question about Autorest I found out that I need to use

       app.UseSwagger(c =>
        {
            c.SerializeAsV2 = true;
        });

However the UI gives

Fetch error
Internal Server Error v1/swagger.json

The json is

{
"StatusCode": 500,
"ErrorMessage": "Internal server error: Invalid URI: The format of the URI could not be determined. nStack:
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at Microsoft.OpenApi.Models.OpenApiDocument.WriteHostInfoV2(IOpenApiWriter writer, IList`1 servers)
at Microsoft.OpenApi.Models.OpenApiDocument.SerializeAsV2(IOpenApiWriter writer)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson(HttpResponse response, OpenApiDocument swagger)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)",
"SystemMessage": "Invalid URI: The format of the URI could not be determined."
}

[Update]

I found mention of it here

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

Now the json shows

{
"StatusCode": 500,
"ErrorMessage": "Internal server error: Invalid URI: Invalid port specified.
Stack:   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at Microsoft.OpenApi.Models.OpenApiDocument.WriteHostInfoV2(IOpenApiWriter writer, IList`1 servers)
at Microsoft.OpenApi.Models.OpenApiDocument.SerializeAsV2(IOpenApiWriter writer)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson(HttpResponse response, OpenApiDocument swagger)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)",
"SystemMessage": "Invalid URI: Invalid port specified."
}

回答1:


The following worked

        var basePath = "/v1";
        app.UseSwagger(c =>
        {
            c.RouteTemplate = "swagger/{documentName}/swagger.json";
            c.SerializeAsV2 = true;
            c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
            {
                swaggerDoc.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}{basePath}" } };
            });

        });


来源:https://stackoverflow.com/questions/60813853/microsoft-openapi-models-openapidocument-serializeasv2-invalid-uri-the-format-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!