Skip to content

Commit

Permalink
format date in codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Zemke committed Jan 21, 2025
1 parent c75fbb3 commit 0c87116
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import java.text.SimpleDateFormat;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -71,6 +72,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code

private final Logger LOGGER = LoggerFactory.getLogger(AbstractJavaCodegen.class);
private static final String ARTIFACT_VERSION_DEFAULT_VALUE = "1.0.0";
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);

public static final String DEFAULT_LIBRARY = "<default>";
public static final String DATE_LIBRARY = "dateLibrary";
Expand Down Expand Up @@ -1638,6 +1640,9 @@ public void setParameterExampleValue(CodegenParameter p) {
@Override
public String toExampleValue(Schema p) {
if (p.getExample() != null) {
if (p.getExample() instanceof Date) {
return DATE_FORMAT.format(p.getExample());
}
return escapeText(p.getExample().toString());
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,4 +965,11 @@ public void removeAnnotationsTest() {
Assert.assertEquals(codegen.removeAnnotations("List<@Valid Pet>"), "List<Pet>");
}

@Test(description = "test generated example values for string properties")
public void testGeneratedExampleValues() {
final OpenAPI openAPI = FLATTENED_SPEC.get("3_0/spring/date-time-parameter-types-for-testing");
codegen.setOpenAPI(openAPI);
DateSchema dateSchema = (DateSchema) openAPI.getPaths().get("/thingy/{date}").getPost().getParameters().get(0).getSchema();
Assert.assertTrue(codegen.escapeQuotationMark(codegen.toExampleValue(dateSchema)).matches("2021-01-01"));
}
}

0 comments on commit 0c87116

Please sign in to comment.