GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Regression with java.time.Duration, LocalTime and OffsetTime (since 3.1.1) · Issue #3442 · springdoc/springdoc-openapi

Regression with java.time.Duration, LocalTime and OffsetTime (since 3.1.1) #3442

New issue
Copy link
New issue
Copy link
Closed
Closed
Regression with java.time.Duration, LocalTime and OffsetTime (since 3.1.1)#3442
Copy link

Description

@fleboulch
fleboulch
opened on Nov 16, 2026
Issue body actions

Describe the bug

Since 3.1.1 (#3351: java.time.Duration, LocalTime and OffsetTime are now resolved by swagger-core and carry a format), the example declared with @Schema(example = ...) on these types is no longer rendered
correctly:

  1. On model properties (request/response bodies, components.schemas), the example is dropped silently.
  2. On @ParameterObject fields, the string example is parsed as a Duration and re-serialized as a number of seconds (604800.000000000 instead of P7D), while the schema still says type: string.

Adding format: duration / format: time-local is the documented change and works as expected. Losing or changing the user-supplied example looks like a regression.

To Reproduce

Versions:

  • springdoc-openapi-starter-webmvc-api: 3.1.0 → 3.2.0 (also includes 3.1.1)
  • Spring Boot 4.1.2
  • Java 25
  • springdoc.api-docs.version=openapi_3_0

Sample code:

// Request body / response model                                                                                                                                                                                                 
public record Request(                                                                                                                                                                                                 
        @Schema(description = "Frequency", example = "PT2H")                                                                                                                                                    
        Duration frequency,                                                                                                                                                                                                      
                                                                                                                                                                                                                                 
        @Schema(description = "Times (UTC)", example = """                                                                                                                                                      
                ["00:00", "06:00"]                                                                                                                                                                                               
                """)                                                                                                                                                                                                             
        List<LocalTime> fixedTimesFrequency) {}     

// Query parameters                                                                                                                                                                                                              
@ParameterObject                                                                                                                                                                                                                 
public record DailyRequest(                                                                                                                                                                                                      
        @Schema(description = "Time window to look back on, as an ISO-8601 duration",                                                                                                                                            
                example = "P7D",                                                                                                                                                                                                 
                requiredMode = Schema.RequiredMode.REQUIRED)                                                                                                                                                                     
        @NotNull                                                                                                                                                                                                                 
        Duration range) {}                                                                                                                                                                                                       
                                                                                                                                                                                                                                 
@RestController                                                                                                                                                                                                                  
class DemoController {                                                                                                                                                                                                           
    @PostMapping("/foo")                                                                                                                                                                                                 
    void create(@RequestBody Request request) {}                                                                                                                                                                       
                                                                                                                                                                                                                                 
    @GetMapping("/daily")                                                                                                                                                                                                        
    void daily(DailyRequest request) {}                                                                                                                                                                                          
}                                                                                                                                                                                                                                

Actual behavior

Diff of the generated spec between 3.1.0 and 3.2.0:

Model properties: example dropped

         frequency:                                                                                                                                                                                                              
           type: string                                                                                                                                                                                                          
           description: "Frequency"                                                                                                                                                                             
-          example: PT2H                                                                                                                                                                                                         
+          format: duration                                                                                                                                                                                                      
         fixedTimesFrequency:                                                                                                                                                                                                    
           type: array                                                                                                                                                                                                           
           items:                                                                                                                                                                                                                
             type: string                                                                                                                                                                                                        
-            example: "[\"00:00\",\"06:00\"]"                                                                                                                                                                                    
+            format: time-local                                                                                                                                                                                                  

The same happens on response models (e.g. a Duration duration field annotated with example = "PT2H7M26S").

@ParameterObject field: example turned into a number

       - name: range                                                                                                                                                                                                             
         in: query                                                                                                                                                                                                               
         required: true                                                                                                                                                                                                          
         schema:                                                                                                                                                                                                                 
           type: string                                                                                                                                                                                                          
           description: "Time window to look back on, as an ISO-8601 duration"                                                                                                                                                   
-          example: P7D                                                                                                                                                                                                          
+          format: duration                                                                                                                                                                                                      
+          example: 604800.000000000                                                                                                                                                                                             
         example: P7D                                                                                                                                                                                                            

Duration path variables without an example only get the new format: duration, which is fine.

Expected behavior

The example given in @Schema(example = ...) is kept as-is, as it was in 3.1.0:

  frequency:                                                                                                                                                                                                                       
    type: string                                                                                                                                                                                                                   
    format: duration                                                                                                                                                                                                               
    example: PT2H                                                                                                                                                                                                                  
schema:                                                                                                                                                                                                                          
  type: string                                                                                                                                                                                                                   
  format: duration                                                                                                                                                                                                               
  example: P7D                                                                                                                                                                                                                   

Additional context

  • Probably introduced by upgrade swagger-core from 2.2.53 to 2.2.54 #3351 (3.1.1) together with the swagger-core upgrade to 2.2.55. I have not checked whether the example is lost in springdoc or in swagger-core's ModelResolver, so it may need to be moved to
    swagger-core.

Activity

  1. rajeshvasa commented on Nov 17, 2026

    @rajeshvasa
    rajeshvasa
    on Nov 17, 2026
    More actions

    I'd like to work on this. Since #3351 these types are resolved by swagger-core with a format, and the user-supplied example from @Schema(example = ...) is then dropped on model properties and converted to seconds on @ParameterObject fields. I'll look at where springdoc applies the example for these types, keep the declared example as-is (e.g. P7D) alongside the new format, and add tests for model properties and @ParameterObject fields for Duration, LocalTime and OffsetTime. Happy to adjust if you'd prefer a different approach.

  2. rajeshvasa commented on Nov 17, 2026

    @rajeshvasa
    rajeshvasa
    on Nov 17, 2026
    More actions

    Initial findings from reading the code (not yet reproduced): the conversion seems to happen in swagger-core's new schema classes (DurationSchema, TimeLocalSchema, TimeSchema), whose cast() turns the string example into a java.time object. ObjectMapperFactory disables WRITE_DATES_AS_TIMESTAMPS but not WRITE_DURATIONS_AS_TIMESTAMPS, which would explain P7D being written as 604800.000000000. I still need to confirm why the example is dropped on model properties. If it is confirmed, the root fix probably belongs in swagger-core (keeping the declared example as a string for these types). I'm reproducing it locally now and will follow up with a test.

  3. rajeshvasa commented on Nov 17, 2026

    @rajeshvasa
    rajeshvasa
    on Nov 17, 2026
    More actions

    Follow-up after reproducing it on current main (swagger-core 3.0.0), with the exact records from the issue:

    • OpenAPI 3.1: all examples are kept as declared (model properties and @ParameterObject fields), in JSON and YAML.
    • OpenAPI 3.0: model properties keep their examples, but a Duration example on a @ParameterObject field is rewritten: P7D becomes PT168H.
    • The "dropped" examples and the 604800.000000000 value from 3.2.0 no longer reproduce on main, so those look fixed by the swagger-core 3.0.0 upgrade.

    Correcting my earlier guess: the remaining problem is not WRITE_DURATIONS_AS_TIMESTAMPS. For 3.0 the parameter schema is a swagger-core DurationSchema, and Schema.setExample() calls DurationSchema.cast(), which parses the string into a java.time.Duration. A Duration has no day unit, so P7D is written back as PT168H. In 3.1 the schema is a plain JsonSchema without cast(), so the example stays as declared.

    The clean fix seems to be in swagger-core: keep a string example as declared in DurationSchema (and TimeLocalSchema / TimeSchema), instead of normalizing it. I can open that PR in swagger-core and then add a springdoc regression test once it's released. Would that work for you, or would you prefer a springdoc-side workaround as well?

  4. fleboulch commented on Nov 17, 2026

    @fleboulch
    fleboulch
    on Nov 17, 2026
    Author
    More actions

    Thanks for the feedback! Can we prepare a new release including the bump of swagger-core to 3.0.0 as it's already on the master branch?

  5. rajeshvasa commented on Nov 18, 2026

    @rajeshvasa
    rajeshvasa
    on Nov 18, 2026
    More actions

    @bnasslahsen, a release is up to you; I'm not a maintainer. On what a release from main would cover: the dropped examples and the 604800.000000000 values are gone there. One case remains with OpenAPI 3.0: a Duration example on a @ParameterObject field is still normalized (P7D → PT168H) by swagger-core's DurationSchema. I'm happy to work on that in swagger-core, so it may be worth keeping this issue open to track it.

  6. bnasslahsen commented on Nov 19, 2026

    @bnasslahsen
    bnasslahsen
    on Nov 19, 2026
    Collaborator
    More actions

    Thanks for the precise report. I reproduced it with your records.

    swagger-core 3.0.0 is not in 3.2.0 yet: 3.2.0 still ships swagger-core 2.2.55. The upgrade is on main and will be in the next release, 3.2.1, which fixes the dropped and converted examples you reported:

    • The examples are kept as declared on model properties and @ParameterObject fields, for Duration, LocalTime and OffsetTime. The List<LocalTime> example is set on the array as ["00:00", "06:00"].
    • One difference remains with springdoc.api-docs.version=openapi_3_0: the schema example of a Duration @ParameterObject field is normalized to PT168H, an equivalent value. The parameter example itself stays P7D. This comes from swagger-core's DurationSchema, which parses string examples into a java.time.Duration, so the fix belongs in swagger-core.
    • With OpenAPI 3.1 (the default) everything is kept as declared.

    3.2.1 will be released just after Spring Boot 4.2.0, which also brings fixes for some CVEs: http://calendar.spring.io/

    @rajeshvasa, a swagger-core PR keeping the declared string example in DurationSchema is welcome; we'll add a regression test once it's released.

  7. bnasslahsen
    closed this as completedon Nov 19, 2026
  8. rajeshvasa commented on Nov 20, 2026

    @rajeshvasa
    rajeshvasa
    on Nov 20, 2026
    More actions

    Thanks @bnasslahsen! Opened swagger-api/swagger-core#5420: it keeps the declared string example in DurationSchema, and also in TimeSchema and TimeLocalSchema, which had the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.