yahehe / Nancy.Swagger

Nancy plugin for generated API documentation in Swagger format.
MIT License
133 stars 60 forks source link

Support standalone generation of swagger.json #110

Closed ghost closed 7 years ago

ghost commented 7 years ago

This is not an issue, more of a question of understanding:

From the examples, it seems like the intended use of this library is at runtime of the Nancy application. That is, annotations or metadata modules are used to produce a .../swagger.json path on the web server where the Swagger definitions can be accessed.

Is there any support for generating this file in a standalone project? For my use case, I would very much prefer the "simple" approach of adding a new C# project referencing my Nancy project, analyze its annotations, and write the output to a file (which I can then publish/use/... separately). (I would use this e.g. on CI to generate the swagger.json for manual inspection)

If this is not yet possible, and there is some interest in it, I would be happy to provide a PR.

jnallard commented 7 years ago

Yeah, the project is designed for hosting the swagger.json at runtime.

But if your new project would have access to the Dependency Injection container, you could use that to get the ISwaggerMetadataProvider and just call this function: provider.GetSwaggerJson(null).ToJson(); As the SwaggerModule does: https://github.com/yahehe/Nancy.Swagger/blob/master/src/Nancy.Swagger/Modules/SwaggerModule.cs

Though that null should be the Context. (I believe the Context object is only available during requests, but I'm not sure.) The Context object is not used by the Nancy.Swagger ISwaggerMetadataProvider, but by the Nancy.Swagger.Annotations ISwaggerMetadataProvider. So this method would not work for the Annotations method.

There might be some other solutions, but if you also want to create a PR to make this supported by default, go ahead.

ghost commented 7 years ago

Thanks for the explanation!

I'll get started with implementing a proof of concept, try out the methods you described and come back to you.

yahehe commented 7 years ago

What use case do you envision where you do not want the swagger.json to be dynamically created? If your service also provides the swagger-ui (or you have a separate solution for swagger browsing) you should be able to just check that page at runtime and see if your documentation is what you expect.

yahehe commented 7 years ago

I don't think it's unreasonable to have some sort of functionality that allows for generating json or yaml files though, I know for example AWS API Gateway allows for uploading a swagger file and generating a service skeleton from that

ghost commented 7 years ago

I am planning to version the finished swagger.json correctly and then generate a client library of Typescript definitions for that version. I would then consume these definitions in a client accessing the described API.

By including the additional step of using a swagger.json without needing the actual API around, I can verify the API and client separately by verifying that they both adhere to the Swagger definitions.