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

[TCGC] Change the responses and exceptions to be arrays #1540

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
changeKind: breaking
packages:
- "@azure-tools/typespec-client-generator-core"
---

1. The type of `responses` and `exceptions` in `SdkHttpOperation` changed from `Map` to array.
2. The type of `responses` in `SdkHttpOperationExample` changed from `Map` to array.
3. `SdkHttpResponse` adds a new property `statusCodes` to store its corresponding status code or status code range.
tadelesh marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 15 additions & 11 deletions packages/typespec-client-generator-core/src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
isService,
resolvePath,
} from "@typespec/compiler";
import { HttpStatusCodeRange } from "@typespec/http";
import { getOperationId } from "@typespec/openapi";
import {
SdkArrayExampleValue,
Expand Down Expand Up @@ -291,12 +290,12 @@ function handleHttpParameters(
}

function handleHttpResponses(
responses: Map<number | HttpStatusCodeRange, SdkHttpResponse>,
responses: SdkHttpResponse[],
example: any,
relativePath: string
): [Map<number, SdkHttpResponseExampleValue>, readonly Diagnostic[]] {
): [SdkHttpResponseExampleValue[], readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
const responseExamples = new Map<number, SdkHttpResponseExampleValue>();
const responseExamples: SdkHttpResponseExampleValue[] = [];
if (
"responses" in example &&
typeof example.responses === "object" &&
Expand All @@ -305,11 +304,13 @@ function handleHttpResponses(
for (const code of Object.keys(example.responses)) {
const statusCode = parseInt(code, 10);
let found = false;
for (const [responseCode, response] of responses.entries()) {
for (const response of responses) {
const responseCode = response.statusCodes;
if (responseCode === statusCode) {
responseExamples.set(
statusCode,
diagnostics.pipe(handleHttpResponse(response, example.responses[code], relativePath))
responseExamples.push(
diagnostics.pipe(
handleHttpResponse(response, statusCode, example.responses[code], relativePath)
)
);
found = true;
break;
Expand All @@ -319,9 +320,10 @@ function handleHttpResponses(
responseCode.start <= statusCode &&
responseCode.end >= statusCode
) {
responseExamples.set(
statusCode,
diagnostics.pipe(handleHttpResponse(response, example.responses[code], relativePath))
responseExamples.push(
diagnostics.pipe(
handleHttpResponse(response, statusCode, example.responses[code], relativePath)
)
);
found = true;
break;
Expand All @@ -341,12 +343,14 @@ function handleHttpResponses(

function handleHttpResponse(
response: SdkHttpResponse,
statusCode: number,
example: any,
relativePath: string
): [SdkHttpResponseExampleValue, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
const responseExample: SdkHttpResponseExampleValue = {
response,
statusCode,
headers: [],
};
if (typeof example === "object" && example !== null) {
Expand Down
16 changes: 8 additions & 8 deletions packages/typespec-client-generator-core/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
HttpOperationParameter,
HttpOperationPathParameter,
HttpOperationQueryParameter,
HttpStatusCodeRange,
getHeaderFieldName,
getHeaderFieldOptions,
getPathParamName,
Expand Down Expand Up @@ -412,14 +411,14 @@ function getSdkHttpResponseAndExceptions(
httpOperation: HttpOperation
): [
{
responses: Map<HttpStatusCodeRange | number, SdkHttpResponse>;
exceptions: Map<HttpStatusCodeRange | number | "*", SdkHttpResponse>;
responses: SdkHttpResponse[];
exceptions: SdkHttpResponse[];
},
readonly Diagnostic[],
] {
const diagnostics = createDiagnosticCollector();
const responses: Map<HttpStatusCodeRange | number, SdkHttpResponse> = new Map();
const exceptions: Map<HttpStatusCodeRange | number | "*", SdkHttpResponse> = new Map();
const responses: SdkHttpResponse[] = [];
const exceptions: SdkHttpResponse[] = [];
for (const response of httpOperation.responses) {
const headers: SdkServiceResponseHeader[] = [];
let body: Type | undefined;
Expand Down Expand Up @@ -471,6 +470,7 @@ function getSdkHttpResponseAndExceptions(
kind: "http",
type: body ? diagnostics.pipe(getClientTypeWithDiagnostics(context, body)) : undefined,
headers,
statusCodes: response.statusCodes,
contentTypes: contentTypes.length > 0 ? contentTypes : undefined,
defaultContentType: contentTypes.includes("application/json")
? "application/json"
Expand All @@ -482,10 +482,10 @@ function getSdkHttpResponseAndExceptions(
),
description: response.description,
};
if (response.statusCodes === "*" || (body && isErrorModel(context.program, body))) {
exceptions.set(response.statusCodes, sdkResponse);
if (sdkResponse.statusCodes === "*" || (body && isErrorModel(context.program, body))) {
exceptions.push(sdkResponse);
} else {
responses.set(response.statusCodes, sdkResponse);
responses.push(sdkResponse);
}
}
return diagnostics.wrap({ responses, exceptions });
Expand Down
8 changes: 5 additions & 3 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ export interface SdkHttpResponse extends SdkServiceResponse {
contentTypes?: string[];
defaultContentType?: string;
description?: string;
statusCodes: number | HttpStatusCodeRange | "*";
ArcturusZhang marked this conversation as resolved.
Show resolved Hide resolved
}

interface SdkServiceOperationBase {}
Expand All @@ -564,8 +565,8 @@ export interface SdkHttpOperation extends SdkServiceOperationBase {
verb: HttpVerb;
parameters: (SdkPathParameter | SdkQueryParameter | SdkHeaderParameter)[];
bodyParam?: SdkBodyParameter;
responses: Map<HttpStatusCodeRange | number, SdkHttpResponse>;
exceptions: Map<HttpStatusCodeRange | number | "*", SdkHttpResponse>;
responses: SdkHttpResponse[];
exceptions: SdkHttpResponse[];
examples?: SdkHttpOperationExample[];
}

Expand Down Expand Up @@ -701,7 +702,7 @@ interface SdkExampleBase {
export interface SdkHttpOperationExample extends SdkExampleBase {
kind: "http";
parameters: SdkHttpParameterExampleValue[];
responses: Map<number, SdkHttpResponseExampleValue>;
responses: SdkHttpResponseExampleValue[];
}

export interface SdkHttpParameterExampleValue {
Expand All @@ -711,6 +712,7 @@ export interface SdkHttpParameterExampleValue {

export interface SdkHttpResponseExampleValue {
response: SdkHttpResponse;
statusCode: number;
headers: SdkHttpResponseHeaderExampleValue[];
bodyValue?: SdkExampleValue;
}
Expand Down
11 changes: 3 additions & 8 deletions packages/typespec-client-generator-core/src/internal-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
HttpOperationBody,
HttpOperationMultipartBody,
HttpOperationResponseContent,
HttpStatusCodeRange,
} from "@typespec/http";
import { getAddedOnVersions, getRemovedOnVersions, getVersions } from "@typespec/versioning";
import { getParamAlias } from "./decorators.js";
Expand Down Expand Up @@ -397,15 +396,13 @@ export function getNullOption(type: Union): Type | undefined {
return [...type.variants.values()].map((x) => x.type).filter((t) => isNullType(t))[0];
}

export function getAllResponseBodiesAndNonBodyExists(
responses: Map<HttpStatusCodeRange | number | "*", SdkHttpResponse>
): {
export function getAllResponseBodiesAndNonBodyExists(responses: SdkHttpResponse[]): {
allResponseBodies: SdkType[];
nonBodyExists: boolean;
} {
const allResponseBodies: SdkType[] = [];
let nonBodyExists = false;
for (const response of responses.values()) {
for (const response of responses) {
if (response.type) {
if (response.type.kind === "nullable") {
nonBodyExists = true;
Expand All @@ -418,9 +415,7 @@ export function getAllResponseBodiesAndNonBodyExists(
return { allResponseBodies, nonBodyExists };
}

export function getAllResponseBodies(
responses: Map<HttpStatusCodeRange | number | "*", SdkHttpResponse>
): SdkType[] {
export function getAllResponseBodies(responses: SdkHttpResponse[]): SdkType[] {
return getAllResponseBodiesAndNonBodyExists(responses).allResponseBodies;
}

Expand Down
Loading
Loading