diff --git a/expr/http_body_types.go b/expr/http_body_types.go index ec37ed7ee8..4651b42cdd 100644 --- a/expr/http_body_types.go +++ b/expr/http_body_types.go @@ -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, @@ -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) diff --git a/http/codegen/openapi/json_schema.go b/http/codegen/openapi/json_schema.go index 112db5ffe8..82d1b3c5ef 100644 --- a/http/codegen/openapi/json_schema.go +++ b/http/codegen/openapi/json_schema.go @@ -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) @@ -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) }