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

BREAKING CHANGE: [IOCOM-1891] Use 3xx responses as success type fallback #330

Merged
merged 1 commit into from
Oct 22, 2024
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
32 changes: 31 additions & 1 deletion src/commands/gen-api-models/__tests__/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as SwaggerParser from "swagger-parser";
import { renderDefinitionCode, renderAllOperations } from "../render";
import { getDefinitionOrFail, getParser } from "./utils/parser.utils";

import { getfirstSuccessType } from "../render";

let spec: OpenAPIV2.Document;

describe.each`
Expand Down Expand Up @@ -50,5 +52,33 @@ describe.each`
const code = renderAllOperations([operationInfo1], true)
expect(code).toMatchSnapshot();
})

});

describe("getfirstSuccessType", () => {
it("should return the first successful response", () => {
const responses = [
{ e1: "200", e2: "SuccessType", e3: [] },
{ e1: "301", e2: "RedirectType", e3: [] }
];
const result = getfirstSuccessType(responses);
expect(result).toEqual({ e1: "200", e2: "SuccessType", e3: [] });
});

it("should return the first redirection response if no successful response is found", () => {
const responses = [
{ e1: "301", e2: "RedirectType", e3: [] },
{ e1: "302", e2: "AnotherRedirectType", e3: [] }
];
const result = getfirstSuccessType(responses);
expect(result).toEqual({ e1: "301", e2: "RedirectType", e3: [] });
});

it("should return undefined if no successful or redirection responses are found", () => {
const responses = [
{ e1: "400", e2: "ClientErrorType", e3: [] },
{ e1: "500", e2: "ServerErrorType", e3: [] }
];
const result = getfirstSuccessType(responses);
expect(result).toBeUndefined();
});
});
34 changes: 31 additions & 3 deletions src/commands/gen-api-models/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,36 @@ export const renderOperation = (
${requestTypeDefinition}${responsesDecoderCode}`;
};

/**
* Retrieves the first successful response type from a list of operation responses.
*
* This function iterates through the provided responses and checks the status code.
* If a response has a status code starting with "2" (indicating success), it returns that response.
* If no such response is found, it checks for responses with status codes starting with "3" (indicating redirection).
* If any redirection responses are found, it returns the first one.
* If neither successful nor redirection responses are found, it returns `undefined`.
*
* @param responses - An array of operation responses to evaluate.
* @returns The first successful response, the first redirection response if no successful response is found, or `undefined` if neither are found.
*/
export const getfirstSuccessType = (
responses: IOperationInfo["responses"]
): IOperationInfo["responses"][number] | undefined => {
const redirectResponses = [];
for (const response of responses) {
if (response.e1.length === 3) {
if (response.e1[0] === "2") {
return response;
}
if (response.e1[0] === "3") {
// eslint-disable-next-line functional/immutable-data
redirectResponses.push(response);
}
}
}
return redirectResponses.length > 0 ? redirectResponses[0] : undefined;
};

/**
* Compose the code for response decoder of an operation
*
Expand All @@ -206,9 +236,7 @@ export const renderOperation = (
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, prefer-arrow/prefer-arrow-functions
export function renderDecoderCode({ responses, operationId }: IOperationInfo) {
// use the first 2xx type as "success type" that we allow to be overridden
const firstSuccessType = responses.find(
({ e1 }) => e1.length === 3 && e1[0] === "2"
);
const firstSuccessType = getfirstSuccessType(responses);
if (!firstSuccessType) {
return "";
}
Expand Down
Loading