swaggest / openapi-go

OpenAPI structures for Go
https://pkg.go.dev/github.com/swaggest/openapi-go/openapi3
MIT License
248 stars 23 forks source link

Doc page gets 404 when getting swagger-ui assets #81

Closed paul-england closed 1 year ago

paul-england commented 1 year ago

In the console of the docs page, it attempts to GET a few js/css files (from Cloudflare) and fails (404). The result is the page renders white. Is there a way to to use an alternative?

Here is the output from my console.

GET https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.19.1/swagger-ui.css net::ERR_ABORTED 404
docs:34     GET https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.19.1/swagger-ui-standalone-preset.js net::ERR_ABORTED 404
docs:33     GET https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.19.1/swagger-ui-bundle.js net::ERR_ABORTED 404
docs:63 Uncaught ReferenceError: SwaggerUIBundle is not defined
    at window.onload (docs:63:5)
paul-england commented 1 year ago

Worth noting, the above is for an in-progress project that has some features I'll obviously need to look at. Copy / pasting the example from README.md, I get the following in the console, but the page renders, so obviously better.

DevTools failed to load source map: Could not load content for http://localhost:8011/docs/swagger-ui-bundle.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
DevTools failed to load source map: Could not load content for http://localhost:8011/docs/swagger-ui-standalone-preset.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
DevTools failed to load source map: Could not load content for http://localhost:8011/docs/swagger-ui.css.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
paul-england commented 1 year ago

Using v3cdn fixes this issue. Sorry to spam the board.

vearutop commented 1 year ago

Missing source maps are expected, as they are optional for the web app to function and only needed to debug frontend code.

However, both cdn and non-cdn packages should render Swagger UI properly.

paul-england commented 1 year ago

For the record, using v3cdn or v5cdn both render the page just fine. v4cdn, for my project, does not. I'm fine w/ ticking up to v5.

vearutop commented 1 year ago

Could you share some details to reproduce the issue?

paul-england commented 1 year ago

I'm using it w/ *chirouter.Wrapper. Find the code below. Simply swapping out the problematic v4cdn seems to work. And again, the error is that the page renders white (with no real clues in the console).

func NewRouter(apiVersion string, jsonLog bool) *chirouter.Wrapper {

    logger := httplog.NewLogger("ems", httplog.Options{
        JSON: jsonLog,
        Tags: map[string]string{
            "apiVer": apiVersion,
        },
    })

    apiSchema := &openapi.Collector{}
    apiSchema.Reflector().SpecEns().Info.Title = "My Service"
    apiSchema.Reflector().SpecEns().Info.WithDescription("My Description")
    apiSchema.Reflector().SpecEns().Info.Version = apiVersion

    validatorFactory := jsonschema.NewFactory(apiSchema, apiSchema)
    decoderFactory := request.NewDecoderFactory()
    decoderFactory.SetDecoderFunc(rest.ParamInPath, chirouter.PathToURLValues)

    router := chirouter.NewWrapper(chi.NewRouter())

    router.Use(
        middleware.RealIP, 
        httplog.RequestLogger(logger),
        nethttp.OpenAPIMiddleware(apiSchema), 
        request.DecoderMiddleware(decoderFactory),
        request.ValidatorMiddleware(validatorFactory),
        response.EncoderMiddleware,   
        gzip.Middleware, 
    )

    router.Method(http.MethodGet, "/api/v1/docs/openapi", apiSchema)
    router.Mount("/api/v1/docs", v4cdn.NewHandler(apiSchema.Reflector().Spec.Info.Title, "/api/v1/docs/openapi", "/api/v1/docs"))

    return router
}
vearutop commented 1 year ago

Oh, very interesting.

https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.19.1/swagger-ui-bundle.js is indeed 404, apparently it was removed from CDN for some reason. Thank you for the report, I'll downgrade v4cdn to 4.18.3 that still exists.

vearutop commented 1 year ago

Fixed in https://github.com/swaggest/swgui/releases/tag/v1.7.3.

paul-england commented 1 year ago

Excellent! Thanks for the help. Great project, btw.