Skip to content

Commit 72b5d51

Browse files
roberth1988Robert Hoppe
and
Robert Hoppe
authored
openapi2conv: fix for refs on items within additional properties (#1067)
* Introduce fix for refs on items within additional properties * Remove space --------- Co-authored-by: Robert Hoppe <robert.hoppe@mail.schwarz>
1 parent 75e2cc5 commit 72b5d51

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

openapi2conv/issue1049_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package openapi2conv
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestIssue1049(t *testing.T) {
12+
spec := `
13+
{
14+
"swagger": "2.0",
15+
"info": {
16+
"description": "Test for additionalProperties",
17+
"version": "v1",
18+
"title": "API Test"
19+
},
20+
"host": "Test",
21+
"paths": {
22+
"/map": {
23+
"post": {
24+
"summary": "api test summary",
25+
"description": "api test description",
26+
"operationId": "apiTestUsingPOST",
27+
"consumes": ["application/json"],
28+
"produces": ["*/*"],
29+
"responses": {
30+
"200": {
31+
"description": "OK",
32+
"schema": {
33+
"$ref": "#/definitions/ResponseOfMap"
34+
}
35+
}
36+
}
37+
}
38+
}
39+
},
40+
"definitions": {
41+
"BaseDictResp": {
42+
"type": "object",
43+
"properties": {
44+
"key": {
45+
"type": "string",
46+
"description": "key of map"
47+
},
48+
"value": {
49+
"type": "string",
50+
"description": "value of map"
51+
}
52+
},
53+
"title": "BaseDictResp",
54+
"description": "BaseDictResp description"
55+
},
56+
57+
"ResponseOfMap": {
58+
"type": "object",
59+
"properties": {
60+
"data": {
61+
"type": "object",
62+
"description": "response data",
63+
"additionalProperties": {
64+
"type": "array",
65+
"items": {
66+
"$ref": "#/definitions/BaseDictResp"
67+
}
68+
}
69+
}
70+
},
71+
"title": "ResponseOfMap"
72+
}
73+
}
74+
}
75+
`
76+
doc3, err := v2v3JSON([]byte(spec))
77+
require.NoError(t, err)
78+
79+
spec3, err := json.Marshal(doc3)
80+
require.NoError(t, err)
81+
const expected = `{"components":{"schemas":{"BaseDictResp":{"description":"BaseDictResp description","properties":{"key":{"description":"key of map","type":"string"},"value":{"description":"value of map","type":"string"}},"title":"BaseDictResp","type":"object"},"ResponseOfMap":{"properties":{"data":{"additionalProperties":{"items":{"$ref":"#/components/schemas/BaseDictResp"},"type":"array"},"description":"response data","type":"object"}},"title":"ResponseOfMap","type":"object"}}},"info":{"description":"Test for additionalProperties","title":"API Test","version":"v1"},"openapi":"3.0.3","paths":{"/map":{"post":{"description":"api test description","operationId":"apiTestUsingPOST","responses":{"200":{"content":{"*/*":{"schema":{"$ref":"#/components/schemas/ResponseOfMap"}}},"description":"OK"}},"summary":"api test summary"}}},"servers":[{"url":"https://Test/"}]}`
82+
require.JSONEq(t, expected, string(spec3))
83+
84+
err = doc3.Validate(context.Background())
85+
require.NoError(t, err)
86+
}

openapi2conv/openapi2_conv.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,9 @@ func convertRefsInV3SchemaRef(from *openapi3.SchemaRef) *openapi3.SchemaRef {
563563
if to.Value != nil {
564564
v := *from.Value
565565
to.Value = &v
566+
if to.Value.Items != nil {
567+
to.Value.Items.Ref = ToV3Ref(to.Value.Items.Ref)
568+
}
566569
to.Value.AdditionalProperties = toV3AdditionalProperties(to.Value.AdditionalProperties)
567570
}
568571
return &to

0 commit comments

Comments
 (0)