Skip to content

Map format: duration to the runtime's RFC 3339 Duration type#2458

Merged
mromaszewicz merged 1 commit into
mainfrom
fix/issue-2456
Jul 13, 2026
Merged

Map format: duration to the runtime's RFC 3339 Duration type#2458
mromaszewicz merged 1 commit into
mainfrom
fix/issue-2456

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Closes: #2456

A type: string, format: duration schema previously fell through to a plain Go string, leaving parsing and validation of the OpenAPI "duration" format entirely to the user. The default type mapping now resolves it to openapi_types.Duration, the RFC 3339 duration type added to the runtime in v1.5.0, alongside the other openapi_types formats (date, uuid, email, binary).

This changes generated types for specs already using format: duration. The old behavior can be restored by explicitly mapping the format back to a plain string in the configuration file:

output-options:
  type-mapping:
    string:
      formats:
        duration:
          type: string

Closes: #2456

A `type: string, format: duration` schema previously fell through to a
plain Go string, leaving parsing and validation of the OpenAPI
"duration" format entirely to the user. The default type mapping now
resolves it to openapi_types.Duration, the RFC 3339 duration type added
to the runtime in v1.5.0, alongside the other openapi_types formats
(date, uuid, email, binary).

This changes generated types for specs already using format: duration.
The old behavior can be restored by explicitly mapping the format back
to a plain string in the configuration file:

    output-options:
      type-mapping:
        string:
          formats:
            duration:
              type: string

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner July 13, 2026 00:05
@mromaszewicz mromaszewicz added enhancement New feature or request notable changes Used for release notes to highlight these more highly labels Jul 13, 2026
@greptile-apps

greptile-apps Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Maps the OpenAPI format: duration string format to openapi_types.Duration (from github.com/oapi-codegen/runtime/types v1.5.0) in the default type mapping, alongside the existing mappings for date, uuid, email, and binary. An escape hatch via output-options.type-mapping is documented in the PR for users who need to preserve the previous plain-string behavior.

  • pkg/codegen/typemapping.go: one-line addition of \"duration\": {Type: \"openapi_types.Duration\"} to DefaultTypeMapping.String.Formats.
  • pkg/codegen/typemapping_test.go: adds a completeness assertion for the new entry and a TestDurationFormatMapping test that calls Generate() end-to-end and checks the string output, including a second pass that confirms the opt-out type-mapping restores string.

Confidence Score: 4/5

Safe to merge after discussion on whether the unconditional default change is the right approach for existing users.

The change itself is tiny and correct — the new mapping follows the exact same pattern as date, uuid, and binary. The main concern is that any downstream project already using format: duration will silently get a different generated type on upgrade, which breaks their build until they add an explicit override. The test coverage gap means the generated code is never actually built against the real runtime.

Both changed files are small; typemapping.go is the one to scrutinize for the default-on behavior decision.

Important Files Changed

Filename Overview
pkg/codegen/typemapping.go Adds "duration": {Type: "openapi_types.Duration"} to the default string format mapping — a one-line change that unconditionally changes generated types for any spec using format: duration.
pkg/codegen/typemapping_test.go Adds a unit assertion for the new duration mapping and a TestDurationFormatMapping end-to-end string-output test; no compiled integration fixture in internal/test/schemas/ to verify the generated code actually builds against the runtime.
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
pkg/codegen/typemapping.go:102
**Unconditional breaking change for existing `duration` users**

Any project already using `type: string, format: duration` in its spec will silently start generating `openapi_types.Duration` instead of `string` when it upgrades — that's a type mismatch that breaks compilation without the user doing anything wrong. Per the project's stated practice (all the other conditionally-gated behavior changes in `output-options` / `compatibility`), would a new compatibility flag (e.g. `disable-legacy-duration-string`) or an explicit opt-in be more appropriate here, rather than flipping the default? The existing `type-mapping` escape hatch described in the PR description does work, but it requires users to discover and apply it after already hitting a broken build.

### Issue 2 of 2
pkg/codegen/typemapping_test.go:88-143
**Integration test belongs in `internal/test/schemas/`**

Per the project's test placement rules, regression tests for schema-to-type mapping go under `internal/test/schemas/`, not in `pkg/codegen/`. The most natural home is `internal/test/schemas/primitives/` — extending `spec.yaml` with a `format: duration` field (analogous to the existing `format: date` field) and regenerating `primitives.gen.go`. That fixture compiles the generated code against the real runtime package, giving a stronger guarantee than the string-matching approach here. A provenance comment like `# From issue-2456: duration format maps to openapi_types.Duration` keeps the traceability.

Reviews (1): Last reviewed commit: "Map format: duration to the runtime's RF..." | Re-trigger Greptile

Comment thread pkg/codegen/typemapping.go
@mromaszewicz
mromaszewicz merged commit 68f8c3b into main Jul 13, 2026
30 checks passed
@mromaszewicz
mromaszewicz deleted the fix/issue-2456 branch July 13, 2026 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request notable changes Used for release notes to highlight these more highly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for RFC3339 durations

1 participant