File tree Expand file tree Collapse file tree 2 files changed +12
-0
lines changed
modules/openapi-generator/src
main/java/org/openapitools/codegen/languages
test/java/org/openapitools/codegen/java Expand file tree Collapse file tree 2 files changed +12
-0
lines changed Original file line number Diff line number Diff line change 31
31
import io .swagger .v3 .oas .models .parameters .RequestBody ;
32
32
import io .swagger .v3 .oas .models .servers .Server ;
33
33
import io .swagger .v3 .parser .util .SchemaTypeUtil ;
34
+ import java .text .SimpleDateFormat ;
34
35
import lombok .Getter ;
35
36
import lombok .Setter ;
36
37
import org .apache .commons .io .FilenameUtils ;
@@ -71,6 +72,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
71
72
72
73
private final Logger LOGGER = LoggerFactory .getLogger (AbstractJavaCodegen .class );
73
74
private static final String ARTIFACT_VERSION_DEFAULT_VALUE = "1.0.0" ;
75
+ private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat ("yyyy-MM-dd" , Locale .ROOT );
74
76
75
77
public static final String DEFAULT_LIBRARY = "<default>" ;
76
78
public static final String DATE_LIBRARY = "dateLibrary" ;
@@ -1638,6 +1640,9 @@ public void setParameterExampleValue(CodegenParameter p) {
1638
1640
@ Override
1639
1641
public String toExampleValue (Schema p ) {
1640
1642
if (p .getExample () != null ) {
1643
+ if (p .getExample () instanceof Date ) {
1644
+ return DATE_FORMAT .format (p .getExample ());
1645
+ }
1641
1646
return escapeText (p .getExample ().toString ());
1642
1647
} else {
1643
1648
return null ;
Original file line number Diff line number Diff line change @@ -965,4 +965,11 @@ public void removeAnnotationsTest() {
965
965
Assert .assertEquals (codegen .removeAnnotations ("List<@Valid Pet>" ), "List<Pet>" );
966
966
}
967
967
968
+ @ Test (description = "test generated example values for string properties" )
969
+ public void testGeneratedExampleValues () {
970
+ final OpenAPI openAPI = FLATTENED_SPEC .get ("3_0/spring/date-time-parameter-types-for-testing" );
971
+ codegen .setOpenAPI (openAPI );
972
+ DateSchema dateSchema = (DateSchema ) openAPI .getPaths ().get ("/thingy/{date}" ).getPost ().getParameters ().get (0 ).getSchema ();
973
+ Assert .assertTrue (codegen .escapeQuotationMark (codegen .toExampleValue (dateSchema )).matches ("2021-01-01" ));
974
+ }
968
975
}
You can’t perform that action at this time.
0 commit comments