Skip to content

Commit

Permalink
Merge branch 'v3' into dependabot/go_modules/github.com/gorilla/webso…
Browse files Browse the repository at this point in the history
…cket-1.5.1
  • Loading branch information
raphael authored Nov 9, 2023
2 parents a0be584 + 61eaf24 commit 6b8ef21
Show file tree
Hide file tree
Showing 5 changed files with 155 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
1 change: 1 addition & 0 deletions http/codegen/openapi/v2/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestSections(t *testing.T) {
{"with-spaces", testdata.WithSpacesDSL},
{"with-map", testdata.WithMapDSL},
{"path-with-wildcards", testdata.PathWithWildcardDSL},
{"typename", testdata.TypenameDSL},
{"not-generate-server", testdata.NotGenerateServerDSL},
{"not-generate-host", testdata.NotGenerateHostDSL},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"swagger":"2.0","info":{"title":"","version":""},"host":"goa.design","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/bar":{"post":{"tags":["testService"],"summary":"bar testService","operationId":"testService#bar","parameters":[{"name":"BarRequestBody","in":"body","required":true,"schema":{"$ref":"#/definitions/BarPayload"}}],"responses":{"200":{"description":"OK response.","schema":{"$ref":"#/definitions/BarResult"}}},"schemes":["https"]}},"/baz":{"post":{"tags":["testService"],"summary":"baz testService","operationId":"testService#baz","parameters":[{"name":"BazRequestBody","in":"body","required":true,"schema":{"$ref":"#/definitions/BazPayload"}}],"responses":{"200":{"description":"OK response.","schema":{"$ref":"#/definitions/BazResult"}}},"schemes":["https"]}},"/foo":{"post":{"tags":["testService"],"summary":"foo testService","operationId":"testService#foo","parameters":[{"name":"FooRequestBody","in":"body","required":true,"schema":{"$ref":"#/definitions/FooPayload"}}],"responses":{"200":{"description":"OK response.","schema":{"$ref":"#/definitions/FooResult"}}},"schemes":["https"]}}},"definitions":{"BarPayload":{"title":"BarPayload","type":"object","properties":{"value":{"type":"string","example":""}},"example":{"value":""}},"BarResult":{"title":"Mediatype identifier: application/vnd.goa.example.bar; view=default","type":"object","properties":{"value":{"type":"string","example":""}},"description":"BarResponseBody result type (default view)","example":{"value":""}},"BazPayload":{"title":"BazPayload","type":"object","properties":{"value":{"type":"string","example":""}},"example":{"value":""}},"BazResult":{"title":"BazResult","type":"object","properties":{"value":{"type":"string","example":""}},"example":{"value":""}},"FooPayload":{"title":"FooPayload","type":"object","properties":{"value":{"type":"string","example":""}},"example":{"value":""}},"FooResult":{"title":"Mediatype identifier: application/vnd.goa.example.bar; view=default","type":"object","properties":{"value":{"type":"string","example":""}},"description":"FooResponseBody result type (default view)","example":{"value":""}}}}
128 changes: 128 additions & 0 deletions http/codegen/openapi/v2/testdata/TestSections/typename_file1.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
swagger: "2.0"
info:
title: ""
version: ""
host: goa.design
consumes:
- application/json
- application/xml
- application/gob
produces:
- application/json
- application/xml
- application/gob
paths:
/bar:
post:
tags:
- testService
summary: bar testService
operationId: testService#bar
parameters:
- name: BarRequestBody
in: body
required: true
schema:
$ref: '#/definitions/BarPayload'
responses:
"200":
description: OK response.
schema:
$ref: '#/definitions/BarResult'
schemes:
- https
/baz:
post:
tags:
- testService
summary: baz testService
operationId: testService#baz
parameters:
- name: BazRequestBody
in: body
required: true
schema:
$ref: '#/definitions/BazPayload'
responses:
"200":
description: OK response.
schema:
$ref: '#/definitions/BazResult'
schemes:
- https
/foo:
post:
tags:
- testService
summary: foo testService
operationId: testService#foo
parameters:
- name: FooRequestBody
in: body
required: true
schema:
$ref: '#/definitions/FooPayload'
responses:
"200":
description: OK response.
schema:
$ref: '#/definitions/FooResult'
schemes:
- https
definitions:
BarPayload:
title: BarPayload
type: object
properties:
value:
type: string
example: ""
example:
value: ""
BarResult:
title: 'Mediatype identifier: application/vnd.goa.example.bar; view=default'
type: object
properties:
value:
type: string
example: ""
description: BarResponseBody result type (default view)
example:
value: ""
BazPayload:
title: BazPayload
type: object
properties:
value:
type: string
example: ""
example:
value: ""
BazResult:
title: BazResult
type: object
properties:
value:
type: string
example: ""
example:
value: ""
FooPayload:
title: FooPayload
type: object
properties:
value:
type: string
example: ""
example:
value: ""
FooResult:
title: 'Mediatype identifier: application/vnd.goa.example.bar; view=default'
type: object
properties:
value:
type: string
example: ""
description: FooResponseBody result type (default view)
example:
value: ""

0 comments on commit 6b8ef21

Please sign in to comment.