Skip to content

Commit e230c13

Browse files
openapi2conv: convert schemaRef for additional props (#1030)
1 parent 4fda0cc commit e230c13

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

openapi2conv/issue1016_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package openapi2conv
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"testing"
7+
8+
"github.com/getkin/kin-openapi/openapi2"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestIssue1016(t *testing.T) {
13+
v2 := []byte(`
14+
{
15+
"basePath": "/v2",
16+
"host": "test.example.com",
17+
"info": {
18+
"title": "MyAPI",
19+
"version": "0.1",
20+
"x-info": "info extension"
21+
},
22+
"paths": {
23+
"/foo": {
24+
"get": {
25+
"operationId": "getFoo",
26+
"responses": {
27+
"200": {
28+
"description": "returns all information",
29+
"schema": {
30+
"$ref": "#/definitions/PetDirectory"
31+
}
32+
},
33+
"default": {
34+
"description": "OK"
35+
}
36+
},
37+
"summary": "get foo"
38+
}
39+
}
40+
},
41+
"schemes": [
42+
"http"
43+
],
44+
"swagger": "2.0",
45+
"definitions": {
46+
"Pet": {
47+
"type": "object",
48+
"required": ["petType"],
49+
"properties": {
50+
"petType": {
51+
"type": "string"
52+
},
53+
"name": {
54+
"type": "string"
55+
},
56+
"age": {
57+
"type": "integer"
58+
}
59+
}
60+
},
61+
"PetDirectory":{
62+
"type": "object",
63+
"additionalProperties": {
64+
"$ref": "#/definitions/Pet"
65+
}
66+
}
67+
}
68+
}
69+
`)
70+
71+
var doc2 openapi2.T
72+
err := json.Unmarshal(v2, &doc2)
73+
require.NoError(t, err)
74+
75+
doc3, err := v2v3YAML(v2)
76+
require.NoError(t, err)
77+
78+
err = doc3.Validate(context.Background())
79+
require.NoError(t, err)
80+
require.Equal(t, "#/components/schemas/Pet", doc3.Components.Schemas["PetDirectory"].Value.AdditionalProperties.Schema.Ref)
81+
}

openapi2conv/openapi2_conv.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,10 @@ func ToV3SchemaRef(schema *openapi2.SchemaRef) *openapi3.SchemaRef {
516516
AdditionalProperties: schema.Value.AdditionalProperties,
517517
}
518518

519+
if schema.Value.AdditionalProperties.Schema != nil {
520+
v3Schema.AdditionalProperties.Schema.Ref = ToV3Ref(schema.Value.AdditionalProperties.Schema.Ref)
521+
}
522+
519523
if schema.Value.Discriminator != "" {
520524
v3Schema.Discriminator = &openapi3.Discriminator{
521525
PropertyName: schema.Value.Discriminator,

0 commit comments

Comments
 (0)