Giter VIP home page Giter VIP logo

Comments (9)

deepmap-marcinr avatar deepmap-marcinr commented on May 14, 2024

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.

utx0 avatar utx0 commented on May 14, 2024

ok thanks heaps!

from oapi-codegen.

deepmap-marcinr avatar deepmap-marcinr commented on May 14, 2024

from oapi-codegen.

utx0 avatar utx0 commented on May 14, 2024

Yeah like whats sort of special cases you speak of? Maybe I can help...

from oapi-codegen.

deepmap-marcinr avatar deepmap-marcinr commented on May 14, 2024

from oapi-codegen.

utx0 avatar utx0 commented on May 14, 2024

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.

deepmap-marcinr avatar deepmap-marcinr commented on May 14, 2024

from oapi-codegen.

utx0 avatar utx0 commented on May 14, 2024

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.

StevenACoffman avatar StevenACoffman commented on May 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.