Skip to content

Commit

Permalink
Add openapi:typename Meta support to openapi v2
Browse files Browse the repository at this point in the history
  • Loading branch information
tchssk committed Nov 7, 2023
1 parent 45f3bef commit 3b23c66
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
14 changes: 12 additions & 2 deletions expr/http_body_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ func httpRequestBody(a *HTTPEndpointExpr) *AttributeExpr {
}
appendSuffix(ut.Attribute().Type, suffix)

// Remember openapi typename for example to generate friendly OpenAPI specs.
if t, ok := payload.Type.(UserType); ok {
if m, ok := t.Attribute().Meta["openapi:typename"]; ok {
ut.AttributeExpr.AddMeta("openapi:typename", m...)
}
}

return &AttributeExpr{
Type: ut,
Validation: att.Validation,
Expand Down Expand Up @@ -318,10 +325,13 @@ func buildHTTPResponseBody(name string, attr *AttributeExpr, resp *HTTPResponseE
UID: concat(svc.Name(), "#", name),
}

// Remember original type name for example to generate friendly OpenAPI
// specs.
// Remember original type name and openapi typename for example
// to generate friendly OpenAPI specs.
if t, ok := attr.Type.(UserType); ok {
userType.AttributeExpr.AddMeta("name:original", t.Name())
if m, ok := t.Attribute().Meta["openapi:typename"]; ok {
userType.AttributeExpr.AddMeta("openapi:typename", m...)
}
}

appendSuffix(userType.Attribute().Type, suffix)
Expand Down
13 changes: 13 additions & 0 deletions http/codegen/openapi/json_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,18 @@ func ResultTypeRefWithPrefix(api *expr.APIExpr, mt *expr.ResultTypeExpr, view st
if err != nil {
panic(fmt.Sprintf("failed to project media type %#v: %s", mt.Identifier, err)) // bug
}
var metaName string
if n, ok := mt.Meta["openapi:typename"]; ok {
metaName = codegen.Goify(n[0], true)
}
if metaName != "" {
projected.TypeName = metaName
}
if _, ok := Definitions[projected.TypeName]; !ok {
projected.TypeName = codegen.Goify(prefix, true) + codegen.Goify(projected.TypeName, true)
if metaName != "" {
projected.TypeName = metaName
}
GenerateResultTypeDefinition(api, projected, "default")
}
return fmt.Sprintf("#/definitions/%s", projected.TypeName)
Expand All @@ -254,6 +264,9 @@ func TypeRefWithPrefix(api *expr.APIExpr, ut *expr.UserTypeExpr, prefix string)
if prefix != "" {
typeName = codegen.Goify(prefix, true) + codegen.Goify(ut.TypeName, true)
}
if n, ok := ut.Meta["openapi:typename"]; ok {
typeName = codegen.Goify(n[0], true)
}
if _, ok := Definitions[typeName]; !ok {
GenerateTypeDefinitionWithName(api, ut, typeName)
}
Expand Down

0 comments on commit 3b23c66

Please sign in to comment.