wI2L / fizz

:lemon: Gin wrapper with OpenAPI 3 spec generation
https://pkg.go.dev/github.com/wI2L/fizz
MIT License
214 stars 52 forks source link

Example tag #32

Closed ghost closed 3 years ago

ghost commented 5 years ago

How define example values for parameters?

wI2L commented 5 years ago

It isn't possible at the moment.

The Parameter is missing the example and examples fields required type. https://github.com/wI2L/fizz/blob/bbaa01a9f70702e7c16baa507eefe416faaf67cb/openapi/spec.go#L94

The Schema type is also missing the example field. https://github.com/wI2L/fizz/blob/bbaa01a9f70702e7c16baa507eefe416faaf67cb/openapi/spec.go#L145

Once those fields are added, we could add a new OperationOption to add an example for a parameter, like fizz.Example(value interface{}, summary string). Would it work for you ?

Note that the OpenAPI 3 spec defines the examples and example fields. The former let you defines an example mapped to a specific media type, while the latter doesn't. It is also possible to add an example within a Schema, but schemas are generated based on the Go types the API is using, and I can't think of a simple way to allow a user to define it without using a global function to register an example per type.

ghost commented 5 years ago

That is true, but we can generate example by set tag example on struct fields instead add fizz.Example(value interface{}, summary string). It's would be more simple, IMO.

wI2L commented 5 years ago

Yes, you are right. But this would be scoped to the example field only, which I guess is good enough. There's always the possibility to edit the generated spec for advanced use cases. Feel free to send a PR, otherwise I'll look at it when I have some time.