Skip to content

[java] Support 'duration' format#23695

Closed
Allsimon wants to merge 1 commit intoOpenAPITools:masterfrom
Allsimon:duration
Closed

[java] Support 'duration' format#23695
Allsimon wants to merge 1 commit intoOpenAPITools:masterfrom
Allsimon:duration

Conversation

@Allsimon
Copy link
Copy Markdown
Contributor

@Allsimon Allsimon commented May 5, 2026

See https://spec.openapis.org/registry/format/duration.html

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@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 duration string format (per https://spec.openapis.org/registry/format/duration.html). Properties with format: duration now generate as java.time.Duration with correct default handling.

  • New Features
    • Map duration -> Duration and import java.time.Duration.
    • Parse defaults with Duration.parse(...) when using the java8 date library; otherwise keep the raw string default. Works for nested object defaults too.
    • Added ModelUtils.isDurationSchema, tests, and Spring sample updates to cover generation and defaults.

Written for commit 9e782e0. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

@Allsimon
Copy link
Copy Markdown
Contributor Author

Allsimon commented May 5, 2026

the bot noticed that duration doesn't deserialize correctly #23695 (comment)

closing this for now

@Allsimon Allsimon closed this May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant