Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 159 additions & 21 deletions internal/test/schemas/schemas.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 39 additions & 1 deletion internal/test/schemas/schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/EnumInObjInArray"
/issues/975:
get:
operationId: Issue975
description: |
Deprecated fields should get a proper comment
responses:
200:
description: A struct with deprecated fields with varying level of documentation
content:
application/json:
schema:
$ref: "#/components/schemas/DeprecatedProperty"

components:
schemas:
GenericObject:
Expand Down Expand Up @@ -165,6 +178,32 @@ components:
enum:
- first
- second
DeprecatedProperty:
type: object
required:
- newProp
- oldProp
properties:
newProp:
type: string
description: Use this now!
oldProp1:
type: string
deprecated: true
# description: No description on this one to test generation in that case
oldProp2:
type: string
deprecated: true
description: It used to do this and that
oldProp3:
type: string
deprecated: true
x-deprecated-reason: Use NewProp instead!
oldProp4:
type: string
deprecated: true
x-deprecated-reason: Use NewProp instead!
description: It used to do this and that
parameters:
StringInPath:
name: str
Expand All @@ -183,4 +222,3 @@ components:
JWT-format access token.
security:
- access-token: [ ]

17 changes: 11 additions & 6 deletions pkg/codegen/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ const (
// extGoName is used to override a field name
extGoName = "x-go-name"
// extGoTypeName is used to override a generated typename for something.
extGoTypeName = "x-go-type-name"
extPropGoJsonIgnore = "x-go-json-ignore"
extPropOmitEmpty = "x-omitempty"
extPropExtraTags = "x-oapi-codegen-extra-tags"
extEnumVarNames = "x-enum-varnames"
extEnumNames = "x-enumNames"
extGoTypeName = "x-go-type-name"
extPropGoJsonIgnore = "x-go-json-ignore"
extPropOmitEmpty = "x-omitempty"
extPropExtraTags = "x-oapi-codegen-extra-tags"
extEnumVarNames = "x-enum-varnames"
extEnumNames = "x-enumNames"
extDeprecationReason = "x-deprecated-reason"
)

func extString(extPropValue interface{}) (string, error) {
Expand Down Expand Up @@ -82,3 +83,7 @@ func extParseEnumVarNames(extPropValue interface{}) ([]string, error) {
}
return names, nil
}

func extParseDeprecationReason(extPropValue interface{}) (string, error) {
return extString(extPropValue)
}
14 changes: 14 additions & 0 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type Property struct {
WriteOnly bool
NeedsFormTag bool
Extensions map[string]interface{}
Deprecated bool
}

func (p Property) GoFieldName() string {
Expand Down Expand Up @@ -387,6 +388,7 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
ReadOnly: p.Value.ReadOnly,
WriteOnly: p.Value.WriteOnly,
Extensions: p.Value.Extensions,
Deprecated: p.Value.Deprecated,
}
outSchema.Properties = append(outSchema.Properties, prop)
}
Expand Down Expand Up @@ -621,6 +623,18 @@ func GenFieldsFromProperties(props []Property) []string {
field += fmt.Sprintf("%s\n", StringWithTypeNameToGoComment(p.Description, p.GoFieldName()))
}

if p.Deprecated {
// This comment has to be on its own line for godoc & IDEs to pick up
var deprecationReason string
if _, ok := p.Extensions[extDeprecationReason]; ok {
if extOmitEmpty, err := extParseDeprecationReason(p.Extensions[extDeprecationReason]); err == nil {
deprecationReason = extOmitEmpty
}
}

field += fmt.Sprintf("%s\n", DeprecationComment(deprecationReason))
}

field += fmt.Sprintf(" %s %s", goFieldName, p.GoTypeDef())

// Support x-omitempty
Expand Down
9 changes: 9 additions & 0 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,15 @@ func StringWithTypeNameToGoComment(in, typeName string) string {
return stringToGoCommentWithPrefix(in, typeName)
}

func DeprecationComment(reason string) string {
var content = "Deprecated:" // The colon is required at the end even without reason
if reason != "" {
content += fmt.Sprintf(" %s", reason)
}

return stringToGoCommentWithPrefix(content, "")
}

func stringToGoCommentWithPrefix(in, prefix string) string {
if len(in) == 0 || len(strings.TrimSpace(in)) == 0 { // ignore empty comment
return ""
Expand Down