Skip to content

Commit afac8ae

Browse files
authored
Add proper Godoc comment for properties marked as deprecated (#976)
* Add proper Godoc comment for properties marked as deprecated Also allow to specify a reason for deprecation using an extension: x-deprecated-reason * Add proper Godoc comment for properties marked as deprecated (fix description)
1 parent 22d1cf4 commit afac8ae

File tree

5 files changed

+232
-28
lines changed

5 files changed

+232
-28
lines changed

internal/test/schemas/schemas.gen.go

Lines changed: 159 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/schemas/schemas.yaml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ paths:
121121
application/json:
122122
schema:
123123
$ref: "#/components/schemas/EnumInObjInArray"
124+
/issues/975:
125+
get:
126+
operationId: Issue975
127+
description: |
128+
Deprecated fields should get a proper comment
129+
responses:
130+
200:
131+
description: A struct with deprecated fields with varying level of documentation
132+
content:
133+
application/json:
134+
schema:
135+
$ref: "#/components/schemas/DeprecatedProperty"
136+
124137
components:
125138
schemas:
126139
GenericObject:
@@ -165,6 +178,32 @@ components:
165178
enum:
166179
- first
167180
- second
181+
DeprecatedProperty:
182+
type: object
183+
required:
184+
- newProp
185+
- oldProp
186+
properties:
187+
newProp:
188+
type: string
189+
description: Use this now!
190+
oldProp1:
191+
type: string
192+
deprecated: true
193+
# description: No description on this one to test generation in that case
194+
oldProp2:
195+
type: string
196+
deprecated: true
197+
description: It used to do this and that
198+
oldProp3:
199+
type: string
200+
deprecated: true
201+
x-deprecated-reason: Use NewProp instead!
202+
oldProp4:
203+
type: string
204+
deprecated: true
205+
x-deprecated-reason: Use NewProp instead!
206+
description: It used to do this and that
168207
parameters:
169208
StringInPath:
170209
name: str
@@ -183,4 +222,3 @@ components:
183222
JWT-format access token.
184223
security:
185224
- access-token: [ ]
186-

pkg/codegen/extension.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ const (
1212
// extGoName is used to override a field name
1313
extGoName = "x-go-name"
1414
// extGoTypeName is used to override a generated typename for something.
15-
extGoTypeName = "x-go-type-name"
16-
extPropGoJsonIgnore = "x-go-json-ignore"
17-
extPropOmitEmpty = "x-omitempty"
18-
extPropExtraTags = "x-oapi-codegen-extra-tags"
19-
extEnumVarNames = "x-enum-varnames"
20-
extEnumNames = "x-enumNames"
15+
extGoTypeName = "x-go-type-name"
16+
extPropGoJsonIgnore = "x-go-json-ignore"
17+
extPropOmitEmpty = "x-omitempty"
18+
extPropExtraTags = "x-oapi-codegen-extra-tags"
19+
extEnumVarNames = "x-enum-varnames"
20+
extEnumNames = "x-enumNames"
21+
extDeprecationReason = "x-deprecated-reason"
2122
)
2223

2324
func extString(extPropValue interface{}) (string, error) {
@@ -82,3 +83,7 @@ func extParseEnumVarNames(extPropValue interface{}) ([]string, error) {
8283
}
8384
return names, nil
8485
}
86+
87+
func extParseDeprecationReason(extPropValue interface{}) (string, error) {
88+
return extString(extPropValue)
89+
}

pkg/codegen/schema.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type Property struct {
8585
WriteOnly bool
8686
NeedsFormTag bool
8787
Extensions map[string]interface{}
88+
Deprecated bool
8889
}
8990

9091
func (p Property) GoFieldName() string {
@@ -388,6 +389,7 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
388389
ReadOnly: p.Value.ReadOnly,
389390
WriteOnly: p.Value.WriteOnly,
390391
Extensions: p.Value.Extensions,
392+
Deprecated: p.Value.Deprecated,
391393
}
392394
outSchema.Properties = append(outSchema.Properties, prop)
393395
}
@@ -622,6 +624,18 @@ func GenFieldsFromProperties(props []Property) []string {
622624
field += fmt.Sprintf("%s\n", StringWithTypeNameToGoComment(p.Description, p.GoFieldName()))
623625
}
624626

627+
if p.Deprecated {
628+
// This comment has to be on its own line for godoc & IDEs to pick up
629+
var deprecationReason string
630+
if _, ok := p.Extensions[extDeprecationReason]; ok {
631+
if extOmitEmpty, err := extParseDeprecationReason(p.Extensions[extDeprecationReason]); err == nil {
632+
deprecationReason = extOmitEmpty
633+
}
634+
}
635+
636+
field += fmt.Sprintf("%s\n", DeprecationComment(deprecationReason))
637+
}
638+
625639
field += fmt.Sprintf(" %s %s", goFieldName, p.GoTypeDef())
626640

627641
// Support x-omitempty

pkg/codegen/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,15 @@ func StringWithTypeNameToGoComment(in, typeName string) string {
694694
return stringToGoCommentWithPrefix(in, typeName)
695695
}
696696

697+
func DeprecationComment(reason string) string {
698+
var content = "Deprecated:" // The colon is required at the end even without reason
699+
if reason != "" {
700+
content += fmt.Sprintf(" %s", reason)
701+
}
702+
703+
return stringToGoCommentWithPrefix(content, "")
704+
}
705+
697706
func stringToGoCommentWithPrefix(in, prefix string) string {
698707
if len(in) == 0 || len(strings.TrimSpace(in)) == 0 { // ignore empty comment
699708
return ""

0 commit comments

Comments
 (0)