Skip to content

Commit

Permalink
[@typespec/openapi3] Add $ref to Response Object
Browse files Browse the repository at this point in the history
  • Loading branch information
sarangan12 committed Jul 18, 2024
1 parent 8d8e454 commit ad362b1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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");
});
});

0 comments on commit ad362b1

Please sign in to comment.