Skip to content

Commit f659026

Browse files
authored
DrSatyr/issue212 (#774)
* test: add AnyOf diff test cases for schema validation * test: add allOf diff test cases for schema validation
1 parent 0c3586e commit f659026

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

core/src/test/java/org/openapitools/openapidiff/core/SchemaDiffTest.java

+56
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,60 @@ public void changePatternHandling() {
352352
assertThat(props.get("patternAdded").getPattern().getNewPattern())
353353
.isEqualTo("^\\d{3}-\\d{2}-\\d{4}$?");
354354
}
355+
356+
@Test // issue #212
357+
public void testAnyOfDiff() {
358+
ChangedOpenApi changedOpenApi =
359+
OpenApiCompare.fromLocations(
360+
"schemaDiff/anyOf-diff-1.yaml", "schemaDiff/anyOf-diff-2.yaml");
361+
ChangedSchema changedSchema =
362+
getRequestBodyChangedSchema(changedOpenApi, POST, "/anyof/test", "application/json");
363+
364+
assertThat(changedSchema).isNotNull();
365+
// The diff compares the *merged* schema resulting from anyOf, not the anyOf structure itself.
366+
// See details in #772
367+
assertThat(changedSchema.isChanged()).isEqualTo(DiffResult.COMPATIBLE);
368+
369+
// fieldA only changed required status, not its schema
370+
assertThat(changedSchema.getChangedProperties()).isEmpty();
371+
372+
// fieldB: Removed from the merged schema properties
373+
assertThat(changedSchema.getMissingProperties()).containsKey("fieldB");
374+
375+
// fieldC: Added to the merged schema properties
376+
assertThat(changedSchema.getIncreasedProperties()).containsKey("fieldC");
377+
378+
// Check the overall required list changes for the merged schema
379+
assertThat(changedSchema.getRequired().isChanged()).isEqualTo(DiffResult.COMPATIBLE);
380+
assertThat(changedSchema.getRequired().getMissing()).containsExactly("fieldA");
381+
assertThat(changedSchema.getRequired().getIncreased()).isEmpty();
382+
}
383+
384+
@Test // issue #212 - adapted for allOf
385+
public void testAllOfDiff() {
386+
ChangedOpenApi changedOpenApi =
387+
OpenApiCompare.fromLocations(
388+
"schemaDiff/allOf-diff-1.yaml", "schemaDiff/allOf-diff-2.yaml");
389+
ChangedSchema changedSchema =
390+
getRequestBodyChangedSchema(changedOpenApi, POST, "/allof/test", "application/json");
391+
392+
assertThat(changedSchema).isNotNull();
393+
// The diff compares the *merged* schema resulting from allOf, not the allOf structure itself.
394+
// See details in #772
395+
assertThat(changedSchema.isChanged()).isEqualTo(DiffResult.COMPATIBLE);
396+
397+
// fieldA only changed required status, commonField is unchanged
398+
assertThat(changedSchema.getChangedProperties()).isEmpty();
399+
400+
// fieldB: Removed from the merged schema properties
401+
assertThat(changedSchema.getMissingProperties()).containsKey("fieldB");
402+
403+
// fieldC: Added to the merged schema properties
404+
assertThat(changedSchema.getIncreasedProperties()).containsKey("fieldC");
405+
406+
// Check the overall required list changes for the merged schema
407+
assertThat(changedSchema.getRequired().isChanged()).isEqualTo(DiffResult.COMPATIBLE);
408+
assertThat(changedSchema.getRequired().getMissing()).containsExactly("fieldA");
409+
assertThat(changedSchema.getRequired().getIncreased()).isEmpty();
410+
}
355411
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
openapi: 3.0.0
2+
info:
3+
title: AllOf Diff Test - Version 1
4+
version: 1.0.0
5+
paths:
6+
/allof/test:
7+
post:
8+
summary: Test endpoint for allOf diff
9+
requestBody:
10+
required: true
11+
content:
12+
application/json:
13+
schema:
14+
allOf:
15+
- $ref: '#/components/schemas/TypeA'
16+
- $ref: '#/components/schemas/TypeB'
17+
responses:
18+
'200':
19+
description: OK
20+
components:
21+
schemas:
22+
TypeA:
23+
type: object
24+
required:
25+
- fieldA
26+
properties:
27+
fieldA:
28+
type: string
29+
commonField:
30+
type: integer
31+
TypeB:
32+
type: object
33+
properties:
34+
fieldB:
35+
type: boolean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
openapi: 3.0.0
2+
info:
3+
title: AllOf Diff Test - Version 2
4+
version: 1.0.0
5+
paths:
6+
/allof/test:
7+
post:
8+
summary: Test endpoint for allOf diff
9+
requestBody:
10+
required: true
11+
content:
12+
application/json:
13+
schema:
14+
allOf:
15+
- $ref: '#/components/schemas/TypeA'
16+
- $ref: '#/components/schemas/TypeC'
17+
responses:
18+
'200':
19+
description: OK
20+
components:
21+
schemas:
22+
TypeA:
23+
type: object
24+
# fieldA is no longer required here
25+
properties:
26+
fieldA:
27+
type: string
28+
commonField:
29+
type: integer
30+
TypeC:
31+
type: object
32+
properties:
33+
fieldC:
34+
type: number
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
openapi: 3.0.0
2+
info:
3+
title: AnyOf Diff Test - Version 1
4+
version: 1.0.0
5+
paths:
6+
/anyof/test:
7+
post:
8+
summary: Test endpoint for anyOf diff
9+
requestBody:
10+
required: true
11+
content:
12+
application/json:
13+
schema:
14+
anyOf:
15+
- $ref: '#/components/schemas/TypeA'
16+
- $ref: '#/components/schemas/TypeB'
17+
responses:
18+
'200':
19+
description: OK
20+
components:
21+
schemas:
22+
TypeA:
23+
type: object
24+
properties:
25+
fieldA:
26+
type: string
27+
commonField:
28+
type: integer
29+
required:
30+
- fieldA
31+
TypeB:
32+
type: object
33+
properties:
34+
fieldB:
35+
type: boolean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.0
2+
info:
3+
title: AnyOf Diff Test - Version 2
4+
version: 1.0.0
5+
paths:
6+
/anyof/test:
7+
post:
8+
summary: Test endpoint for anyOf diff
9+
requestBody:
10+
required: true
11+
content:
12+
application/json:
13+
schema:
14+
anyOf:
15+
- $ref: '#/components/schemas/TypeA'
16+
- $ref: '#/components/schemas/TypeC'
17+
responses:
18+
'200':
19+
description: OK
20+
components:
21+
schemas:
22+
TypeA:
23+
type: object
24+
properties:
25+
fieldA:
26+
type: string
27+
commonField:
28+
type: integer
29+
TypeC:
30+
type: object
31+
properties:
32+
fieldC:
33+
type: number

0 commit comments

Comments
 (0)