Skip to content

Commit 4e8bfcf

Browse files
committed
fix: prevent similar components from being overwritten
When collating schemas from multiple services, it is very likely that components with the same name will be defined in several places. We use a comparison function to detect when this is the case and inline components if they have already been declared. This comparison function however ignored fields meant for documenting fields. Which is fine when it was written as the only concern was generating openapi specs inner service to validate requests. However now we are using the collated specs to generate documentation, this behaviour is very surprising. Especially when the description is context sensitive. This will increase the size of our specs slightly, it should not be that significant. It will require us to recompile our existing specs to fix the broken versions.
1 parent 93f01b0 commit 4e8bfcf

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

collator.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,7 @@ var cmpComponents = cmp.Options{
244244
// Refs themselves can mutate during relocation, so they are excluded from
245245
// content comparison.
246246
cmp.FilterPath(func(p cmp.Path) bool {
247-
switch p.Last().String() {
248-
case ".Ref", ".Description", ".Example", ".Summary":
249-
return true
250-
}
251-
return false
247+
return p.Last().String() == ".Ref"
252248
}, cmp.Ignore()),
253249
}
254250

collator_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ func TestCollator(t *testing.T) {
9696
"\"Requested API version\",\"pattern\":\"^(wip|work-in-progress|experimental|beta|((([0-9]{4})-([0-1][0-9]))"+
9797
"-((3[01])|(0[1-9])|([12][0-9]))(~(wip|work-in-progress|experimental|beta))?))$\",\"type\":\"string\"}}\n", qt.JSONEquals, exampleParameterRef.Value)
9898

99+
projectConflictRef := result.Paths["/orgs/{orgId}/projects"].Get.Parameters[6]
100+
exampleConflictRef := result.Paths["/examples/hello-world/{id}"].Get.Parameters[3]
101+
c.Assert(projectConflictRef.Ref, qt.Not(qt.Equals), exampleConflictRef.Ref)
102+
99103
projectResp400Ref := result.Paths["/orgs/{orgId}/projects"].Get.Responses["400"]
100104
c.Assert(projectResp400Ref.Ref, qt.Equals, "#/components/responses/400")
101105
exampleResp400Ref := result.Paths["/examples/hello-world/{id}"].Get.Responses["400"]

testdata/conflict-components/_examples/2021-06-01/spec.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ paths:
2020
required: true
2121
schema:
2222
type: string
23+
- $ref: '#/components/parameters/Conflict'
2324
responses:
2425
'400': { $ref: 'https://raw.githubusercontent.com/snyk/sweater-comb/v1.2.2/components/responses/400.yaml#/400' }
2526
'401': { $ref: 'https://raw.githubusercontent.com/snyk/sweater-comb/v1.2.2/components/responses/401.yaml#/401' }
@@ -43,6 +44,14 @@ paths:
4344
additionalProperties: false
4445

4546
components:
47+
parameters:
48+
Conflict:
49+
name: conflict
50+
in: query
51+
required: true
52+
description: Initial conflict description
53+
schema:
54+
type: string
4655
schemas:
4756
HelloWorld:
4857
type: object

testdata/conflict-components/projects/2021-06-04/spec.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ components:
1717
description: The requested version of the endpoint to process the request
1818
schema:
1919
type: number
20+
Conflict:
21+
name: conflict
22+
in: query
23+
required: true
24+
description: Different conflict description
25+
schema:
26+
type: string
2027
schemas:
2128
JsonApi:
2229
type: object
@@ -78,6 +85,7 @@ paths:
7885
type: array
7986
items:
8087
type: string
88+
- $ref: '#/components/parameters/Conflict'
8189
responses:
8290
'400': { $ref: '../../../resources/schemas/responses/400.yaml#/400' }
8391
'401': { $ref: 'https://raw.githubusercontent.com/snyk/sweater-comb/v1.2.2/components/responses/401.yaml#/401' }

0 commit comments

Comments
 (0)