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 { 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.
@ -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: [],
};
@ -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
@ -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;

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

@ -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");
});
});

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

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