c# - How do you programmatically access the Swagger.json file after it has been generated -
i trying create dynamic rest api uses swagger documentation in .netcore (using swashbuckle.aspnetcore). dynamic in sense there 1 controller 1 possible response begin with, user can add "end points" posting service, later controller can translate new routes respond accordingly
in order this, need able access , change swagger.json file have ui change reflect changes - possible? if how?
note: know can access , see swagger doc navigating /{documentname}/swagger.json not allow me change it
you can extend, filter , customize schema custom filters: https://github.com/domaindrivendev/swashbuckle.aspnetcore#extend-generator-with-operation-schema--document-filters
i did use decorating more header-fields each request (like authorization header). i'm not sure, if it's gonna work whole endpoints. maybe it's worth try.
update (edited)
here sample idocumentfilter adds whole endpoints:
private class documentfilteraddfakes : idocumentfilter { private pathitem fakepathitem(int i) { var x = new pathitem(); x.get = new operation() { tags = new[] { "fake" }, operationid = "fake_get" + i.tostring(), consumes = null, produces = new[] { "application/json", "text/json", "application/xml", "text/xml" }, parameters = new list<iparameter>() { new nonbodyparameter() // can bodyparameter { name = "id", @in = "path", required = true, type = "integer", format = "int32", @default = 8 } }, }; x.get.responses = new dictionary<string, response>(); x.get.responses.add("200", new response() { description = "ok", schema = new schema() { type = "string" } }); return x; } public void apply(swaggerdocument swaggerdoc, documentfiltercontext context) { (int = 0; < 10; i++) swaggerdoc.paths.add("/fake/" + + "/{id}", fakepathitem(i)); } }
Comments
Post a Comment