Skip to content
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

Fix: Bug#212 Added support for changes using AnyOf #334

Closed
wants to merge 1 commit into from
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 @@ -82,19 +82,26 @@ public static SchemaDiffResult getSchemaDiffResult(
protected static Schema<?> resolveComposedSchema(Components components, Schema<?> schema) {
if (schema instanceof ComposedSchema) {
ComposedSchema composedSchema = (ComposedSchema) schema;
List<Schema> allOfSchemaList = composedSchema.getAllOf();
if (allOfSchemaList != null) {
for (Schema<?> allOfSchema : allOfSchemaList) {
allOfSchema = refPointer.resolveRef(components, allOfSchema, allOfSchema.get$ref());
allOfSchema = resolveComposedSchema(components, allOfSchema);
schema = addSchema(schema, allOfSchema);
}
composedSchema.setAllOf(null);
List<Schema> anyOrAllOfSchemaList = getAnyOrAllOf(composedSchema);
for (Schema<?> anyOrAllOfSchema : anyOrAllOfSchemaList) {
anyOrAllOfSchema =
refPointer.resolveRef(components, anyOrAllOfSchema, anyOrAllOfSchema.get$ref());
anyOrAllOfSchema = resolveComposedSchema(components, anyOrAllOfSchema);
schema = addSchema(schema, anyOrAllOfSchema);
}
composedSchema.setAllOf(null);
composedSchema.setAnyOf(null);
}
return schema;
}

private static List<Schema> getAnyOrAllOf(ComposedSchema composedSchema) {
List<Schema> schemas = new ArrayList<>();
Optional.ofNullable(composedSchema.getAllOf()).ifPresent(schemas::addAll);
Optional.ofNullable(composedSchema.getAnyOf()).ifPresent(schemas::addAll);
return schemas;
}

protected static Schema<?> addSchema(Schema<?> schema, Schema<?> fromSchema) {
if (fromSchema.getProperties() != null) {
if (schema.getProperties() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.openapitools.openapidiff.core;

import static org.openapitools.openapidiff.core.TestUtils.assertOpenApiAreEquals;
import static org.openapitools.openapidiff.core.TestUtils.assertOpenApiChangedEndpoints;

import org.junit.jupiter.api.Test;

public class AnyOfDiffTest {

private final String OPENAPI_DOC1 = "anyOf_diff_1.yaml";
private final String OPENAPI_DOC2 = "anyOf_diff_2.yaml";
private final String OPENAPI_DOC3 = "anyOf_diff_3.yaml";
private final String OPENAPI_DOC4 = "anyOf_diff_4.yaml";

@Test
public void testDiffSame() {
assertOpenApiAreEquals(OPENAPI_DOC1, OPENAPI_DOC1);
}

@Test
public void testDiffSameWithAnyOf() {
assertOpenApiAreEquals(OPENAPI_DOC1, OPENAPI_DOC2);
}

@Test
public void testDiffDifferent1() {
assertOpenApiChangedEndpoints(OPENAPI_DOC1, OPENAPI_DOC3);
}

@Test
public void testDiffDifferent2() {
assertOpenApiChangedEndpoints(OPENAPI_DOC1, OPENAPI_DOC4);
}
}
129 changes: 129 additions & 0 deletions core/src/test/resources/anyOf_diff_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
openapi: 3.0.0
servers:
- url: 'http://petstore.swagger.io/v2'
info:
description: >-
This is a sample server Petstore server. You can find out more about
Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net,
#swagger](http://swagger.io/irc/). For this sample, you can use the api key
`special-key` to test the authorization filters.
version: 1.0.0
title: Swagger Petstore
termsOfService: 'http://swagger.io/terms/'
contact:
email: [email protected]
license:
name: Apache 2.0
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
tags:
- name: pet
description: Everything about your Pets
externalDocs:
description: Find out more
url: 'http://swagger.io'
- name: store
description: Access to Petstore orders
- name: user
description: Operations about user
externalDocs:
description: Find out more about our store
url: 'http://swagger.io'
paths:
/pet/findByStatus:
get:
tags:
- pet
summary: Finds Pets by status
description: Multiple status values can be provided with comma separated strings
operationId: findPetsByStatus
parameters:
- name: status
in: query
description: Status values that need to be considered for filter
required: true
explode: true
schema:
type: array
items:
type: string
enum:
- available
- pending
- sold
default: available
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
properties:
pets:
type: array
items:
$ref: '#/components/schemas/Cat'
'400':
description: Invalid status value
security:
- petstore_auth:
- 'write:pets'
- 'read:pets'
externalDocs:
description: Find out more about Swagger
url: 'http://swagger.io'
components:
requestBodies:
Pet:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store
required: true
securitySchemes:
petstore_auth:
type: oauth2
flows:
implicit:
authorizationUrl: 'http://petstore.swagger.io/oauth/dialog'
scopes:
'write:pets': modify pets in your account
'read:pets': read your pets
api_key:
type: apiKey
name: api_key
in: header
schemas:
BasePet:
type: object
properties:
pet_color:
type: string
Pet:
anyOf:
- $ref: '#/components/schemas/BasePet'
type: object
required:
- pet_type
properties:
pet_type:
nullable: false
anyOf:
- type: string
Cat:
description: Cat class
anyOf:
- $ref: '#/components/schemas/Pet'
type: object
properties:
name:
type: string
Dog:
description: Dog class
anyOf:
- $ref: '#/components/schemas/Pet'
type: object
properties:
bark:
type: string
125 changes: 125 additions & 0 deletions core/src/test/resources/anyOf_diff_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
openapi: 3.0.0
servers:
- url: 'http://petstore.swagger.io/v2'
info:
description: >-
This is a sample server Petstore server. You can find out more about
Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net,
#swagger](http://swagger.io/irc/). For this sample, you can use the api key
`special-key` to test the authorization filters.
version: 1.0.0
title: Swagger Petstore
termsOfService: 'http://swagger.io/terms/'
contact:
email: [email protected]
license:
name: Apache 2.0
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
tags:
- name: pet
description: Everything about your Pets
externalDocs:
description: Find out more
url: 'http://swagger.io'
- name: store
description: Access to Petstore orders
- name: user
description: Operations about user
externalDocs:
description: Find out more about our store
url: 'http://swagger.io'
paths:
/pet/findByStatus:
get:
tags:
- pet
summary: Finds Pets by status
description: Multiple status values can be provided with comma separated strings
operationId: findPetsByStatus
parameters:
- name: status
in: query
description: Status values that need to be considered for filter
required: true
explode: true
schema:
type: array
items:
type: string
enum:
- available
- pending
- sold
default: available
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
properties:
pets:
type: array
items:
$ref: '#/components/schemas/Cat'
'400':
description: Invalid status value
security:
- petstore_auth:
- 'write:pets'
- 'read:pets'
externalDocs:
description: Find out more about Swagger
url: 'http://swagger.io'
components:
requestBodies:
Pet:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store
required: true
securitySchemes:
petstore_auth:
type: oauth2
flows:
implicit:
authorizationUrl: 'http://petstore.swagger.io/oauth/dialog'
scopes:
'write:pets': modify pets in your account
'read:pets': read your pets
api_key:
type: apiKey
name: api_key
in: header
schemas:
Pet:
type: object
required:
- pet_type
properties:
pet_type:
nullable: false
type: string
Cat:
description: Cat class
type: object
required:
- pet_type
properties:
pet_type:
type: string
name:
type: string
pet_color:
type: string
Dog:
description: Dog class
anyOf:
- $ref: '#/components/schemas/Pet'
type: object
properties:
bark:
type: string
Loading