Skip to content
Closed
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
54 changes: 54 additions & 0 deletions packages/mock/src/faker/getters/scalar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,60 @@ describe('getMockScalar (undefined filtering)', () => {
'faker.string.alpha({length: {min: 5, max: 20}})',
);
});

it('should include only max when only maxLength is specified', () => {
const result = getMockScalar({
...baseArg,
item: {
type: 'string' as const,
maxLength: 5,
name: 'test-item',
},
});

expect(result.value).toBe('faker.string.alpha({length: {max: 5}})');
});

it('should include only min when only minLength is specified', () => {
const result = getMockScalar({
...baseArg,
item: {
type: 'string' as const,
minLength: 30,
name: 'test-item',
},
});

expect(result.value).toBe('faker.string.alpha({length: {min: 30}})');
});

it('should include only max when only maxItems is specified', () => {
const result = getMockScalar({
...baseArg,
item: {
type: 'array' as const,
maxItems: 5,
name: 'test-item',
items: { type: 'string' },
},
});

expect(result.value).toContain('faker.number.int({max: 5})');
});

it('should include only min when only minItems is specified', () => {
const result = getMockScalar({
...baseArg,
item: {
type: 'array' as const,
minItems: 3,
name: 'test-item',
items: { type: 'string' },
},
});

expect(result.value).toContain('faker.number.int({min: 3})');
});
});

describe('getMockScalar (exclusiveMinimum/exclusiveMaximum handling)', () => {
Expand Down
28 changes: 16 additions & 12 deletions packages/mock/src/faker/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,14 @@ export function getMockScalar({
mapValue = `{${value}}`;
}

const arrMin = (item.minItems ?? safeMockOptions.arrayMin) as
| number
| undefined;
const arrMax = (item.maxItems ?? safeMockOptions.arrayMax) as
| number
| undefined;
const arrMin = (item.minItems ??
(item.maxItems === undefined
? safeMockOptions.arrayMin
: undefined)) as number | undefined;
const arrMax = (item.maxItems ??
(item.minItems === undefined
? safeMockOptions.arrayMax
: undefined)) as number | undefined;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const arrParts: string[] = [];
if (arrMin !== undefined) arrParts.push(`min: ${arrMin}`);
if (arrMax !== undefined) arrParts.push(`max: ${arrMax}`);
Expand All @@ -345,12 +347,14 @@ export function getMockScalar({
}

case 'string': {
const strMin = (item.minLength ?? safeMockOptions.stringMin) as
| number
| undefined;
const strMax = (item.maxLength ?? safeMockOptions.stringMax) as
| number
| undefined;
const strMin = (item.minLength ??
(item.maxLength === undefined
? safeMockOptions.stringMin
: undefined)) as number | undefined;
const strMax = (item.maxLength ??
(item.minLength === undefined
? safeMockOptions.stringMax
: undefined)) as number | undefined;
const strLenParts: string[] = [];
if (strMin !== undefined) strLenParts.push(`min: ${strMin}`);
if (strMax !== undefined) strLenParts.push(`max: ${strMax}`);
Expand Down
82 changes: 82 additions & 0 deletions tests/__snapshots__/mock/mock-constraints/endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Generated by orval v8.6.2 🍺
* Do not edit manually.
* Mock Constraints
* Test one-sided string and array constraints for MSW mock generation
* OpenAPI spec version: 1.0.0
*/
import axios from 'axios';
import type { AxiosRequestConfig, AxiosResponse } from 'axios';

import type { ConstrainedItem } from './model';

import { faker } from '@faker-js/faker';

import { HttpResponse, http } from 'msw';
import type { RequestHandlerOptions } from 'msw';

/**
* @summary Get items with one-sided constraints
*/
export const getItems = (
options?: AxiosRequestConfig,
): Promise<AxiosResponse<ConstrainedItem>> => {
return axios.get(`/items`, options);
};

export type GetItemsResult = AxiosResponse<ConstrainedItem>;

export const getGetItemsResponseMock = (
overrideResponse: Partial<Extract<ConstrainedItem, object>> = {},
): ConstrainedItem => ({
maxLengthOnly: faker.helpers.arrayElement([
faker.string.alpha({ length: { max: 5 } }),
undefined,
]),
minLengthOnly: faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 30 } }),
undefined,
]),
maxItemsOnly: faker.helpers.arrayElement([
Array.from({ length: faker.number.int({ max: 2 }) }, (_, i) => i + 1).map(
() => faker.string.alpha({ length: { min: 10, max: 20 } }),
),
undefined,
]),
minItemsOnly: faker.helpers.arrayElement([
Array.from({ length: faker.number.int({ min: 5 }) }, (_, i) => i + 1).map(
() => faker.string.alpha({ length: { min: 10, max: 20 } }),
),
undefined,
]),
bothBounds: faker.helpers.arrayElement([
faker.string.alpha({ length: { min: 2, max: 8 } }),
undefined,
]),
...overrideResponse,
});

export const getGetItemsMockHandler = (
overrideResponse?:
| ConstrainedItem
| ((
info: Parameters<Parameters<typeof http.get>[1]>[0],
) => Promise<ConstrainedItem> | ConstrainedItem),
options?: RequestHandlerOptions,
) => {
return http.get(
'*/items',
async (info: Parameters<Parameters<typeof http.get>[1]>[0]) => {
return HttpResponse.json(
overrideResponse !== undefined
? typeof overrideResponse === 'function'
? await overrideResponse(info)
: overrideResponse
: getGetItemsResponseMock(),
{ status: 200 },
);
},
options,
);
};
export const getMockConstraintsMock = () => [getGetItemsMockHandler()];
23 changes: 23 additions & 0 deletions tests/__snapshots__/mock/mock-constraints/model/constrainedItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Generated by orval v8.6.2 🍺
* Do not edit manually.
* Mock Constraints
* Test one-sided string and array constraints for MSW mock generation
* OpenAPI spec version: 1.0.0
*/

export interface ConstrainedItem {
/** @maxLength 5 */
maxLengthOnly?: string;
/** @minLength 30 */
minLengthOnly?: string;
/** @maxItems 2 */
maxItemsOnly?: string[];
/** @minItems 5 */
minItemsOnly?: string[];
/**
* @minLength 2
* @maxLength 8
*/
bothBounds?: string;
}
9 changes: 9 additions & 0 deletions tests/__snapshots__/mock/mock-constraints/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Generated by orval v8.6.2 🍺
* Do not edit manually.
* Mock Constraints
* Test one-sided string and array constraints for MSW mock generation
* OpenAPI spec version: 1.0.0
*/

export * from './constrainedItem';
14 changes: 14 additions & 0 deletions tests/configs/mock.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,20 @@ export default defineConfig({
target: '../specifications/msw-mixed-content-each-status.yaml',
},
},
mockConstraints: {
output: {
target: '../generated/mock/mock-constraints/endpoints.ts',
schemas: '../generated/mock/mock-constraints/model',
mock: {
type: 'msw',
},
clean: true,
prettier: true,
},
input: {
target: '../specifications/mock-constraints.yaml',
},
},
mswMixedContentUnionVendor: {
output: {
target: '../generated/mock/msw-mixed-content-union-vendor/endpoints.ts',
Expand Down
49 changes: 49 additions & 0 deletions tests/specifications/mock-constraints.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
openapi: 3.1.0
info:
title: Mock Constraints
description: 'Test one-sided string and array constraints for MSW mock generation'
version: 1.0.0
tags:
- name: mock-constraints
description: One-sided constraint test endpoints
servers:
- url: http://localhost
paths:
/items:
get:
tags:
- mock-constraints
summary: Get items with one-sided constraints
operationId: getItems
responses:
200:
description: Successful Operation
content:
application/json:
schema:
$ref: '#/components/schemas/ConstrainedItem'
components:
schemas:
ConstrainedItem:
type: object
properties:
maxLengthOnly:
type: string
maxLength: 5
minLengthOnly:
type: string
minLength: 30
maxItemsOnly:
type: array
maxItems: 2
items:
type: string
minItemsOnly:
type: array
minItems: 5
items:
type: string
bothBounds:
type: string
minLength: 2
maxLength: 8
Loading