Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CodeGen correctly produces yyyy-mm-dd format for date with examples #20516

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -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"));
}
}