Skip to content

Add warning when @defaultResponse is used on model with status code or @error - #10555

Merged
timotheeguerin merged 6 commits into
mainfrom
copilot/fix-duplicate-ref-in-return-type
Jun 1, 2026
Merged

Add warning when @defaultResponse is used on model with status code or @error#10555
timotheeguerin merged 6 commits into
mainfrom
copilot/fix-duplicate-ref-in-return-type

Conversation

Copilot AI commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Using @defaultResponse on a model that already has @statusCode or @error produces duplicate $ref entries in OpenAPI output (e.g., anyOf with two identical refs). Rather than silently generating invalid output, emit a diagnostic warning at the source.

Changes

  • packages/openapi/src/lib.ts — New default-response-with-status-code diagnostic with statusCode and error message variants
  • packages/openapi/src/decorators.ts$defaultResponse now checks the target model for existing @statusCode properties and @error decoration before setting the wildcard status code
  • packages/openapi/test/decorators.test.ts — Tests for both warning cases and the clean no-warning path

Example

// This now emits a warning:
@error
@defaultResponse
model DefaultError is Error<500>;

// Recommended alternatives:
@error
model MyError { message: string; }
op read(): Data | MyError;

// Or split concerns:
@error model Error500 is Error<500>;
@error @defaultResponse model DefaultError { message: string; }
alias Result<T> = T | Error500 | DefaultError;

Copilot AI changed the title [WIP] Fix duplicate $ref in return type union for defaultResponse model Add warning when @defaultResponse is used on model with status code or @error Apr 29, 2026
Copilot AI requested a review from timotheeguerin April 29, 2026 17:32
@pkg-pr-new

pkg-pr-new Bot commented Apr 29, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/openapi@10555

commit: ae69b8f

@timotheeguerin

Copy link
Copy Markdown
Member

@copilot fix lint

Copilot AI commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

@copilot fix lint

Fixed in c10f3ab — removed the unnecessary escape character \@@ in the regex, and added the missing changeset for @typespec/openapi.

@github-actions

github-actions Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • @typespec/openapi
Show changes

@typespec/openapi - feature ✏️

Added a warning diagnostic when @defaultResponse is used on a model that already has a @statusCode property or is marked with @error.

@timotheeguerin
timotheeguerin marked this pull request as ready for review May 28, 2026 12:23
@timotheeguerin

Copy link
Copy Markdown
Member

@copilot test fail

 FAIL   @typespec/openapi  test/decorators.test.ts > openapi: decorators > @defaultResponse > emits warning when used on a model marked with @error
AssertionError: Expected 1 diagnostics but found 0:
 

simple

 ❯ expectDiagnostics ../compiler/src/testing/expect.ts:70:9
 ❯ test/decorators.test.ts:594:7
    592|         }
    593|       `);
    594|       expectDiagnostics(diagnostics, [
       |       ^
    595|         { code: "@typespec/openapi/default-response-with-status-code",…
    596|       ]);

Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>

Copilot AI commented May 28, 2026

Copy link
Copy Markdown
Contributor Author

@copilot test fail

 FAIL   @typespec/openapi  test/decorators.test.ts > openapi: decorators > @defaultResponse > emits warning when used on a model marked with @error

...

The @error check was running during decorator application, before @error had been applied to the model. Moved the validation into an onTargetFinish callback so it runs after all decorators on the target are applied. All 60 tests pass now.

@timotheeguerin
timotheeguerin added this pull request to the merge queue Jun 1, 2026
Merged via the queue into main with commit b96c6dd Jun 1, 2026
28 checks passed
@timotheeguerin
timotheeguerin deleted the copilot/fix-duplicate-ref-in-return-type branch June 1, 2026 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: @defaultResponse model included in return type union generates anyOf with duplicate $ref

3 participants