fizz icon indicating copy to clipboard operation
fizz copied to clipboard

feat: set a description to the schema request body

Open chuliang opened this issue 3 years ago • 2 comments

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",
	}
}

chuliang avatar Mar 20 '23 13:03 chuliang

@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 ?

wI2L avatar Mar 28 '23 16:03 wI2L

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?

chuliang avatar Apr 13 '23 10:04 chuliang