Skip to content

Commit

Permalink
fix: valid syntax when anyOf is present on querystring (#1341)
Browse files Browse the repository at this point in the history
* fix: anyOf on querystring

* tests: added any-of tests
  • Loading branch information
arthurfiorette committed Apr 30, 2024
1 parent bc0a72e commit 41f6612
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
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']

0 comments on commit 41f6612

Please sign in to comment.