[java] Support 'duration' format#23695
Conversation
There was a problem hiding this comment.
3 issues found across 7 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java:1465">
P2: Duration defaults are dropped for `java8-*` variants because this branch only accepts exact `java8` even though `duration` is mapped for all `java8*` libraries.</violation>
<violation number="2" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java:1561">
P2: Nested `duration` properties have the same exact-`java8` gate, so defaults are also dropped for `java8-*` variants.</violation>
</file>
<file name="samples/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/model/Pet.java">
<violation number="1" location="samples/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/model/Pet.java:50">
P2: Mapping OpenAPI `duration` to `java.time.Duration` rejects valid RFC3339 duration values with year/month/week components, so payloads like `P1Y` or `P2W` will not deserialize correctly.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| defaultPropertyExpression = String.format(Locale.ROOT, "java.time.LocalDateTime.parse(\"%s\")", value.asText()); | ||
| } | ||
| } else if(ModelUtils.isDurationSchema(propertySchema)) { | ||
| if("java8".equals(getDateLibrary())) { |
There was a problem hiding this comment.
P2: Nested duration properties have the same exact-java8 gate, so defaults are also dropped for java8-* variants.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java, line 1561:
<comment>Nested `duration` properties have the same exact-`java8` gate, so defaults are also dropped for `java8-*` variants.</comment>
<file context>
@@ -1548,6 +1557,10 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
defaultPropertyExpression = String.format(Locale.ROOT, "java.time.LocalDateTime.parse(\"%s\")", value.asText());
}
+ } else if(ModelUtils.isDurationSchema(propertySchema)) {
+ if("java8".equals(getDateLibrary())) {
+ defaultPropertyExpression = String.format(Locale.ROOT, "java.time.Duration.parse(\"%s\")", value.asText());
+ }
</file context>
| return null; | ||
| } else if (ModelUtils.isDurationSchema(schema)) { | ||
| if (schema.getDefault() != null) { | ||
| if ("java8".equals(getDateLibrary())) { |
There was a problem hiding this comment.
P2: Duration defaults are dropped for java8-* variants because this branch only accepts exact java8 even though duration is mapped for all java8* libraries.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java, line 1465:
<comment>Duration defaults are dropped for `java8-*` variants because this branch only accepts exact `java8` even though `duration` is mapped for all `java8*` libraries.</comment>
<file context>
@@ -1458,6 +1460,13 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
return null;
+ } else if (ModelUtils.isDurationSchema(schema)) {
+ if (schema.getDefault() != null) {
+ if ("java8".equals(getDateLibrary())) {
+ return String.format(Locale.ROOT, "Duration.parse(\"%s\")", schema.getDefault());
+ }
</file context>
|
|
||
| private LocalDateTime adoptionDate = LocalDateTime.parse("2007-12-03T10:15:30"); | ||
|
|
||
| private Duration tripDuration = Duration.parse("PT10H15M30S"); |
There was a problem hiding this comment.
P2: Mapping OpenAPI duration to java.time.Duration rejects valid RFC3339 duration values with year/month/week components, so payloads like P1Y or P2W will not deserialize correctly.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/model/Pet.java, line 50:
<comment>Mapping OpenAPI `duration` to `java.time.Duration` rejects valid RFC3339 duration values with year/month/week components, so payloads like `P1Y` or `P2W` will not deserialize correctly.</comment>
<file context>
@@ -46,6 +47,8 @@ public class Pet {
private LocalDateTime adoptionDate = LocalDateTime.parse("2007-12-03T10:15:30");
+ private Duration tripDuration = Duration.parse("PT10H15M30S");
+
public Pet() {
</file context>
|
the bot noticed that duration doesn't deserialize correctly #23695 (comment) closing this for now |
See https://spec.openapis.org/registry/format/duration.html
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)
Summary by cubic
Adds Java support for the OpenAPI
durationstring format (per https://spec.openapis.org/registry/format/duration.html). Properties withformat: durationnow generate asjava.time.Durationwith correct default handling.duration->Durationand importjava.time.Duration.Duration.parse(...)when using thejava8date library; otherwise keep the raw string default. Works for nested object defaults too.ModelUtils.isDurationSchema, tests, and Spring sample updates to cover generation and defaults.Written for commit 9e782e0. Summary will update on new commits.