microsoft / microsoft/typespec
[Bug]: Encoding ISO8601 Duration to Numeric Values Causes Ambiguity
- Dominant language
- Java
- Stars
- 5.9k
- Forks
- 394
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 104
Description
### Describe the bug
Encoding ISO8601 duration to seconds or any other numeric value can be problematic when the duration includes non-zero values for days, weeks, months, or years without additional metadata. For example, encoding P1Y (1 year) to seconds requires a reference point to accurately determine the number of seconds. Depending on the reference point, a year can have 365 or 366 days. Similar issues arise with months, weeks, and days.
Suggested Solutions:
- Disallow encoding ISO8601 durations to numeric values and maintain the original format.
- Implement a mechanism to disambiguate the encoding by providing additional metadata or reference points.
Additional Information:
This issue affects any service that relies on precise duration encoding and can lead to significant inaccuracies in time-based calculations.
### Reproduction
This is a [simple spec](https://typespec.io/playground?c=aW1wb3J0ICJAdHlwZXNwZWMvaHR0cCI7Cgp1c2luZyBUeXBlU3BlYy5IdHRwOwpAc2VydmljZSh7CiAgdGl0bGU6ICJXaWRnZXQgU8YbIiwKfSkKbmFtZXNwYWNlIERlbW%2FHGzsKCm1vZGVsIEZvbyDEQkBlbmNvZGUoRHVyYXRpb25Lbm93bkXEFGluZy5zZWNvbmRzLCBpbnQzMikKICBtecgqOiBkxwo7Cn0KCkByb3V0ZSgiL2ZvbyIpCkB0YWcoIkbFDGludGVyZuQAiuYAqHPGenBvc3QgY3JlYXRlKC4uLkZvbyk65ACYxE8%3D&e=%40typespec%2Fopenapi3&options=%7B%22linterRuleSet%22%3A%7B%22extends%22%3A%5B%22%40typespec%2Fhttp%2Fall%22%5D%7D%7D)
Depending on the client implementation the service may receive different values for the same ISO8601 string `P1Y`
### Checklist
- [X] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [X] Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/Microsoft/typespec/discussions).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
Contributor guide
Assessment
This issue has not been assessed yet.