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: valid syntax when anyOf is present on querystring #1341

Merged
merged 2 commits into from
Apr 30, 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: 21 additions & 11 deletions packages/core/src/getters/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const combineSchemas = ({

const isAllEnums = resolvedData.isEnum.every((v) => v);

let resolvedValue;
let resolvedValue: ScalarValue | undefined;

if (schema.properties) {
resolvedValue = getScalar({ item: omit(schema, separator), name, context });
Expand All @@ -171,19 +171,29 @@ export const combineSchemas = ({
});

if (isAllEnums && name && items.length > 1) {
const newEnum = `\n\n// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const ${pascal(
const newEnum = `// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const ${pascal(
name,
)} = ${getCombineEnumValue(resolvedData)}`;
)} = ${getCombineEnumValue(resolvedData)};\n`;

return {
value:
`typeof ${pascal(name)}[keyof typeof ${pascal(name)}] ${nullable};` +
newEnum,
imports: resolvedData.imports.map<GeneratorImport>((toImport) => ({
...toImport,
values: true,
})),
schemas: resolvedData.schemas,
value: `typeof ${pascal(name)}[keyof typeof ${pascal(name)}] ${nullable}`,
imports: [
...resolvedData.imports.map<GeneratorImport>((toImport) => ({
...toImport,
values: true,
})),
{
name: pascal(name),
},
],
schemas: [
...resolvedData.schemas,
{
imports: [],
model: newEnum,
name: pascal(name),
},
],
isEnum: false,
type: 'object' as SchemaType,
isRef: false,
Expand Down
8 changes: 8 additions & 0 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ export default defineConfig({
mock: true,
},
},
'any-of': {
input: '../specifications/any-of.yaml',
output: {
schemas: '../generated/default/any-of/model',
target: '../generated/default/any-of/endpoints.ts',
mock: true,
},
},
'deeply-nested-refs': {
input: '../specifications/deeply-nested-refs.yaml',
output: {
Expand Down
22 changes: 22 additions & 0 deletions tests/specifications/any-of.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: AnyOf Schema
license:
name: MIT
paths:
/test:
get:
summary: Gets one of the items
operationId: getItems
parameters:
- name: test
in: query
schema:
anyOf:
- type: string
enum: ['A']
- type: string
enum: ['B']
- type: string
enum: ['C']