Skip to content

Commit

Permalink
fix(msw): correctly add imports for enum references (#1456)
Browse files Browse the repository at this point in the history
* fix: add import for enum reference, fixes #1455

* fix: add tests to import enum reference
  • Loading branch information
dteske25 authored Jun 14, 2024
1 parent da1f789 commit f580f5d
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 4 deletions.
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

0 comments on commit f580f5d

Please sign in to comment.