Skip to content

Commit

Permalink
Fix for #18735 optional collections are initialized
Browse files Browse the repository at this point in the history
check required collections are not null
update samples and tests
  • Loading branch information
Slawomir Messner authored and mosesonline committed Jun 17, 2024
1 parent 18b4bcb commit e8c9139
Show file tree
Hide file tree
Showing 412 changed files with 942 additions and 834 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1257,8 +1257,8 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
if (ModelUtils.isArraySchema(schema)) {
if (schema.getDefault() == null) {
// nullable or containerDefaultToNull set to true
if (cp.isNullable || containerDefaultToNull) {
// nullable or optional or containerDefaultToNull set to true
if (cp.isNullable || !cp.required || containerDefaultToNull) {
return null;
}
return getDefaultCollectionType(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ public class AbstractJavaCodegenTest {
"3_0/mapSchemas", TestUtils.parseFlattenSpec("src/test/resources/3_0/mapSchemas.yaml"),
"3_0/spring/date-time-parameter-types-for-testing", TestUtils.parseFlattenSpec("src/test/resources/3_0/spring/date-time-parameter-types-for-testing.yml")
);

private AbstractJavaCodegen codegen;

/**
* In TEST-NG, test class (and its fields) is only constructed once (vs. for every test in Jupiter),
* using @BeforeMethod to have a fresh codegen mock for each test
*/
@BeforeMethod void mockAbstractCodegen() {
codegen = Mockito.mock(
AbstractJavaCodegen.class, Mockito.withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS).useConstructor()
);
);
}

@Test
Expand Down Expand Up @@ -124,7 +124,7 @@ public void testPreprocessOpenApiIncludeAllMediaTypesInAcceptHeader() {
@Test
public void testPreprocessOpenAPINumVersion() {
final OpenAPI openAPIOtherNumVersion = TestUtils.parseFlattenSpec("src/test/resources/2_0/duplicateOperationIds.yaml");

codegen.preprocessOpenAPI(openAPIOtherNumVersion);

Assert.assertEquals(codegen.getArtifactVersion(), openAPIOtherNumVersion.getInfo().getVersion());
Expand Down Expand Up @@ -156,7 +156,7 @@ public void convertVarNameWithCaml() {
Assert.assertEquals(codegen.toVarName("$name"), "$Name");
Assert.assertEquals(codegen.toVarName("1AAaa"), "_1AAaa");
}

@Test
public void convertModelName() {
Assert.assertEquals(codegen.toModelName("name"), "Name");
Expand Down Expand Up @@ -399,7 +399,7 @@ public void defaultVersionTest() {
codegen.setArtifactVersion(null);
OpenAPI api = TestUtils.createOpenAPI();
api.getInfo().setVersion(null);

codegen.processOpts();
codegen.preprocessOpenAPI(api);

Expand All @@ -412,7 +412,7 @@ public void snapshotVersionTest() {
codegen.additionalProperties().put("snapshotVersion", "true");
OpenAPI api = TestUtils.createOpenAPI();
api.getInfo().setVersion(null);

codegen.processOpts();
codegen.preprocessOpenAPI(api);

Expand All @@ -425,7 +425,7 @@ public void snapshotVersionOpenAPITest() {
codegen.additionalProperties().put(CodegenConstants.SNAPSHOT_VERSION, "true");
OpenAPI api = TestUtils.createOpenAPI();
api.getInfo().setVersion("2.0");

codegen.processOpts();
codegen.preprocessOpenAPI(api);

Expand Down Expand Up @@ -484,7 +484,7 @@ public void snapshotVersionAlreadySnapshotTest() {

codegen.processOpts();
codegen.preprocessOpenAPI(TestUtils.createOpenAPI());

Assert.assertEquals(codegen.getArtifactVersion(), "4.1.2-SNAPSHOT");
}

Expand Down Expand Up @@ -537,11 +537,11 @@ public void toDefaultValueTest() {

ModelUtils.setGenerateAliasAsModel(false);
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", schema), schema);
Assert.assertEquals(defaultValue, "new ArrayList<>()");
Assert.assertNull(defaultValue);

ModelUtils.setGenerateAliasAsModel(true);
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", schema), schema);
Assert.assertEquals(defaultValue, "new ArrayList<>()");
Assert.assertNull(defaultValue);

// Create a map schema with additionalProperties type set to array alias
schema = new MapSchema().additionalProperties(new Schema<>().$ref("#/components/schemas/NestedArray"));
Expand Down Expand Up @@ -623,7 +623,7 @@ public void getTypeDeclarationGivenSchemaMappingTest() {
codegen.schemaMapping().put("MyStringType", "com.example.foo");
codegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas("MyStringType", new StringSchema())));
Schema<?> schema = new ArraySchema().items(new Schema<>().$ref("#/components/schemas/MyStringType"));

String defaultValue = codegen.getTypeDeclaration(schema);

Assert.assertEquals(defaultValue, "List<com.example.foo>");
Expand Down Expand Up @@ -678,16 +678,16 @@ public void getTypeDeclarationTest() {
@Test
public void processOptsBooleanTrueFromString() {
codegen.additionalProperties().put(CodegenConstants.SNAPSHOT_VERSION, "true");

codegen.preprocessOpenAPI(FLATTENED_SPEC.get("3_0/petstore"));

Assert.assertTrue((boolean) codegen.additionalProperties().get(CodegenConstants.SNAPSHOT_VERSION));
}

@Test
public void processOptsBooleanTrueFromBoolean() {
codegen.additionalProperties().put(CodegenConstants.SNAPSHOT_VERSION, true);

codegen.preprocessOpenAPI(FLATTENED_SPEC.get("3_0/petstore"));

Assert.assertTrue((boolean) codegen.additionalProperties().get(CodegenConstants.SNAPSHOT_VERSION));
Expand All @@ -696,7 +696,7 @@ public void processOptsBooleanTrueFromBoolean() {
@Test
public void processOptsBooleanFalseFromString() {
codegen.additionalProperties().put(CodegenConstants.SNAPSHOT_VERSION, "false");

codegen.preprocessOpenAPI(FLATTENED_SPEC.get("3_0/petstore"));

Assert.assertFalse((boolean) codegen.additionalProperties().get(CodegenConstants.SNAPSHOT_VERSION));
Expand All @@ -705,16 +705,16 @@ public void processOptsBooleanFalseFromString() {
@Test
public void processOptsBooleanFalseFromBoolean() {
codegen.additionalProperties().put(CodegenConstants.SNAPSHOT_VERSION, false);

codegen.preprocessOpenAPI(FLATTENED_SPEC.get("3_0/petstore"));

Assert.assertFalse((boolean) codegen.additionalProperties().get(CodegenConstants.SNAPSHOT_VERSION));
}

@Test
public void processOptsBooleanFalseFromGarbage() {
codegen.additionalProperties().put(CodegenConstants.SNAPSHOT_VERSION, "blibb");

codegen.preprocessOpenAPI(FLATTENED_SPEC.get("3_0/petstore"));

Assert.assertFalse((boolean) codegen.additionalProperties().get(CodegenConstants.SNAPSHOT_VERSION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void converterInArrayTest() {
Assert.assertEquals(enumVar.dataType, "List<String>");
Assert.assertEquals(enumVar.datatypeWithEnum, "List<NameEnum>");
Assert.assertEquals(enumVar.name, "name");
Assert.assertEquals(enumVar.defaultValue, "new ArrayList<>()");
Assert.assertEquals(enumVar.defaultValue, null);
Assert.assertEquals(enumVar.baseType, "List");
Assert.assertTrue(enumVar.isEnum);

Expand Down Expand Up @@ -108,7 +108,7 @@ public void converterInArrayInArrayTest() {
Assert.assertEquals(enumVar.dataType, "List<List<String>>");
Assert.assertEquals(enumVar.datatypeWithEnum, "List<List<NameEnum>>");
Assert.assertEquals(enumVar.name, "name");
Assert.assertEquals(enumVar.defaultValue, "new ArrayList<>()");
Assert.assertEquals(enumVar.defaultValue, null);
Assert.assertEquals(enumVar.baseType, "List");
Assert.assertTrue(enumVar.isEnum);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void listPropertyTest() {
Assert.assertEquals(property.setter, "setUrls");
Assert.assertEquals(property.dataType, "List<String>");
Assert.assertEquals(property.name, "urls");
Assert.assertEquals(property.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.baseType, "List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -164,7 +164,7 @@ public void setPropertyTest() {
Assert.assertEquals(property.setter, "setUrls");
Assert.assertEquals(property.dataType, "Set<String>");
Assert.assertEquals(property.name, "urls");
Assert.assertEquals(property.defaultValue, "new LinkedHashSet<>()");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.baseType, "Set");
Assert.assertEquals(property.containerType, "set");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -250,7 +250,7 @@ public void list2DPropertyTest() {
Assert.assertEquals(property.setter, "setList2D");
Assert.assertEquals(property.dataType, "List<List<Pet>>");
Assert.assertEquals(property.name, "list2D");
Assert.assertEquals(property.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.baseType, "List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -335,7 +335,7 @@ public void complexListPropertyTest() {
Assert.assertEquals(property.setter, "setChildren");
Assert.assertEquals(property.dataType, "List<Children>");
Assert.assertEquals(property.name, "children");
Assert.assertEquals(property.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.baseType, "List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -398,7 +398,7 @@ public void complexArrayPropertyTest() {
Assert.assertEquals(property.setter, "setChildren");
Assert.assertEquals(property.dataType, "List<Children>");
Assert.assertEquals(property.name, "children");
Assert.assertEquals(property.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.baseType, "List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -431,7 +431,7 @@ public void complexSetPropertyTest() {
Assert.assertEquals(property.setter, "setChildren");
Assert.assertEquals(property.dataType, "Set<Children>");
Assert.assertEquals(property.name, "children");
Assert.assertEquals(property.defaultValue, "new LinkedHashSet<>()");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.baseType, "Set");
Assert.assertEquals(property.containerType, "set");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -468,7 +468,7 @@ public void arrayModelWithItemNameTest() {
Assert.assertEquals(property.setter, "setChildren");
Assert.assertEquals(property.dataType, "List<Child>");
Assert.assertEquals(property.name, "children");
Assert.assertEquals(property.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.baseType, "List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -976,7 +976,7 @@ public void modelWithWrappedXmlTest() {
Assert.assertEquals(property2.setter, "setArray");
Assert.assertEquals(property2.dataType, "List<String>");
Assert.assertEquals(property2.name, "array");
Assert.assertEquals(property2.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property2.defaultValue, null);
Assert.assertEquals(property2.baseType, "List");
Assert.assertTrue(property2.isContainer);
Assert.assertTrue(property2.isXmlWrapped);
Expand Down Expand Up @@ -1131,14 +1131,14 @@ public void stringPropertyTest() {
Assert.assertEquals(cp.maxLength, Integer.valueOf(10));
Assert.assertEquals(cp.pattern, "^[A-Z]+$");
}

@Test(description = "convert string property with password format")
public void stringPropertyPasswordFormatTest() {
OpenAPI openAPI = TestUtils.createOpenAPI();
final Schema property = new StringSchema().format("password");
final DefaultCodegen codegen = new JavaClientCodegen();
codegen.setOpenAPI(openAPI);

final CodegenProperty cp = codegen.fromProperty("somePropertyWithPasswordFormat", property);
Assert.assertEquals(cp.isPassword, true);
}
Expand Down
Loading

0 comments on commit e8c9139

Please sign in to comment.