Skip to content
Open
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 @@ -696,6 +696,11 @@ protected boolean isSelfReference(String name, Schema subSchema) {
* @return Schema
*/
public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
// normalize reference schema
if (StringUtils.isNotEmpty(schema.get$ref())) {
normalizeReferenceSchema(schema);
}

if (skipNormalization(schema, visitedSchemas)) {
return schema;
}
Expand Down Expand Up @@ -763,6 +768,30 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
return schema;
}

/**
* Normalize reference schema with allOf to support sibling properties
*
* @param schema Schema
*/
protected void normalizeReferenceSchema(Schema schema) {
if (schema.getTitle() != null || schema.getDescription() != null
|| schema.getNullable() != null || schema.getDefault() != null || schema.getDeprecated() != null
|| schema.getMaximum() != null || schema.getMinimum() != null
|| schema.getExclusiveMaximum() != null || schema.getExclusiveMinimum() != null
|| schema.getMaxItems() != null || schema.getMinItems() != null
|| schema.getMaxProperties() != null || schema.getMinProperties() != null
|| schema.getMaxLength() != null || schema.getMinLength() != null
|| schema.getWriteOnly() != null || schema.getReadOnly() != null
|| schema.getExample() != null || (schema.getExamples() != null && !schema.getExamples().isEmpty())
|| schema.getMultipleOf() != null || schema.getPattern() != null
|| (schema.getExtensions() != null && !schema.getExtensions().isEmpty())
) {
// create allOf with a $ref schema
schema.addAllOfItem(new Schema<>().$ref(schema.get$ref()));
// clear $ref in original schema
schema.set$ref(null);
}
}

/**
* Check if normalization is needed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,10 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf31SpecForIssue18184() {

Schema schema2 = openAPI.getComponents().getSchemas().get("Item");
assertEquals(((Schema) schema2.getProperties().get("my_enum")).getAnyOf(), null);
assertEquals(((Schema) schema2.getProperties().get("my_enum")).get$ref(), "#/components/schemas/MyEnum");
assertEquals(((Schema) schema2.getProperties().get("my_enum")).getAllOf().size(), 1);
assertEquals(((Schema) schema2.getProperties().get("my_enum")).getNullable(), true);
assertEquals(((Schema) schema2.getProperties().get("my_enum")).get$ref(), null);
assertEquals(((Schema) ((Schema) schema2.getProperties().get("my_enum")).getAllOf().get(0)).get$ref(), "#/components/schemas/MyEnum");
}

@Test
Expand Down Expand Up @@ -1104,7 +1107,10 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf31Spec() {
Schema schema18 = openAPI.getComponents().getSchemas().get("OneOfNullAndRef3");
// original oneOf removed and simplified to just $ref (oneOf sub-schema) instead
assertEquals(schema18.getOneOf(), null);
assertEquals(schema18.get$ref(), "#/components/schemas/Parent");
assertEquals(schema18.get$ref(), null);
assertEquals(schema18.getNullable(), true);
assertEquals(((Schema) schema18.getAllOf().get(0)).get$ref(), "#/components/schemas/Parent");


Schema schema20 = openAPI.getComponents().getSchemas().get("ParentWithOneOfProperty");
assertEquals(((Schema) schema20.getProperties().get("number")).get$ref(), "#/components/schemas/Number");
Expand Down Expand Up @@ -1184,6 +1190,24 @@ public void testOpenAPINormalizerProcessingAllOfSchema31Spec() {
assertEquals(((Schema) schema2.getProperties().get("property2")).getAllOf(), null);
}

@Test
public void testOpenAPINormalizerNormalizeReferenceSchema() {
// to test array schema processing in 3.1 spec
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/unsupported_schema_test.yaml");

Schema schema = openAPI.getComponents().getSchemas().get("Dummy");
assertEquals(((Schema) schema.getProperties().get("property3")).get$ref(), "#/components/schemas/RefSchema");

Map<String, String> inputRules = Map.of("NORMALIZE_31SPEC", "true");
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, inputRules);
openAPINormalizer.normalize();

Schema schema2 = openAPI.getComponents().getSchemas().get("Dummy");
assertEquals(((Schema) schema2.getProperties().get("property3")).getAllOf().size(), 1);
assertEquals(((Schema) schema2.getProperties().get("property3")).getDescription(), "Override description in $ref schema");
assertEquals(((Schema) ((Schema) schema2.getProperties().get("property3")).getAllOf().get(0)).get$ref(), "#/components/schemas/RefSchema");
}

@Test
public void testOpenAPINormalizerComponentsResponses31Spec() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/common-parameters.yaml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ paths:
$ref: "#/components/schemas/Dummy"
components:
schemas:
RefSchema:
description: reference schema
properties:
id:
type: string
Dummy:
type: object
properties:
Expand Down Expand Up @@ -61,4 +66,7 @@ components:
enum:
- FIRST
- SECOND
- THIRD
- THIRD
property3:
$ref: "#/components/schemas/RefSchema"
description: Override description in $ref schema
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export class DefaultApi {
/**
*
*/
public async testDecodeArrayOfNullableObjectsGet (options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.IncomingMessage; body: Array<ComplexObject>; }> {
public async testDecodeArrayOfNullableObjectsGet (options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.IncomingMessage; body: Array<ComplexObject | null>; }> {
const localVarPath = this.basePath + '/test/decode/array-of/nullable-objects';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this._defaultHeaders);
Expand Down Expand Up @@ -376,13 +376,13 @@ export class DefaultApi {
localVarRequestOptions.form = localVarFormParams;
}
}
return new Promise<{ response: http.IncomingMessage; body: Array<ComplexObject>; }>((resolve, reject) => {
return new Promise<{ response: http.IncomingMessage; body: Array<ComplexObject | null>; }>((resolve, reject) => {
localVarRequest(localVarRequestOptions, (error, response, body) => {
if (error) {
reject(error);
} else {
if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
body = ObjectSerializer.deserialize(body, "Array<ComplexObject>");
body = ObjectSerializer.deserialize(body, "Array<ComplexObject | null>");
resolve({ response: response, body: body });
} else {
reject(new HttpError(response, body, response.statusCode));
Expand Down Expand Up @@ -1187,7 +1187,7 @@ export class DefaultApi {
*
* @param complexObject
*/
public async testEncodeArrayOfNullableObjectsPost (complexObject: Array<ComplexObject>, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.IncomingMessage; body?: any; }> {
public async testEncodeArrayOfNullableObjectsPost (complexObject: Array<ComplexObject | null>, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.IncomingMessage; body?: any; }> {
const localVarPath = this.basePath + '/test/encode/array-of/nullable-objects';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this._defaultHeaders);
Expand All @@ -1209,7 +1209,7 @@ export class DefaultApi {
uri: localVarPath,
useQuerystring: this._useQuerystring,
json: true,
body: ObjectSerializer.serialize(complexObject, "Array<ComplexObject>")
body: ObjectSerializer.serialize(complexObject, "Array<ComplexObject | null>")
};

let authenticationPromise = Promise.resolve();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading