Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for "title" property #1762

Merged
merged 1 commit into from Feb 20, 2024

Conversation

matteobassan
Copy link
Contributor

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

feat(field_parser.go): add support for "title" tag in structField struct to allow specifying a custom field title
  chore(operation.go): add "titleTag" constant to define the tag name for the "title" tag used in field_parser.go to avoid hardcoding it multiple times
  feat(field_parser_test.go): test case for "title" tag support in structField struct and schema customization based on the tag value
Copy link

codecov bot commented Feb 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sdghchj sdghchj merged commit 91624ad into swaggo:master Feb 20, 2024
12 checks passed
@matteobassan matteobassan deleted the support-title-tag branch February 20, 2024 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants