Burgyn / MMLib.SwaggerForOcelot

This repo contains swagger extension for ocelot.
MIT License
354 stars 95 forks source link

NullReferenceException when "servers" field in specification is present but does not contain any elements. #267

Closed Rasmus715 closed 1 year ago

Rasmus715 commented 1 year ago

I faced this issue when integrating NestJS autogenerated specification into the Gateway running your library. When accessing that documentation, the "servers" array is present but it does not contain any elements. This causes the application to throw a NullReferenceException while parsing it.

That issue can be suppressed by switching "TransformByOcelotConfig" flag to "true" in the Ocelot configuration file. But then it's impossible to use downstream mapping, which is crucial in my case.

Attaching a screenshot of a problem and creating a PR with the solution below.

image image

Call stack trace from browser network page:

System.NullReferenceException: Object reference not set to an instance of an object. at MMLib.SwaggerForOcelot.Transformation.SwaggerJsonTransformer.TransformOpenApi(JObject openApi, IEnumerable1 routes, String serverOverride, SwaggerEndPointOptions endPointOptions) in C:\REDACTED\ABC\MMLib.SwaggerForOcelot\Transformation\SwaggerJsonTransformer.cs:line 132 at MMLib.SwaggerForOcelot.Transformation.SwaggerJsonTransformer.TransformSwaggerOrOpenApi(String swaggerJson, IEnumerable`1 routes, String serverOverride, SwaggerEndPointOptions endPointOptions) in C:\REDACTED\ABC\MMLib.SwaggerForOcelot\Transformation\SwaggerJsonTransformer.cs:line 66 at MMLib.SwaggerForOcelot.Transformation.SwaggerJsonTransformer.Transform(String swaggerJson, IEnumerable1 routes, String serverOverride, SwaggerEndPointOptions endPointOptions) in C:\REDACTED\ABC\MMLib.SwaggerForOcelot\Transformation\SwaggerJsonTransformer.cs:line 39 at MMLib.SwaggerForOcelot.Middleware.SwaggerForOcelotMiddleware.Invoke(HttpContext context, ISwaggerEndPointProvider swaggerEndPointRepository, IDownstreamSwaggerDocsRepository downstreamSwaggerDocs) in C:\REDACTED\ABC\MMLib.SwaggerForOcelot\Middleware\SwaggerForOcelotMiddleware.cs:line 97 at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.InvokeCore(HttpContext context, PathString matchedPath, PathString remainingPath) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

update-docs[bot] commented 1 year ago

Thanks for opening this pull request! If you have implemented new functions, write about them in the readme file.

Burgyn commented 1 year ago

Thanks a lot. Your changes will be released soon v6.3.1

Rasmus715 commented 1 year ago

Please be aware that this solution could not be the optimal one because I haven't tested it in different scenarios.

Burgyn commented 1 year ago

There's a pretty big set of tests there, it would reveal if there's a problem. If you feel like it you can add a test for this case of yours.