Skip to content

allow optional discriminator in oneOf #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public <T extends Schema<X>, X> Optional<ChangedSchema> diff(

Discriminator leftDis = leftComposedSchema.getDiscriminator();
Discriminator rightDis = rightComposedSchema.getDiscriminator();
if (leftDis == null
|| rightDis == null
|| leftDis.getPropertyName() == null
|| rightDis.getPropertyName() == null) {
throw new IllegalArgumentException(
"discriminator or property not found for oneOf schema");
} else if (!leftDis.getPropertyName().equals(rightDis.getPropertyName())
|| (CollectionUtils.isEmpty(leftComposedSchema.getOneOf())
|| CollectionUtils.isEmpty(rightComposedSchema.getOneOf()))) {
if ((leftDis == null && rightDis != null)
|| (leftDis != null && rightDis == null)
|| (leftDis != null && rightDis != null && (
(leftDis.getPropertyName() == null && rightDis.getPropertyName() != null)
|| (leftDis.getPropertyName() != null && rightDis.getPropertyName() == null)
|| (leftDis.getPropertyName() != null && rightDis.getPropertyName() != null &&
!leftDis.getPropertyName().equals(rightDis.getPropertyName()))
))
) {
changedSchema.setOldSchema(left);
changedSchema.setNewSchema(right);
changedSchema.setDiscriminatorPropertyChanged(true);
Expand Down Expand Up @@ -109,7 +109,7 @@ private Map<String, String> getMapping(ComposedSchema composedSchema) {
reverseMapping.put(ref, schemaName);
}

if (composedSchema.getDiscriminator().getMapping() != null) {
if (composedSchema.getDiscriminator() != null && composedSchema.getDiscriminator().getMapping() != null) {
for (String ref : composedSchema.getDiscriminator().getMapping().keySet()) {
reverseMapping.put(composedSchema.getDiscriminator().getMapping().get(ref), ref);
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/qdesrame/openapi/test/OneOfDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class OneOfDiffTest {
private final String OPENAPI_DOC5 = "composed_schema_2.yaml";
private final String OPENAPI_DOC6 = "oneOf_discriminator-changed_1.yaml";
private final String OPENAPI_DOC7 = "oneOf_discriminator-changed_2.yaml";
private final String OPENAPI_DOC8 = "oneOf_discriminator-missing_1.yaml";
private final String OPENAPI_DOC9 = "oneOf_discriminator-missing_2.yaml";

@Test
public void testDiffSame() {
Expand All @@ -42,4 +44,14 @@ public void testOneOfDiscrimitatorChanged() {
// The oneOf 'discriminator' changed: 'realtype' -> 'othertype':
assertOpenApiBackwardIncompatible(OPENAPI_DOC6, OPENAPI_DOC7);
}

@Test
public void testOneOfDiscrimitatorMissingSameOrder() {
assertOpenApiAreEquals(OPENAPI_DOC8, OPENAPI_DOC8);
}

@Test
public void testOneOfDiscrimitatorMissingDifferentOrder() {
assertOpenApiAreEquals(OPENAPI_DOC8, OPENAPI_DOC9);
}
}
44 changes: 44 additions & 0 deletions src/test/resources/oneOf_discriminator-missing_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
openapi: 3.0.1
info:
title: oneOf test for issue 29
version: '1.0'
servers:
- url: 'http://localhost:8000/'
paths:
/state:
post:
operationId: update
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/A'
- $ref: '#/components/schemas/B'
required: true
responses:
'201':
description: OK
components:
schemas:
A:
type: object
properties:
realtype:
type: string
othertype:
type: string
message:
type: string
B:
type: object
properties:
realtype:
type: string
othertype:
type: string
description:
type: string
code:
type: integer
format: int32
44 changes: 44 additions & 0 deletions src/test/resources/oneOf_discriminator-missing_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
openapi: 3.0.1
info:
title: oneOf test for issue 29
version: '1.0'
servers:
- url: 'http://localhost:8000/'
paths:
/state:
post:
operationId: update
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/B'
- $ref: '#/components/schemas/A'
required: true
responses:
'201':
description: OK
components:
schemas:
A:
type: object
properties:
realtype:
type: string
othertype:
type: string
message:
type: string
B:
type: object
properties:
realtype:
type: string
othertype:
type: string
description:
type: string
code:
type: integer
format: int32