Swashbuckle.AspNetCore icon indicating copy to clipboard operation
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?

Open remcolam opened this issue 1 year ago • 5 comments

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:

image

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

remcolam avatar Jun 15 '23 12:06 remcolam

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

remcolam avatar Jun 17 '23 08:06 remcolam

@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?

remcolam avatar Jun 17 '23 09:06 remcolam

I created a pull request, which adds support for injecting a custom document serializer.

remcolam avatar Jun 29 '23 05:06 remcolam

Sorry, I should have made the PR from a feature branch, I closed it and created a new one

remcolam avatar Jun 29 '23 10:06 remcolam

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/2677#issuecomment-2053573458

martincostello avatar Apr 14 '24 09:04 martincostello