Skip to content

Commit ab56913

Browse files
committed
test: add allOf diff test cases for schema validation
1 parent e5e3ffb commit ab56913

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,32 @@ public void testAnyOfDiff() {
380380
assertThat(changedSchema.getRequired().getMissing()).containsExactly("fieldA");
381381
assertThat(changedSchema.getRequired().getIncreased()).isEmpty();
382382
}
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+
}
383411
}
Lines changed: 35 additions & 0 deletions
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
Lines changed: 34 additions & 0 deletions
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

0 commit comments

Comments
 (0)