Skip to content
Merged
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 @@ -809,6 +809,13 @@ public String toDefaultValue(Schema schema) {

return String.format(Locale.ROOT, pattern, typeDeclaration);
} else if (ModelUtils.isMapSchema(schema) && !(schema instanceof ComposedSchema)) {
if (schema.getProperties() != null && schema.getProperties().size() > 0) {
// object is complex object with free-form additional properties
if (schema.getDefault() != null) {
return super.toDefaultValue(schema);
}
return null;
}
final String pattern;
if (fullJavaUtil) {
pattern = "new java.util.HashMap<%s>()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.time.format.DateTimeFormatter;
import java.util.Date;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.AbstractJavaCodegen;
Expand Down Expand Up @@ -524,6 +525,55 @@ public void processOptsBooleanFalseFromNumeric() {
Assert.assertFalse((boolean) codegen.additionalProperties().get(CodegenConstants.SNAPSHOT_VERSION));
}

@Test
public void nullDefaultValueForModelWithDynamicProperties() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/mapSchemas.yaml");
codegen.additionalProperties().put(CodegenConstants.GENERATE_ALIAS_AS_MODEL, true);
codegen.setOpenAPI(openAPI);

Schema schema = openAPI.getComponents().getSchemas().get("ModelWithAdditionalProperties");
CodegenModel cm = codegen.fromModel("ModelWithAdditionalProperties", schema);
Assert.assertEquals(cm.vars.size(), 1, "Expected single declared var");
Assert.assertEquals(cm.vars.get(0).name, "id");
Assert.assertNull(cm.defaultValue, "Expected no defined default value in spec");

String defaultValue = codegen.toDefaultValue(schema);
Assert.assertNull(defaultValue);
}

@Test
public void maplikeDefaultValueForModelWithStringToStringMapping() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/mapSchemas.yaml");
codegen.additionalProperties().put(CodegenConstants.GENERATE_ALIAS_AS_MODEL, true);
codegen.setOpenAPI(openAPI);

Schema schema = openAPI.getComponents().getSchemas().get("ModelWithStringToStringMapping");
CodegenModel cm = codegen.fromModel("ModelWithAdditionalProperties", schema);
Assert.assertEquals(cm.vars.size(), 0, "Expected no declared vars");
Assert.assertNull(cm.defaultValue, "Expected no defined default value in spec");

String defaultValue = codegen.toDefaultValue(schema);
Assert.assertEquals(defaultValue, "new HashMap<String, String>()", "Expected string-string map aliased model to default to new HashMap<String, String>()");
}

@Test
public void maplikeDefaultValueForModelWithStringToModelMapping() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/mapSchemas.yaml");
codegen.additionalProperties().put(CodegenConstants.GENERATE_ALIAS_AS_MODEL, true);
codegen.setOpenAPI(openAPI);

Schema schema = openAPI.getComponents().getSchemas().get("ModelWithStringToModelMapping");
CodegenModel cm = codegen.fromModel("ModelWithStringToModelMapping", schema);
Assert.assertEquals(cm.vars.size(), 0, "Expected no declared vars");
Assert.assertNull(cm.defaultValue, "Expected no defined default value in spec");

String defaultValue = codegen.toDefaultValue(schema);
Assert.assertEquals(defaultValue, "new HashMap<String, ComplexModel>()", "Expected string-ref map aliased model to default to new HashMap<String, ComplexModel>()");
}

private static Schema<?> createObjectSchemaWithMinItems() {
return new ObjectSchema()
.addProperties("id", new IntegerSchema().format("int32"))
Expand Down
71 changes: 71 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/mapSchemas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
openapi: "3.0.0"
info:
title: Example API
version: "3.0.0"
paths:
/:
get:
summary: Empty Route
responses:
"200":
description: Good Request.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/FreeformObjectTypes'
"400":
description: Bad Request.
"403":
description: Forbidden. Access denied.

components:
schemas:
ExampleModel:
type: object
properties:
id:
type: string
withAdditinalProperties:
$ref: '#/components/schemas/ModelWithAdditionalProperties'
ModelWithAdditionalProperties:
type: object
properties:
id:
type: string
additionalProperties: {}
ModelWithStringToStringMapping:
type: object
additionalProperties:
type: string
ModelWithStringToModelMapping:
type: object
additionalProperties:
$ref: '#/components/schemas/ComplexModel'
ComplexModel:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
required:
- name
example:
name: Puma
id: 1
FreeformObjectTypes:
type: object
properties:
ExampleModel:
$ref: '#/components/schemas/ExampleModel'
ModelWithAdditionalProperties:
$ref: '#/components/schemas/ModelWithAdditionalProperties'
ModelWithStringToStringMapping:
$ref: '#/components/schemas/ModelWithStringToStringMapping'
ModelWithStringToModelMapping:
$ref: '#/components/schemas/ModelWithStringToModelMapping'
ComplexModel:
$ref: '#/components/schemas/ComplexModel'