diff --git a/packages/mock/src/faker/getters/scalar.ts b/packages/mock/src/faker/getters/scalar.ts index 1da5d4fff..944b5f859 100644 --- a/packages/mock/src/faker/getters/scalar.ts +++ b/packages/mock/src/faker/getters/scalar.ts @@ -232,7 +232,9 @@ export const getMockScalar = ({ ]; } - value = `faker.helpers.arrayElement(${enumValue})`; + value = item.path?.endsWith('[]') + ? `faker.helpers.arrayElements(${enumValue})` + : `faker.helpers.arrayElement(${enumValue})`; } return { diff --git a/tests/configs/swr.config.ts b/tests/configs/swr.config.ts index 1b699b4b7..499a0fca8 100644 --- a/tests/configs/swr.config.ts +++ b/tests/configs/swr.config.ts @@ -156,4 +156,15 @@ export default defineConfig({ target: '../specifications/arrays.yaml', }, }, + enums: { + output: { + target: '../generated/swr/enums/endpoints.ts', + schemas: '../generated/swr/enums/model', + client: 'swr', + mock: true, + }, + input: { + target: '../specifications/enums.yaml', + }, + }, }); diff --git a/tests/specifications/enums.yaml b/tests/specifications/enums.yaml new file mode 100644 index 000000000..540b1bb51 --- /dev/null +++ b/tests/specifications/enums.yaml @@ -0,0 +1,134 @@ +openapi: 3.0.3 +info: + title: Arrays + version: 1.0.0 +paths: + /api/cat: + get: + summary: sample cat + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Cat' + /api/required-cat: + get: + summary: sample required cat + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/RequiredCat' + /api/dog: + get: + summary: sample dog + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Dog' + /api/required-dog: + get: + summary: sample required dog + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/RequiredDog' + /api/duck: + get: + summary: sample duck + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Duck' +components: + schemas: + Siamese: + type: object + properties: + colours: + type: array + items: + type: string + enum: + - BLACK + - BROWN + - WHITE + - GREY + RequiredSiamese: + type: object + required: ['colours'] + properties: + colours: + type: array + items: + type: string + enum: + - BLACK + - BROWN + - WHITE + - GREY + Cat: + type: object + properties: + petsRequested: + type: array + items: + $ref: '#/components/schemas/Siamese' + RequiredCat: + type: object + properties: + petsRequested: + type: array + items: + $ref: '#/components/schemas/RequiredSiamese' + Bulldog: + type: object + properties: + colour: + type: string + enum: + - BLACK + - BROWN + RequiredBulldog: + type: object + required: ['colour'] + properties: + colour: + type: string + enum: + - BLACK + - BROWN + Dog: + type: object + properties: + petsRequested: + type: array + items: + $ref: '#/components/schemas/Bulldog' + RequiredDog: + type: object + properties: + petsRequested: + type: array + items: + $ref: '#/components/schemas/RequiredBulldog' + Duck: + type: object + properties: + petsRequested: + type: array + items: + type: string