-
Notifications
You must be signed in to change notification settings - Fork 68
feat(duration): add ISO 8601 / RFC 3339 duration format (duration-iso8601) #284
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ | |
| .env | ||
| .mcp.json | ||
| go.work.sum | ||
| .worktrees | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package conv | ||
|
|
||
| import "github.com/go-openapi/strfmt" | ||
|
|
||
| // DurationISO8601 returns a pointer to of the [strfmt.DurationISO8601] value passed in. | ||
| func DurationISO8601(v strfmt.DurationISO8601) *strfmt.DurationISO8601 { | ||
| return &v | ||
| } | ||
|
|
||
| // DurationISO8601Value returns the value of the [strfmt.DurationISO8601] pointer passed in or | ||
| // the default value if the pointer is nil. | ||
| func DurationISO8601Value(v *strfmt.DurationISO8601) strfmt.DurationISO8601 { | ||
| if v == nil { | ||
| return strfmt.DurationISO8601(0) | ||
| } | ||
|
|
||
| return *v | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package conv | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/go-openapi/testify/v2/assert" | ||
|
|
||
| "github.com/go-openapi/strfmt" | ||
| ) | ||
|
|
||
| func TestDurationISO8601Value(t *testing.T) { | ||
| assert.EqualT(t, strfmt.DurationISO8601(0), DurationISO8601Value(nil)) | ||
| duration := strfmt.DurationISO8601(42) | ||
| assert.EqualT(t, duration, DurationISO8601Value(&duration)) | ||
| assert.EqualT(t, duration, *DurationISO8601(duration)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package strfmt | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "iter" | ||
| "os" | ||
| "slices" | ||
| "testing" | ||
|
|
||
| "github.com/go-openapi/testify/v2/assert" | ||
| "github.com/go-openapi/testify/v2/require" | ||
| ) | ||
|
|
||
| // TestDurationISO8601_Compliance asserts the strict parser matches the official | ||
| // JSON Schema Test Suite for the "duration" format (RFC 3339 Appendix A). | ||
| func TestDurationISO8601_Compliance(t *testing.T) { | ||
| for tc := range durationTestCases(t) { | ||
| t.Run(tc.Desc, func(t *testing.T) { | ||
| got := IsDurationISO8601(tc.Input) | ||
| assert.Equalf(t, tc.Valid, got, | ||
| "input %q: strict validity mismatch (%s)", tc.Input, tc.Desc) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestDurationISO8601_Lenient checks that DurationLenient accepts exactly the | ||
| // cases the strict grammar rejects for leniency reasons. | ||
| func TestDurationISO8601_Lenient(t *testing.T) { | ||
| lenient := DurationLenient{}.isoDurationConfig() | ||
|
|
||
| for tc := range lenientISO8601DurationCases() { | ||
| if tc.Valid { | ||
| t.Run("lenient/"+tc.Desc, func(t *testing.T) { | ||
| assert.Falsef(t, IsDurationISO8601(tc.Input), "strict should reject %q", tc.Input) | ||
| _, err := parseISO8601Duration(tc.Input, lenient) | ||
| assert.NoErrorf(t, err, "lenient should accept %q", tc.Input) | ||
| }) | ||
|
|
||
| continue | ||
| } | ||
|
|
||
| // Ordering is still enforced under leniency. | ||
| t.Run("lenient-rejects/"+tc.Desc, func(t *testing.T) { | ||
| _, err := parseISO8601Duration(tc.Input, lenient) | ||
| assert.Errorf(t, err, "lenient should still reject out-of-order %q", tc.Input) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // jsonSchemaSuiteGroup mirrors the JSON Schema Test Suite file layout. | ||
| type jsonSchemaSuiteGroup struct { | ||
| Description string `json:"description"` | ||
| Tests []struct { | ||
| Description string `json:"description"` | ||
| Data json.RawMessage `json:"data"` | ||
| Valid bool `json:"valid"` | ||
| } `json:"tests"` | ||
| } | ||
|
|
||
| type durationTestCase struct { | ||
| Desc string | ||
| Input string | ||
| Valid bool | ||
| } | ||
|
|
||
| func durationTestCases(t *testing.T) iter.Seq[durationTestCase] { | ||
| t.Helper() | ||
|
|
||
| return func(yield func(durationTestCase) bool) { | ||
| raw, err := os.ReadFile("testdata/jsonschema-suite/duration.json") | ||
| require.NoError(t, err) | ||
|
|
||
| var groups []jsonSchemaSuiteGroup | ||
| require.NoError(t, json.Unmarshal(raw, &groups)) | ||
|
|
||
| for _, g := range groups { | ||
| for _, tc := range g.Tests { | ||
| // The parser only ever sees string data; the format keyword ignores | ||
| // non-string JSON values. Skip explicit JSON null (decodes to ""). | ||
| if string(tc.Data) == "null" { | ||
| continue | ||
| } | ||
| var s string | ||
| if json.Unmarshal(tc.Data, &s) != nil { | ||
| continue | ||
| } | ||
|
|
||
| tc := durationTestCase{tc.Description, s, tc.Valid} | ||
| if !yield(tc) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func lenientISO8601DurationCases() iter.Seq[durationTestCase] { | ||
| return slices.Values([]durationTestCase{ | ||
| {Input: "PT0.5S", Desc: "fraction", Valid: true}, | ||
| {Input: "P1,5D", Desc: "comma fraction", Valid: true}, | ||
| {Input: "-P1D", Desc: "sign", Valid: true}, | ||
| {Input: "+P1D", Desc: "sign", Valid: true}, | ||
| {Input: " P1D", Desc: "whitespace", Valid: true}, | ||
| {Input: "P1D ", Desc: "whitespace", Valid: true}, | ||
| {Input: "P1Y2D", Desc: "relaxed anchoring (gap)", Valid: true}, | ||
| {Input: "PT1H2S", Desc: "relaxed anchoring (gap)", Valid: true}, | ||
| {Input: "P1Y2W", Desc: "week combinable", Valid: true}, | ||
| {Input: "P2D1Y", Desc: "invalid under lenient", Valid: false}, | ||
| {Input: "PT1M2H", Desc: "invalid under lenient (2)", Valid: false}, | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo