Skip to content

Commit 4175ea2

Browse files
octomadAndrew Young
andauthored
Fix allOf merges using external refs (oapi-codegen#941)
Co-authored-by: Andrew Young <andrew.young@verizonmedia.com>
1 parent 4a2bef6 commit 4175ea2

5 files changed

Lines changed: 57 additions & 5 deletions

File tree

pkg/codegen/codegen_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,11 @@ func TestExampleOpenAPICodeGeneration(t *testing.T) {
145145
},
146146
}
147147

148+
loader := openapi3.NewLoader()
149+
loader.IsExternalRefsAllowed = true
150+
148151
// Get a spec from the test definition in this file:
149-
swagger, err := openapi3.NewLoader().LoadFromData([]byte(testOpenAPIDefinition))
152+
swagger, err := loader.LoadFromData([]byte(testOpenAPIDefinition))
150153
assert.NoError(t, err)
151154

152155
// Run our code generation:

pkg/codegen/filter_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ func TestFilterOperationsByTag(t *testing.T) {
2323
},
2424
}
2525

26+
loader := openapi3.NewLoader()
27+
loader.IsExternalRefsAllowed = true
28+
2629
// Get a spec from the test definition in this file:
27-
swagger, err := openapi3.NewLoader().LoadFromData([]byte(testOpenAPIDefinition))
30+
swagger, err := loader.LoadFromData([]byte(testOpenAPIDefinition))
2831
assert.NoError(t, err)
2932

3033
// Run our code generation:
@@ -49,8 +52,11 @@ func TestFilterOperationsByTag(t *testing.T) {
4952
},
5053
}
5154

55+
loader := openapi3.NewLoader()
56+
loader.IsExternalRefsAllowed = true
57+
5258
// Get a spec from the test definition in this file:
53-
swagger, err := openapi3.NewLoader().LoadFromData([]byte(testOpenAPIDefinition))
59+
swagger, err := loader.LoadFromData([]byte(testOpenAPIDefinition))
5460
assert.NoError(t, err)
5561

5662
// Run our code generation:

pkg/codegen/merge_schemas.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ func valueWithPropagatedRef(ref *openapi3.SchemaRef) (openapi3.Schema, error) {
5353
}
5454

5555
pathParts := strings.Split(ref.Ref, "#")
56-
if len(pathParts) != 2 {
56+
if len(pathParts) < 1 || len(pathParts) > 2 {
5757
return openapi3.Schema{}, fmt.Errorf("unsupported reference: %s", ref.Ref)
5858
}
59-
remoteComponent, _ := pathParts[0], pathParts[1]
59+
remoteComponent := pathParts[0]
6060

6161
// remote ref
6262
schema := *ref.Value

pkg/codegen/test_schema.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"title": "node",
3+
"type": "object",
4+
"description": "Represents a node",
5+
"properties": {
6+
"id": {
7+
"type": "string",
8+
"format": "uri-reference"
9+
},
10+
"type": {
11+
"type": "string",
12+
"pattern": "^[A-Z][a-zA-Z0-9]*$"
13+
}
14+
}
15+
}

pkg/codegen/test_spec.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ paths:
108108
application/json:
109109
schema:
110110
$ref: '#/components/schemas/Error'
111+
/user:
112+
get:
113+
tags:
114+
- mergeAllOf
115+
summary: Merges allOf ref-ing a JSON schema
116+
operationId: getUser
117+
responses:
118+
200:
119+
description: Success
120+
content:
121+
application/json:
122+
schema:
123+
$ref: '#/components/schemas/User'
124+
default:
125+
description: Error
126+
content:
127+
application/json:
128+
schema:
129+
$ref: '#/components/schemas/Error'
111130

112131
components:
113132
schemas:
@@ -177,3 +196,12 @@ components:
177196
- na
178197
- single
179198
- double
199+
User:
200+
allOf:
201+
- $ref: ./test_schema.json
202+
- type: object
203+
additionalProperties: false
204+
properties:
205+
name:
206+
type: string
207+
description: User name

0 commit comments

Comments
 (0)