swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.46k stars 1.19k forks source link

feat: Add support for "title" property #1762

Closed matteobassan closed 6 months ago

matteobassan commented 6 months ago

Describe the PR Added support for "title" property, as defined by Openapi Specification Schema Object. The property is provided via "title" tag on structs.

Relation issue none

Additional context The problem arose from having to generate the API in typescript for the client through openapi-generator. Because some fields (marked with "x-nullable") were considered "Inline Models" and the openapi-generator CLI suggested using the "title" field to give it a unique name, i tried to find a ready-made solution with no luck.

The "schema" struct already contained the "Title" field, but this is never assigned, so I proceeded to complete the assignment of the field through the "title" tag.

Usage:

the following definition:

type CreatePageRequest struct {
    Title       string                `json:"title" validate:"required,max=255"`
    CoverImage  *EmbeddedImageRequest `json:"coverImage,omitempty" extensions:"x-nullable" title:"EmbeddedImageRequest"`
}

will generate this:

...
{
"title": {
                    "type": "string",
                    "maxLength": 255
                },
"coverImage": {
                    "format": "url",
                    "title": "EmbeddedImageRequest",
                    "allOf": [
                        {
                            "$ref": "#/definitions/EmbeddedImageRequestInternal"
                        }
                    ],
                    "x-nullable": true
                },
}
codecov[bot] commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (56fde5c) 83.35% compared to head (a86b4e3) 83.36%. Report is 1 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1762 +/- ## ======================================= Coverage 83.35% 83.36% ======================================= Files 16 16 Lines 3870 3872 +2 ======================================= + Hits 3226 3228 +2 Misses 545 545 Partials 99 99 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.