From efc4c35bd5f1726c265e9c4d8d1df236804895e0 Mon Sep 17 00:00:00 2001 From: Arthur Fiorette <47537704+arthurfiorette@users.noreply.github.com> Date: Thu, 16 May 2024 18:58:18 -0300 Subject: [PATCH] fix: handle OpenAPI 3.1 nullability for mocks (#1381) --- packages/mock/src/faker/getters/object.ts | 17 +++++++++++++++++ packages/mock/src/faker/getters/scalar.ts | 8 +++++++- tests/configs/mock.config.ts | 10 ++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/mock/src/faker/getters/object.ts b/packages/mock/src/faker/getters/object.ts index 5a752a8e1..f4ee311c5 100644 --- a/packages/mock/src/faker/getters/object.ts +++ b/packages/mock/src/faker/getters/object.ts @@ -72,6 +72,23 @@ export const getMockObject = ({ }); } + if (Array.isArray(item.type)) { + return combineSchemasMock({ + item: { + anyOf: item.type.map((type) => ({ type })), + name: item.name, + }, + separator: 'anyOf', + mockOptions, + operationId, + tags, + combine, + context, + imports, + existingReferencedProperties, + }); + } + if (item.properties) { let value = !combine || diff --git a/packages/mock/src/faker/getters/scalar.ts b/packages/mock/src/faker/getters/scalar.ts index 78a09f62e..33b706afa 100644 --- a/packages/mock/src/faker/getters/scalar.ts +++ b/packages/mock/src/faker/getters/scalar.ts @@ -248,7 +248,13 @@ export const getMockScalar = ({ }; } - case 'object': + case 'null': + return { + value: 'null', + imports: [], + name: item.name, + }; + default: { return getMockObject({ item, diff --git a/tests/configs/mock.config.ts b/tests/configs/mock.config.ts index 0a2786c2c..b65e6a1a1 100644 --- a/tests/configs/mock.config.ts +++ b/tests/configs/mock.config.ts @@ -104,4 +104,14 @@ export default defineConfig({ target: '../specifications/petstore.yaml', }, }, + nullType: { + output: { + schemas: '../generated/mock/null-type/model', + target: '../generated/mock/null-type/endpoints.ts', + mock: true, + }, + input: { + target: '../specifications/null-type.yaml', + }, + }, });