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 @@ -510,6 +510,17 @@ public void processOpts() {
typeMapping.put("DateTime", "OffsetDateTime");
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
}
} else if ("java11".equals(dateLibrary)) {
setJava11Mode(true);
additionalProperties.put(JAVA11_MODE, java11Mode);
additionalProperties.put("jsr310", "true");
additionalProperties.put("java8", true);

typeMapping.put("date", "LocalDate");
typeMapping.put("DateTime", "OffsetDateTime");

importMapping.put("LocalDate", "java.time.LocalDate");
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
} else if (dateLibrary.equals("legacy")) {
additionalProperties.put("legacyDates", true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ public void processOpts() {
if (additionalProperties.get(DATE_LIBRARY).toString().startsWith("java8")) {
this.setJava8(true);
}
else if ("java11".equals(additionalProperties.get(DATE_LIBRARY).toString())) {
this.setJava11(true);
}
}
if (this.java8) {
additionalProperties.put("javaVersion", "1.8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ public void testPackageNamesSetWithAdditionalProperties() throws Exception {
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG), Boolean.TRUE);
}

@Test
public void testJava11DateLibraryMappings() {
final AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();

codegen.additionalProperties().put("dateLibrary", "java11");

codegen.processOpts();

Assert.assertEquals(codegen.typeMapping().get("date"), "LocalDate");
Assert.assertEquals(codegen.typeMapping().get("DateTime"), "OffsetDateTime");

Assert.assertEquals(codegen.importMapping().get("LocalDate"), "java.time.LocalDate");
Assert.assertEquals(codegen.importMapping().get("OffsetDateTime"), "java.time.OffsetDateTime");

Assert.assertTrue(codegen.java11Mode);
Assert.assertEquals(codegen.additionalProperties().get("jsr310"), "true");
Assert.assertEquals(codegen.additionalProperties().get("java11"), Boolean.TRUE);
}

@Test
public void testFixUpParentAndInterfaces_propertyNameDifferent_getterSetterSame_typeDifferent() {
AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
Expand Down Expand Up @@ -288,7 +307,7 @@ public void testFixUpParentAndInterfaces_propertyNameSame_getterSetterSame_typeD
Assert.assertEquals(childModel.vars.get(1).getter, childValueProperty2.getter);
Assert.assertEquals(childModel.vars.get(1).setter, childValueProperty2.setter);
}

/**
* Issue #1066 - testing case when the conflicting property is actually not the first one but the
* second
Expand Down Expand Up @@ -355,7 +374,7 @@ public void testFixUpParentAndInterfaces_2ndproperty_propertyNameSame_getterSett
Assert.assertEquals(childModel.vars.get(0).nameInCamelCase, childValueProperty1.nameInCamelCase);
Assert.assertEquals(childModel.vars.get(0).getter, childValueProperty1.getter);
Assert.assertEquals(childModel.vars.get(0).setter, childValueProperty1.setter);
}
}

public static class P_AbstractJavaCodegen extends AbstractJavaCodegen {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public void testPackageNamesSetWithAdditionalProperties() throws Exception {
Assert.assertEquals(codegen.invokerPackage, "xxx.yyyyy.iiii.invoker");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xxx.yyyyy.iiii.invoker");
}
@Test
public void testJava11DateLibraryEnablesJakarta() {
final AbstractJavaJAXRSServerCodegen codegen =
new P_AbstractJavaJAXRSServerCodegen();

codegen.additionalProperties().put("dateLibrary", "java11");

codegen.processOpts();

Assert.assertTrue(codegen.java11Mode);
Assert.assertEquals(
codegen.additionalProperties().get("jakarta"),
Boolean.TRUE
);
}

private static class P_AbstractJavaJAXRSServerCodegen extends AbstractJavaJAXRSServerCodegen {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,32 @@ public void testMapOfInnerEnum() {
Assert.assertEquals(employeeWithMultiMapOfEnum.vars.get(0).datatypeWithEnum, "Map<String, List<InnerEnum>>");

}
@Test
public void testJava11DateLibrarySchemaMappings() {
final JavaClientCodegen codegen = new JavaClientCodegen();
codegen.additionalProperties().put("dateLibrary", "java11");
codegen.processOpts();

final OpenAPI openAPI = getOpenAPI("3_0_0/java11_date_library.yaml");
final CodegenWrapper codegenWrapper = processSchemas(codegen, openAPI);

final CodegenModel model = codegenWrapper.getAllModels().get("DateModel");
Assert.assertNotNull(model);

final CodegenProperty birthDate = model.getVars().stream()
.filter(property -> "birthDate".equals(property.name))
.findFirst()
.orElse(null);

final CodegenProperty createdAt = model.getVars().stream()
.filter(property -> "createdAt".equals(property.name))
.findFirst()
.orElse(null);

Assert.assertNotNull(birthDate);
Assert.assertNotNull(createdAt);

Assert.assertEquals(birthDate.datatype, "LocalDate");
Assert.assertEquals(createdAt.datatype, "OffsetDateTime");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.swagger.codegen.v3.generators.java;

import org.testng.Assert;
import org.testng.annotations.Test;

public class SpringCodegenTest {

@Test
public void testJava11DateLibraryEnablesJava11Mode() {
SpringCodegen codegen = new SpringCodegen();

codegen.additionalProperties().put("dateLibrary", "java11");

codegen.processOpts();

Assert.assertTrue(codegen.java11);
Assert.assertEquals(codegen.additionalProperties().get("javaVersion"), "11");
Assert.assertEquals(codegen.additionalProperties().get("jdk11"), "true");
Assert.assertEquals(codegen.additionalProperties().get("isJava8or11"), true);
Assert.assertTrue(codegen.defaultInterfaces);
}
}
16 changes: 16 additions & 0 deletions src/test/resources/3_0_0/java11_date_library.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
openapi: 3.0.3
info:
title: Java 11 Date Library Test
version: 1.0.0
paths: {}
components:
schemas:
DateModel:
type: object
properties:
birthDate:
type: string
format: date
createdAt:
type: string
format: date-time