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

Fix set response doc when an envelope #4322

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/http"
---

Use user provided description of model if model has a status code property(detect it as an response envelope)
30 changes: 14 additions & 16 deletions packages/http/src/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ import { HttpProperty } from "./http-property.js";
import { HttpStateKeys, reportDiagnostic } from "./lib.js";
import { Visibility } from "./metadata.js";
import { resolveHttpPayload } from "./payload.js";
import {
HttpOperationBody,
HttpOperationMultipartBody,
HttpOperationResponse,
HttpStatusCodes,
HttpStatusCodesEntry,
} from "./types.js";
import { HttpOperationResponse, HttpStatusCodes, HttpStatusCodesEntry } from "./types.js";

/**
* Get the responses for a given operation.
Expand Down Expand Up @@ -121,13 +115,7 @@ function processResponseType(
statusCode: typeof statusCode === "object" ? "*" : (String(statusCode) as any),
statusCodes: statusCode,
type: responseType,
description: getResponseDescription(
program,
operation,
responseType,
statusCode,
resolvedBody
),
description: getResponseDescription(program, operation, responseType, statusCode, metadata),
responses: [],
};

Expand Down Expand Up @@ -202,12 +190,22 @@ function getResponseHeaders(
return responseHeaders;
}

function isResponseEnvelope(metadata: HttpProperty[]): boolean {
return metadata.some(
(prop) =>
prop.kind === "body" ||
prop.kind === "bodyRoot" ||
prop.kind === "multipartBody" ||
prop.kind === "statusCode"
);
}

function getResponseDescription(
program: Program,
operation: Operation,
responseType: Type,
statusCode: HttpStatusCodes[number],
body: HttpOperationBody | HttpOperationMultipartBody | undefined
metadata: HttpProperty[]
): string | undefined {
// NOTE: If the response type is an envelope and not the same as the body
// type, then use its @doc as the response description. However, if the
Expand All @@ -216,7 +214,7 @@ function getResponseDescription(
// as the response description. This allows more freedom to change how
// TypeSpec is expressed in semantically equivalent ways without causing
// the output to change unnecessarily.
if (body === undefined || body.property) {
if (isResponseEnvelope(metadata)) {
const desc = getDoc(program, responseType);
if (desc) {
return desc;
Expand Down
14 changes: 14 additions & 0 deletions packages/http/test/response-descriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,18 @@ describe("http: response descriptions", () => {
strictEqual(op.responses[2].description, "Not found model");
strictEqual(op.responses[3].description, "Generic error");
});

it("@doc on response model set response doc if model is an evelope with @statusCode", async () => {
const op = await getHttpOp(
`
/** Explicit doc */
model Result {
@statusCode _: 201;
implicit: 200;
}
op read(): Result;
`
);
strictEqual(op.responses[0].description, "Explicit doc");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ paths:
schema:
$ref: '#/components/schemas/Resp'
'404':
description: The server cannot find the requested resource.
description: Not found
content:
application/json:
schema:
Expand Down
Loading