Skip to content

Commit e5e3ffb

Browse files
committed
test: add AnyOf diff test cases for schema validation
1 parent 19bdb61 commit e5e3ffb

3 files changed

Lines changed: 96 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
@@ -352,4 +352,32 @@ 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+
}
355383
}
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: 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
Lines changed: 33 additions & 0 deletions
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)