Skip to content
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
1 change: 1 addition & 0 deletions packages/mock/src/faker/getters/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export function getMockObject({
Array.isArray(prop.type) && prop.type.includes('null');
if (
isNullable &&
!resolvedValue.nullWrapped &&
!resolvedValue.overrided &&
!mockOptions?.nonNullable
) {
Expand Down
14 changes: 12 additions & 2 deletions packages/mock/src/faker/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,13 @@ export function getMockScalar({
),
};

// OpenAPI 3.1 null unions only — 3.0 `nullable: true` is handled in object.ts
// to avoid double-wrapping scalar values that object.ts already null-randomizes.
// Both OpenAPI 3.1 `type: [..., 'null']` and OpenAPI 3.0 `nullable: true`
// reach here as a null union, because @scalar/openapi-parser upgrades 3.0
// inputs to 3.1 before mock generation. When this getter wraps the value via
// `getNullable` it flags the returned MockDefinition with `nullWrapped` so the
// object property layer does not add a second `arrayElement([..., null])`.
const isNullable = Array.isArray(item.type) && item.type.includes('null');
const nullWrapped = isNullable && !nonNullableOption;
// The @scalar/openapi-parser upgrader rewrites `format: binary` to
// `contentMediaType: application/octet-stream` when upgrading OAS 3.0 → 3.1;
// treat both equivalently so the mock emits the binary format value
Expand All @@ -161,6 +165,7 @@ export function getMockScalar({
imports: [],
name: item.name,
overrided: false,
nullWrapped,
};
}
if (item.format && ALL_FORMAT[item.format]) {
Expand All @@ -176,6 +181,7 @@ export function getMockScalar({
imports: [],
name: item.name,
overrided: false,
nullWrapped,
};
}

Expand Down Expand Up @@ -249,6 +255,9 @@ export function getMockScalar({
enums: item.enum,
imports: numberImports,
name: item.name,
// `item.enum` / `const` reassign `value` after `getNullable`, discarding
// the wrap — so only the plain numeric path is actually null-wrapped.
nullWrapped: nullWrapped && !item.enum && !('const' in item),
};
}

Expand Down Expand Up @@ -450,6 +459,7 @@ export function getMockScalar({
enums: item.enum,
name: item.name,
imports: stringImports,
nullWrapped,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/mock/src/faker/resolvers/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ export function resolveMockValue({
imports,
name: newSchema.name,
type: getType(newSchema),
nullWrapped: Boolean(newSchema.nullable) && !mockOptions?.nonNullable,
};
}

Expand Down
4 changes: 4 additions & 0 deletions packages/mock/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export interface MockDefinition {
name: string;
overrided?: boolean;
includedProperties?: string[];
// True when `value` already embeds its own null branch (e.g. the scalar
// getter wrapped it via `getNullable`). The object property layer reads this
// to avoid wrapping the value in a second `arrayElement([..., null])`.
nullWrapped?: boolean;
}

type OpenApiObjectSchema = Extract<OpenApiSchemaObject, object>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ export const getSearchPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -83,10 +80,7 @@ export const getListPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -124,10 +118,7 @@ export const getListPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -186,10 +177,7 @@ export const getUpdatePetByIdResponseMock = (
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -232,10 +220,7 @@ export const getPatchPetByIdResponseMock = (
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down
25 changes: 5 additions & 20 deletions samples/angular-app/__snapshots__/api/http-both/pets/pets.msw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ export const getSearchPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -83,10 +80,7 @@ export const getListPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -124,10 +118,7 @@ export const getListPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -186,10 +177,7 @@ export const getUpdatePetByIdResponseMock = (
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -232,10 +220,7 @@ export const getPatchPetByIdResponseMock = (
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ export const getSearchPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -83,10 +80,7 @@ export const getListPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -124,10 +118,7 @@ export const getListPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -186,10 +177,7 @@ export const getUpdatePetByIdResponseMock = (
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -232,10 +220,7 @@ export const getPatchPetByIdResponseMock = (
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down
25 changes: 5 additions & 20 deletions samples/angular-app/__snapshots__/api/http-client/pets/pets.msw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ export const getSearchPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -83,10 +80,7 @@ export const getListPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -124,10 +118,7 @@ export const getListPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -186,10 +177,7 @@ export const getUpdatePetByIdResponseMock = (
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -232,10 +220,7 @@ export const getPatchPetByIdResponseMock = (
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ export const getSearchPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -83,10 +80,7 @@ export const getListPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -124,10 +118,7 @@ export const getListPetsResponseMock = (): Pets =>
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -186,10 +177,7 @@ export const getUpdatePetByIdResponseMock = (
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down Expand Up @@ -232,10 +220,7 @@ export const getPatchPetByIdResponseMock = (
undefined,
]))(),
requiredNullableString: faker.helpers.arrayElement([
faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
faker.string.alpha({ length: { min: 10, max: 20 } }),
null,
]),
optionalNullableString: faker.helpers.arrayElement([
Expand Down
Loading
Loading