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

feat: set a description to the schema request body #100

Open chuliang opened 1 year ago

chuliang commented 1 year ago

Allow to set a description to a schema used as request body

Example: Input struct:

type RouteInput struct {
    _ struct{} `json:"-" description:"Description of the input schema"`
    FieldA string `json:"field_a" description:"field_a description"`
    ...
}

Generated schema:

{
    "RouteInput": {
        "type": "object",
        "properties": {
            "field_a": {
                "type": "string",
                "description": "field_a description"
            },
            ...
        },
        "description": "Description of the input schema",
    }
}
wI2L commented 1 year ago

@chuliang

Hello,

Regarding the feature itself, I'm not against it, but the implementation itself isn't what I would recommend. Instead of having to rely on unexported struct fields, we could instead provide a package function to register a description for each type (in a global map protected with a mutex). This could then in turn be used by the generator to generate the "components/schemas" part of the spec.

See newSchemaFromType.

So, in terms of usage, this would give something like this:

type RouteInput struct {
    FieldA string `json:"field_a" description:"field_a description"`
}

func init() {
  fizz.RegisterSchemaDescription(RouteInput{}, "Description of the input schema")
}

wdyt ?

chuliang commented 1 year ago

Hello @wI2L :) Thanks for your review.

I think fizz.RegisterSchemaDescription(...) remove the benefit of Tonic to register input schema from the route function parameter. I updated my PR with another way to provide a description for the RequestBody schema. Is it better for you?