Skip to content

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

Open
Copilot wants to merge 6 commits into
mainfrom
copilot/fix-duplicate-ref-in-return-type
Open

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

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

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;

…code or @error

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/2d6ca218-090a-4b98-8bfd-f31ddb57b9c9

Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>
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
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 29, 2026

Open in StackBlitz

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

commit: ae69b8f

@timotheeguerin
Copy link
Copy Markdown
Member

@copilot fix lint

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 29, 2026

@copilot fix lint

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 28, 2026

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>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 28, 2026

@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.

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

2 participants