Skip to content

Commit

Permalink
fix: merge gnostic responses with the existing instead of overwritting
Browse files Browse the repository at this point in the history
  • Loading branch information
sudorandom committed Dec 6, 2024
1 parent 85a2a3b commit eea3f4a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 5 deletions.
15 changes: 11 additions & 4 deletions internal/converter/gnostic/convertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,16 @@ func toMediaTypes(items *goa3.MediaTypes) *orderedmap.Map[string, *v3.MediaType]
}
content := orderedmap.New[string, *v3.MediaType]()
for _, item := range items.GetAdditionalProperties() {
content.Set(item.Name, &v3.MediaType{
mt := &v3.MediaType{
Schema: toSchemaOrReference(item.Value.Schema),
Example: item.Value.Example.ToRawInfo(),
Examples: toExamples(item.Value.GetExamples()),
Encoding: toEncodings(item.Value.GetEncoding()),
Extensions: toExtensions(item.Value.GetSpecificationExtension()),
})
}
if val := item.GetValue().Example; val != nil {
mt.Example = val.ToRawInfo()
}
content.Set(item.Name, mt)
}
return content
}
Expand Down Expand Up @@ -403,7 +406,11 @@ func toHeaders(v *goa3.HeadersOrReferences) *orderedmap.Map[string, *v3.Header]
func toCodes(responses []*goa3.NamedResponseOrReference) *orderedmap.Map[string, *v3.Response] {
resps := orderedmap.New[string, *v3.Response]()
for _, resp := range responses {
resps.Set(resp.Name, toResponse(resp.Value.GetResponse()))
switch tt := resp.GetValue().Oneof.(type) {
case *goa3.ResponseOrReference_Reference:
case *goa3.ResponseOrReference_Response:
resps.Set(resp.Name, toResponse(tt.Response))
}
}
return resps
}
Expand Down
11 changes: 10 additions & 1 deletion internal/converter/gnostic/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ func PathItemWithMethodAnnotations(item *v3.PathItem, md protoreflect.MethodDesc
}

if opts.Responses != nil {
oper.Responses = toResponses(opts.Responses)
responses := toResponses(opts.Responses)
for pair := responses.Codes.First(); pair != nil; pair = pair.Next() {
oper.Responses.Codes.Set(pair.Key(), pair.Value())
}
if responses.Default != nil {
oper.Responses.Default = responses.Default
}
for pair := responses.Extensions.First(); pair != nil; pair = pair.Next() {
oper.Responses.Extensions.Set(pair.Key(), pair.Value())
}
}

if opts.Callbacks != nil {
Expand Down
Binary file modified internal/converter/testdata/fileset.binpb
Binary file not shown.
20 changes: 20 additions & 0 deletions internal/converter/testdata/standard/gnostic.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ service Greeter {
rpc SayHello(HelloRequest) returns (HelloReply) {
option idempotency_level = NO_SIDE_EFFECTS;
option (gnostic.openapi.v3.operation) = {
responses: {
response_or_reference: {
name: "404"
value: {
response: {
description: "user is not found"
content: {
additional_properties: {
name: "application/json"
value: {
schema: {
reference: {_ref: "#/components/schemas/connect.error"}
}
}
}
}
}
}
}
}
deprecated: true
security: [
{
Expand Down
20 changes: 20 additions & 0 deletions internal/converter/testdata/standard/output/gnostic.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@
}
}
}
},
"404": {
"description": "user is not found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/connect.error"
}
}
}
}
},
"deprecated": true,
Expand Down Expand Up @@ -178,6 +188,16 @@
}
}
}
},
"404": {
"description": "user is not found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/connect.error"
}
}
}
}
},
"deprecated": true,
Expand Down
12 changes: 12 additions & 0 deletions internal/converter/testdata/standard/output/gnostic.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/example_with_gnostic.HelloReply'
"404":
description: user is not found
content:
application/json:
schema:
$ref: '#/components/schemas/connect.error'
deprecated: true
security:
- BasicAuth: []
Expand Down Expand Up @@ -109,6 +115,12 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/example_with_gnostic.HelloReply'
"404":
description: user is not found
content:
application/json:
schema:
$ref: '#/components/schemas/connect.error'
deprecated: true
security:
- BasicAuth: []
Expand Down

0 comments on commit eea3f4a

Please sign in to comment.