Comments (9)
The generated code has the swagger spec embedded in it (see the GetSwagger function, see https://github.com/deepmap/oapi-codegen/blob/master/examples/petstore-expanded/api/petstore.gen.go#L375
You could declare a new path for /swagger.json and in the handler, you call GetSwagger and marshal that to JSON, since the openapi3.Swagger struct is designed to be json serialized.
Your handler would be very simple:
ctx.JSON(http.StatusOk, api.GetSwagger())
It's inefficient to do that too often so you might want to cache the json string, but that's the general idea.
from oapi-codegen.
ok thanks heaps!
from oapi-codegen.
from oapi-codegen.
Yeah like whats sort of special cases you speak of? Maybe I can help...
from oapi-codegen.
from oapi-codegen.
ok now that makes sense. Would you guys be open to having this as a feature? Be nice to be able to offer a public /swagger.json endpoint that also isn't minified too?
from oapi-codegen.
from oapi-codegen.
yeah totally get that. I've got it all running now. Just can see myself using this pkg quite a bit. Might have a look at building a middleware where I have space.
from oapi-codegen.
If you already have the gzipped json, and most clients accept gzipped content, it is inefficient to call GetSwagger just to turn it back into JSON and then run it through a middleware to gzip it again. However, the generated embedded spec is currently inaccessible. You would need to add a method to access it, or a handler something like this:
func Handler(si ServerInterface) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
zipped, err := base64.StdEncoding.DecodeString(strings.Join(swaggerSpec, ""))
if err != nil { // ok, this is wrong but you get the idea
return nil, fmt.Errorf("error base64 decoding spec: %s", err)
}
w.Header().Set("Content-Encoding", "gzip")
http.ServeContent(w, req, name, modTime, bytes.NewReader(gzipFile.GzipBytes()))
http.ServeContent(w, r, "foo.txt", time.Now(),
strings.NewReader("I am some content.\n"))
}))
}
from oapi-codegen.
Related Issues (20)
- Unexpected Error Type in Fiber API Custom Error Handler
- x-oapi-codegen-extra-tags HOT 2
- Incorrect array response generation with multiple refs
- Produce a YAML Schema for the configuration file
- docs: add what's missing?
- Generate anonymous objects when `generate: models` HOT 1
- Allow generating multiple `interface`s that build into a single `ServerInterface`
- Allow opt-in to default the `additionalProperties` to true
- Combine multiple errors
- docs(examples): add a Train Travel API example
- Validation of type formats (e.g. email) for Query Parameters
- governance: define the release lifecycle
- Generate code from custom templates HOT 3
- Implement maxProperties and minProperties for additionalProperties HOT 1
- Provide first-class (generated) code to support authentication/authorization
- `nullable` on a `$ref` is overridden by `required` within `type: object` HOT 1
- OpenAPI escaped strings are not escaped in Go
- Use discriminator with several mappings for one schema
- v0.124.0 of kin-openapi introduces compilation errors HOT 1
- Feat: register multiple specs HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oapi-codegen.