Swashbuckle.AspNetCore
Swashbuckle.AspNetCore copied to clipboard
How to put extension at the top of the document: Is there a way to change the order in which components are serialized?
For a API store, we need to have specific extension information in our (yaml) document. I have added this information via a documents filter, however, because it also needs anchors and make use of aliases, the main extension needs to be at the top, otherwise the aliases will not be recognized:
Is there a way that I can get my extension component to be rendered at the top of the document (like right after the info component)?
Remco
To answer my own question, it looks like the serialization is in the microsoft OpenApiDocument, which is also not containing virtual methods for the serialization, meaning they is not a good way to do this....
Although, I can think of a way, super hacky....
Use reflection and unsafe code to replace the SerializeAsV3 with my own method, which does the same thing but in a different order. (Pretty evil)
Looking at the OpenApiDocument code and the swagger code, it doesn't seem there is a proper way to do this. And neither does it seem to be a swashbuckle responsibility
@domaindrivendev I am looking into the source code, I suspect I could modify Swashbuckle.AspNetCore.Cli.Main, Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson and Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerYaml to look for a custom IDocumentSerializer implementation. (Or add it to the SwaggerOptions, but then CLI will need to retrieve that also from the ServiceProvider).
It should probably have a SerializeAsV3() and a SerializeAsV2() (And possible a CanSerializeAsV2/V3) and use that if it is available.
Do you feel that is something that would bring value to swashbuckle?
I created a pull request, which adds support for injecting a custom document serializer.
Sorry, I should have made the PR from a feature branch, I closed it and created a new one
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/2677#issuecomment-2053573458