Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 19, 2026

OpenAPI duration property import fix

This PR fixes the importer to correctly convert OpenAPI type: number, format: duration properties to TypeSpec duration type with appropriate @encode decorator.

Changes completed:

  • Add test cases for number with duration format in generate-type.test.ts
  • Update getNumberType function to handle duration format
  • Add decorator generation for duration format on numbers
  • Run tests to validate changes
  • Format code before final commit
  • Add changelog entry using chronus
  • Manual validation with real OpenAPI specs
  • Add regression test for string duration format (no encode decorator)

Implementation details:

  • Modified getNumberType() in generate-types.ts to return duration when format is duration
  • Added getDurationSchemaDecorators() function in decorators.ts to generate @encode("seconds", float32) decorator for number types with duration format
  • Added test case in generate-type.test.ts for inline type expression
  • Added test case in data-types.test.ts for scalar generation with decorator
  • Added regression test for type: string, format: duration to ensure it converts to duration without encode decorator
  • All 2378 tests in openapi3 package pass successfully
  • Lint checks pass without warnings

Example transformations:

Number with duration (new behavior):

# OpenAPI
DurationInSeconds:
  type: number
  format: duration
// TypeSpec
@encode("seconds", float32)
scalar DurationInSeconds extends duration;

String with duration (existing behavior, no regression):

# OpenAPI
DurationString:
  type: string
  format: duration
// TypeSpec
scalar DurationString extends duration;
Original prompt

This section details on the original issue you should resolve

<issue_title>importer - OpenAPI duration properties should result in a duration properties</issue_title>
<issue_description>### Clear and concise description of the problem

When importing a property with the following schema:

foo:
  type: number
  format: duration

Currently results in this

foo: numeric

But should result in this

@encode("seconds", float32)
foo: duration

Checklist

  • Follow our Code of Conduct
  • Read the docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.</issue_description>

<agent_instructions>use conventional commits.
run the formatting command before any commit.
start by writing the unit tests first.
when done, run the following command to add a feature entry to the changelog npx @chronus/chronus add</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix importer to support OpenAPI duration properties feat(openapi3): convert number type with duration format to duration with @encode decorator Jan 19, 2026
Copilot AI requested a review from baywet January 19, 2026 19:35
@baywet
Copy link
Member

baywet commented Jan 19, 2026

@copilot add another unit test to validate there are no regressions if it does not exist already.

The following OpenAPI description

foo:
  type: string
  format: duration

Should result in this TypeSpec

bar: duration

Note there are no encode attribute in that case.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 19, 2026

Open in StackBlitz

npm i https://pkg.pr.new/microsoft/typespec/@typespec/openapi@9412
npm i https://pkg.pr.new/microsoft/typespec/@typespec/openapi3@9412

commit: 09b3593

Copy link
Contributor Author

Copilot AI commented Jan 19, 2026

@copilot add another unit test to validate there are no regressions if it does not exist already.

The following OpenAPI description
...

Added regression test in commit 09b3593. The test validates that type: string, format: duration converts to duration without an encode decorator, ensuring no regression from the number duration changes.

@baywet baywet marked this pull request as ready for review January 19, 2026 19:49
@baywet baywet enabled auto-merge January 19, 2026 19:49
@microsoft-github-policy-service microsoft-github-policy-service bot added emitter:openapi3 Issues for @typespec/openapi3 emitter openapi3:converter Issues for @typespec/openapi3 openapi to typespec converter labels Jan 19, 2026
@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

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

Labels

emitter:openapi3 Issues for @typespec/openapi3 emitter openapi3:converter Issues for @typespec/openapi3 openapi to typespec converter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

importer - OpenAPI duration properties should result in a duration properties

3 participants