Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(msw): correctly add imports for enum references #1456

Merged
merged 2 commits into from
Jun 14, 2024
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
9 changes: 5 additions & 4 deletions packages/mock/src/faker/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,18 @@ export const getMockScalar = ({
`faker.number.int({min: ${item.minimum}, max: ${item.maximum}})`,
item.nullable,
);
let numberImports: GeneratorImport[] = [];
if (item.enum) {
// By default the value isn't a reference, so we don't have the object explicitly defined.
// So we have to create an array with the enum values and force them to be a const.
const joindEnumValues = item.enum.filter(Boolean).join(',');
const joinedEnumValues = item.enum.filter(Boolean).join(',');

let enumValue = `[${joindEnumValues}] as const`;
let enumValue = `[${joinedEnumValues}] as const`;

// But if the value is a reference, we can use the object directly via the imports and using Object.values.
if (item.isRef) {
enumValue = `Object.values(${item.name})`;
imports = [
numberImports = [
{
name: item.name,
values: true,
Expand All @@ -142,7 +143,7 @@ export const getMockScalar = ({
}
return {
value,
imports: [],
imports: numberImports,
name: item.name,
};
}
Expand Down
12 changes: 12 additions & 0 deletions tests/configs/mock.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,16 @@ export default defineConfig({
target: '../specifications/null-type.yaml',
},
},
enumRefs: {
output: {
mode: 'tags',
schemas: '../generated/mock/enumRefs/model',
target: '../generated/mock/enumRefs/endpoints.ts',
client: 'axios',
mock: true,
},
input: {
target: '../specifications/enum-refs.yaml',
},
},
});
115 changes: 115 additions & 0 deletions tests/specifications/enum-refs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
openapi: 3.0.1
info:
title: Sample API
description: 'A sample api'
version: '1.0'
paths:
'/sampleApi':
get:
tags:
- Sample
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/SampleStepsResponse'
components:
schemas:
SampleAccountStatus:
enum:
- 0
- 1
- 2
- 3
type: integer
format: int32
x-enumNames:
- Unknown
- NotStarted
- InProgress
- Completed
SampleStepResponse:
type: object
properties:
stepKey:
type: string
nullable: true
groupKey:
type: string
nullable: true
displayName:
type: string
nullable: true
description:
type: string
nullable: true
staffInstructions:
type: string
nullable: true
clientInstructions:
type: string
nullable: true
lastModifiedOn:
type: string
format: date-time
lastModifiedBy:
type: string
nullable: true
stepStatus:
$ref: '#/components/schemas/SampleStepStatus'
statusChanges:
type: array
items:
$ref: '#/components/schemas/SampleStepStatusChangeResponse'
nullable: true
additionalProperties: false
SampleStepStatus:
enum:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
type: integer
format: int32
x-enumNames:
- Unknown
- NotStarted
- InProgress
- Completed
- Skipped
- Waiting
- ReadyForReview
SampleStepStatusChangeResponse:
type: object
properties:
oldStatus:
$ref: '#/components/schemas/SampleStepStatus'
newStatus:
$ref: '#/components/schemas/SampleStepStatus'
stepKey:
type: string
nullable: true
modifiedOn:
type: string
format: date-time
modifiedBy:
type: string
nullable: true
additionalProperties: false
SampleStepsResponse:
type: object
properties:
organizationId:
type: string
format: uuid
steps:
type: array
items:
$ref: '#/components/schemas/SampleStepResponse'
nullable: true
additionalProperties: false