Skip to content
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

[@typespec/openapi3] Add $ref to Response Object #3894

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .chronus/changes/Issue3601-2024-6-18-17-42-35.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
sarangan12 marked this conversation as resolved.
Show resolved Hide resolved
packages:
- "@typespec/openapi3"
---

Add '$ref' to the model. Refer [3601](https://github.com/microsoft/typespec/pull/3894) for further details.
sarangan12 marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,10 @@ function createOAPIEmitter(
const openApiResponse = currentEndpoint.responses[statusCode] ?? {
description: response.description ?? getResponseDescriptionForStatusCode(statusCode),
};
const refUrl = getRef(program, response.type);
if (refUrl) {
openApiResponse.$ref = refUrl;
}
emitResponseHeaders(openApiResponse, response.responses, response.type);
emitResponseContent(operation, openApiResponse, response.responses);
currentEndpoint.responses[statusCode] = openApiResponse;
Expand Down
37 changes: 37 additions & 0 deletions packages/openapi3/test/openapi-output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,40 @@ describe("openapi3: extension decorator", () => {
strictEqual(oapi.components.parameters.PetId.schema.pattern, "^[a-zA-Z0-9-]{3,24}$");
});
});

describe("openapi3: useRef decorator", () => {
it("adds a ref extension to a model", async () => {
const oapi = await openApiFor(`
@test @useRef("../common.json#/definitions/Foo")
model Foo {
@statusCode
status: 200;
name: string;
}
@get op get(): Foo;
`);
ok(oapi.paths["/"].get);
strictEqual(oapi.paths["/"].get.responses["200"]["$ref"], "../common.json#/definitions/Foo");
});

it("adds a ref extension to a multiple models", async () => {
const oapi = await openApiFor(`
@test @useRef("../common.json#/definitions/Foo")
model Foo {
@statusCode
status: 200;
name: string;
}
@test @useRef("https://example.com/example.yml")
model Foo2 {
@statusCode
status: 201;
name: string;
}
@get op get(): Foo | Foo2;
`);
ok(oapi.paths["/"].get);
strictEqual(oapi.paths["/"].get.responses["200"]["$ref"], "../common.json#/definitions/Foo");
strictEqual(oapi.paths["/"].get.responses["201"]["$ref"], "https://example.com/example.yml");
});
});
Loading