Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into fix17472
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfinne committed Jan 30, 2024
2 parents 0cdcb36 + 301f25a commit 5de2365
Show file tree
Hide file tree
Showing 216 changed files with 10,724 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7358,7 +7358,20 @@ protected void addBodyModelSchema(CodegenParameter codegenParameter, String name
}

protected void updateRequestBodyForMap(CodegenParameter codegenParameter, Schema schema, String name, Set<String> imports, String bodyParameterName) {
if (StringUtils.isNotBlank(name) && !(ModelUtils.isFreeFormObject(schema) && !ModelUtils.shouldGenerateFreeFormObjectModel(name, this))) {
boolean useModel = true;
if (StringUtils.isBlank(name)) {
useModel = false;
} else {
if (ModelUtils.isFreeFormObject(schema)) {
useModel = ModelUtils.shouldGenerateFreeFormObjectModel(name, this);
} else if (ModelUtils.isMapSchema(schema)) {
useModel = ModelUtils.shouldGenerateMapModel(schema);
} else if (ModelUtils.isArraySchema(schema)) {
useModel = ModelUtils.shouldGenerateArrayModel(schema);
}
}

if (useModel) {
this.addBodyModelSchema(codegenParameter, name, schema, imports, bodyParameterName, true);
} else {
Schema inner = ModelUtils.getAdditionalProperties(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,13 @@ void generateModels(List<File> files, List<ModelMap> allModels, List<String> unu
continue;
}
} else if (ModelUtils.isMapSchema(schema)) { // check to see if it's a "map" model
// A composed schema (allOf, oneOf, anyOf) is considered a Map schema if the additionalproperties attribute is set
// for that composed schema. However, in the case of a composed schema, the properties are defined or referenced
// in the inner schemas, and the outer schema does not have properties.
if (!ModelUtils.isGenerateAliasAsModel(schema) && !ModelUtils.isComposedSchema(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty())) {
if (!ModelUtils.shouldGenerateMapModel(schema)) {
// schema without property, i.e. alias to map
LOGGER.info("Model {} not generated since it's an alias to map (without property) and `generateAliasAsModel` is set to false (default)", name);
continue;
}
} else if (ModelUtils.isArraySchema(schema)) { // check to see if it's an "array" model
if (!ModelUtils.isGenerateAliasAsModel(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty())) {
if (!ModelUtils.shouldGenerateArrayModel(schema)) {
// schema without property, i.e. alias to array
LOGGER.info("Model {} not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)", name);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,15 @@ private Schema processSimplifyAnyOfStringAndEnumString(Schema schema) {
*
* @param schema Schema
*/
private boolean isNullTypeSchema(Schema schema) {
public boolean isNullTypeSchema(Schema schema) {
if (schema == null) {
return true;
}

if (ModelUtils.hasAllOf(schema) || ModelUtils.hasOneOf(schema) || ModelUtils.hasAnyOf(schema)) {
return false;
}

if (schema.getTypes() != null && !schema.getTypes().isEmpty()) {
// 3.1 spec
if (schema.getTypes().size() ==1) { // 1 type only
Expand All @@ -753,7 +757,7 @@ private boolean isNullTypeSchema(Schema schema) {
}
}

if ((schema.getType() == null || schema.getType().equals("null")) && schema.get$ref() == null) {
if (!(schema instanceof JsonSchema) && (schema.getType() == null || schema.getType().equals("null")) && schema.get$ref() == null) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,17 @@ public static boolean shouldGenerateFreeFormObjectModel(String name, CodegenConf
return unaliasedSchema.get$ref() != null;
}

public static boolean shouldGenerateMapModel(Schema schema) {
// A composed schema (allOf, oneOf, anyOf) is considered a Map schema if the additionalproperties attribute is set
// for that composed schema. However, in the case of a composed schema, the properties are defined or referenced
// in the inner schemas, and the outer schema does not have properties.
return ModelUtils.isGenerateAliasAsModel(schema) || ModelUtils.isComposedSchema(schema) || !(schema.getProperties() == null || schema.getProperties().isEmpty());
}

public static boolean shouldGenerateArrayModel(Schema schema) {
return ModelUtils.isGenerateAliasAsModel(schema) || !(schema.getProperties() == null || schema.getProperties().isEmpty());
}

/**
* If a Schema contains a reference to another Schema with '$ref', returns the referenced Schema if it is found or the actual Schema in the other cases.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOfStringAndEnumString() {
assertTrue(schema3.getEnum().size() > 0);
}

@Test
public void isNullTypeSchemaTest() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml");
Map<String, String> options = new HashMap<>();
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
Schema schema = openAPI.getComponents().getSchemas().get("AnyOfStringArrayOfString");
assertFalse(openAPINormalizer.isNullTypeSchema(schema));
}

@Test
public void testOpenAPINormalizerSimplifyOneOfAnyOf() {
// to test the rule SIMPLIFY_ONEOF_ANYOF
Expand All @@ -156,6 +165,9 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf() {
Schema schema7 = openAPI.getComponents().getSchemas().get("Parent");
assertEquals(((Schema) schema7.getProperties().get("number")).getAnyOf().size(), 1);

Schema schema9 = openAPI.getComponents().getSchemas().get("AnyOfStringArrayOfString");
assertEquals(schema9.getAnyOf().size(), 2);

Map<String, String> options = new HashMap<>();
options.put("SIMPLIFY_ONEOF_ANYOF", "true");
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
Expand All @@ -177,6 +189,9 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf() {

Schema schema8 = openAPI.getComponents().getSchemas().get("Parent");
assertEquals(((Schema) schema8.getProperties().get("number")).get$ref(), "#/components/schemas/Number");

Schema schema10 = openAPI.getComponents().getSchemas().get("AnyOfStringArrayOfString");
assertEquals(schema10.getAnyOf().size(), 2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,11 @@ public void testSimpleRefDecoding() {
Assert.assertEquals(decoded, "~1 Hallo/Welt");
}

// 3.1 spec test

// 3.0 spec tests
@Test
public void testIsMapSchema() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_1/schema.yaml");
public void test30Schemas() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/schema.yaml");
Schema misc = ModelUtils.getSchema(openAPI, "Misc");

// test map
Expand All @@ -308,5 +309,61 @@ public void testIsMapSchema() {
Assert.assertTrue(ModelUtils.isFreeFormObject((Schema) misc.getProperties().get("free_form_object_1")));
Assert.assertTrue(ModelUtils.isFreeFormObject((Schema) misc.getProperties().get("free_form_object_2")));
Assert.assertTrue(ModelUtils.isFreeFormObject((Schema) misc.getProperties().get("free_form_object_3")));

// test oneOf
Assert.assertTrue(ModelUtils.isOneOf((Schema) misc.getProperties().get("oneof1")));

// test anyOf model
Schema anyof1 = ModelUtils.getSchema(openAPI, "anyof1");
Assert.assertNotNull(anyof1);
Assert.assertNull(anyof1.getTypes());
Assert.assertNull(anyof1.getType());
Assert.assertTrue(ModelUtils.hasAnyOf(anyof1));
Assert.assertTrue(ModelUtils.isAnyOf(anyof1));

// test anyOf in properties
Schema anyof1Property = (Schema) misc.getProperties().get("anyof1");
Assert.assertNotNull(anyof1Property);
Assert.assertNull(anyof1Property.getTypes());
Assert.assertNull(anyof1Property.getType());
Assert.assertTrue(ModelUtils.hasAnyOf(anyof1Property));
Assert.assertTrue(ModelUtils.isAnyOf(anyof1Property));
}

// 3.1 spec tests
@Test
public void test31Schemas() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/schema.yaml");
Schema misc = ModelUtils.getSchema(openAPI, "Misc");

// test map
Assert.assertTrue(ModelUtils.isMapSchema((Schema) misc.getProperties().get("map1")));

// test free form object
Assert.assertTrue(ModelUtils.isFreeFormObject((Schema) misc.getProperties().get("free_form_object_1")));
Assert.assertTrue(ModelUtils.isFreeFormObject((Schema) misc.getProperties().get("free_form_object_2")));
Assert.assertTrue(ModelUtils.isFreeFormObject((Schema) misc.getProperties().get("free_form_object_3")));

// test oneOf property
Assert.assertTrue(ModelUtils.isOneOf((Schema) misc.getProperties().get("oneof1")));

// test anyOf property
Schema anyof1 = (Schema) misc.getProperties().get("anyof1");
Assert.assertNotNull(anyof1);
Assert.assertNull(anyof1.getTypes());
Assert.assertNull(anyof1.getType());
Assert.assertNotNull(anyof1.getAnyOf());
Assert.assertFalse(anyof1.getAnyOf().isEmpty());
Assert.assertTrue(ModelUtils.hasAnyOf(anyof1));
Assert.assertTrue(ModelUtils.isAnyOf(anyof1));

Schema anyof2 = (Schema) misc.getProperties().get("anyof2");
Assert.assertNotNull(anyof2);
Assert.assertNull(anyof2.getTypes());
Assert.assertNull(anyof2.getType());
Assert.assertNotNull(anyof2.getAnyOf());
Assert.assertFalse(anyof2.getAnyOf().isEmpty());
Assert.assertTrue(ModelUtils.hasAnyOf(anyof2));
Assert.assertTrue(ModelUtils.isAnyOf(anyof2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1811,6 +1828,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1942,6 +1959,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1834,6 +1851,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1794,6 +1811,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1912,6 +1929,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Loading

0 comments on commit 5de2365

Please sign in to comment.