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(zod): all http responses can now be generated #1429

Merged
Merged
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
77 changes: 45 additions & 32 deletions packages/zod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
ZodCoerceType,
generateMutator,
GeneratorMutator,
GlobalMockOptions,
} from '@orval/core';
import uniq from 'lodash.uniq';

Expand Down Expand Up @@ -662,34 +663,35 @@ const generateZodRoute = async (
| PathItemObject
| undefined;

override.useDates;

const parameters = spec?.[verb]?.parameters;
const requestBody = spec?.[verb]?.requestBody;
const response = spec?.[verb]?.responses?.['200'] as
| ResponseObject
| ReferenceObject;

const parsedResponse = parseBodyAndResponse({
data: response,
const parsedParameters = parseParameters({
data: parameters,
context,
name: camel(`${operationName}-response`),
strict: override.zod.strict.response,
operationName,
strict: override.zod.strict,
});

const requestBody = spec?.[verb]?.requestBody;
const parsedBody = parseBodyAndResponse({
data: requestBody,
context,
name: camel(`${operationName}-body`),
strict: override.zod.strict.body,
});

const parsedParameters = parseParameters({
data: parameters,
context,
operationName,
strict: override.zod.strict,
});
const responses = (
(context.output.mock as GlobalMockOptions)?.generateEachHttpStatus
? Object.entries(spec?.[verb]?.responses ?? {})
: [['', spec?.[verb]?.responses[200]]]
) as [string, ResponseObject | ReferenceObject][];
const parsedResponses = responses.map(([code, response]) =>
parseBodyAndResponse({
data: response,
context,
name: camel(`${operationName}-${code}-response`),
strict: override.zod.strict.response,
}),
);

const inputParams = parseZodValidationSchemaDefinition(
parsedParameters.params,
Expand Down Expand Up @@ -730,19 +732,21 @@ const generateZodRoute = async (
})
: undefined;

const inputResponse = parseZodValidationSchemaDefinition(
parsedResponse.input,
context,
override.zod.coerce.response,
preprocessResponse,
const inputResponses = parsedResponses.map((parsedResponse) =>
parseZodValidationSchemaDefinition(
parsedResponse.input,
context,
override.zod.coerce.response,
preprocessResponse,
),
);

if (
!inputParams.zod &&
!inputQueryParams.zod &&
!inputHeaders.zod &&
!inputBody.zod &&
!inputResponse.zod
!inputResponses.some((inputResponse) => inputResponse.zod)
) {
return {
implemtation: '',
Expand Down Expand Up @@ -773,15 +777,24 @@ export const ${operationName}Body = zod.array(${operationName}BodyItem)`
: `export const ${operationName}Body = ${inputBody.zod}`,
]
: []),
...(inputResponse.consts ? [inputResponse.consts] : []),
...(inputResponse.zod
? [
parsedResponse.isArray
? `export const ${operationName}ResponseItem = ${inputResponse.zod}
export const ${operationName}Response = zod.array(${operationName}ResponseItem)`
: `export const ${operationName}Response = ${inputResponse.zod}`,
]
: []),
...inputResponses
.map((inputResponse, index) => {
const operationResponse = camel(
`${operationName}-${responses[index][0]}-response`,
);
return [
...(inputResponse.consts ? [inputResponse.consts] : []),
...(inputResponse.zod
? [
parsedResponses[index].isArray
? `export const ${operationResponse}Item = ${inputResponse.zod}
export const ${operationResponse} = zod.array(${operationResponse}Item)`
: `export const ${operationResponse} = ${inputResponse.zod}`,
]
: []),
];
})
.flat(),
].join('\n\n'),
mutators: preprocessResponse ? [preprocessResponse] : [],
};
Expand Down