Fix set response doc when an envelope (#4322)

fix [#3664](https://github.com/microsoft/typespec/issues/3664)
This commit is contained in:
Timothee Guerin 2024-09-06 16:45:59 -07:00 коммит произвёл GitHub
Родитель d2ac995842
Коммит f4c8710673
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 37 добавлений и 17 удалений

Просмотреть файл

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

Просмотреть файл

@ -19,13 +19,7 @@ import { HttpProperty } from "./http-property.js";
import { HttpStateKeys, reportDiagnostic } from "./lib.js"; import { HttpStateKeys, reportDiagnostic } from "./lib.js";
import { Visibility } from "./metadata.js"; import { Visibility } from "./metadata.js";
import { resolveHttpPayload } from "./payload.js"; import { resolveHttpPayload } from "./payload.js";
import { import { HttpOperationResponse, HttpStatusCodes, HttpStatusCodesEntry } from "./types.js";
HttpOperationBody,
HttpOperationMultipartBody,
HttpOperationResponse,
HttpStatusCodes,
HttpStatusCodesEntry,
} from "./types.js";
/** /**
* Get the responses for a given operation. * Get the responses for a given operation.
@ -121,13 +115,7 @@ function processResponseType(
statusCode: typeof statusCode === "object" ? "*" : (String(statusCode) as any), statusCode: typeof statusCode === "object" ? "*" : (String(statusCode) as any),
statusCodes: statusCode, statusCodes: statusCode,
type: responseType, type: responseType,
description: getResponseDescription( description: getResponseDescription(program, operation, responseType, statusCode, metadata),
program,
operation,
responseType,
statusCode,
resolvedBody
),
responses: [], responses: [],
}; };
@ -202,12 +190,22 @@ function getResponseHeaders(
return responseHeaders; 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( function getResponseDescription(
program: Program, program: Program,
operation: Operation, operation: Operation,
responseType: Type, responseType: Type,
statusCode: HttpStatusCodes[number], statusCode: HttpStatusCodes[number],
body: HttpOperationBody | HttpOperationMultipartBody | undefined metadata: HttpProperty[]
): string | undefined { ): string | undefined {
// NOTE: If the response type is an envelope and not the same as the body // 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 // type, then use its @doc as the response description. However, if the
@ -216,7 +214,7 @@ function getResponseDescription(
// as the response description. This allows more freedom to change how // as the response description. This allows more freedom to change how
// TypeSpec is expressed in semantically equivalent ways without causing // TypeSpec is expressed in semantically equivalent ways without causing
// the output to change unnecessarily. // the output to change unnecessarily.
if (body === undefined || body.property) { if (isResponseEnvelope(metadata)) {
const desc = getDoc(program, responseType); const desc = getDoc(program, responseType);
if (desc) { if (desc) {
return desc; return desc;

Просмотреть файл

@ -66,4 +66,18 @@ describe("http: response descriptions", () => {
strictEqual(op.responses[2].description, "Not found model"); strictEqual(op.responses[2].description, "Not found model");
strictEqual(op.responses[3].description, "Generic error"); 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");
});
}); });

Просмотреть файл

@ -73,7 +73,7 @@ paths:
schema: schema:
$ref: '#/components/schemas/Resp' $ref: '#/components/schemas/Resp'
'404': '404':
description: The server cannot find the requested resource. description: Not found
content: content:
application/json: application/json:
schema: schema: