Skip to content

Commit

Permalink
fix(msw): generated mocks for polymorphic type (orval-labs#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicBach authored Sep 5, 2022
1 parent 9ccbd2d commit 5748a40
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/core/getters/combine.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ export const combineSchemasMock = ({
? `${resolvedValue.value.slice(0, -1)},${itemResolvedValue.value}}`
: resolvedValue.value;

if (!index && !combine) {
const isObjectBounds =
!combine || (combine.separator === 'oneOf' && separator === 'allOf');

if (!index && isObjectBounds) {
if (resolvedValue.enums || separator === 'oneOf') {
if (arr.length === 1) {
return `faker.helpers.arrayElement([${value}])`;
Expand All @@ -99,7 +102,7 @@ export const combineSchemasMock = ({
}
return `${acc}${value}${
itemResolvedValue?.value ? `,${itemResolvedValue.value}` : ''
}${!combine ? '}' : ''}`;
}${isObjectBounds ? '}' : ''}`;
}
if (!value) {
return acc;
Expand Down
12 changes: 12 additions & 0 deletions tests/configs/react-query.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,16 @@ export default defineConfig({
target: '../specifications/petstore.yaml',
},
},
polymorphic: {
output: {
target: '../generated/react-query/polymorphic/endpoints.ts',
schemas: '../generated/react-query/polymorphic/model',
client: 'react-query',
mock: true,
headers: true,
},
input: {
target: '../specifications/polymorphic.yaml',
},
},
});
50 changes: 50 additions & 0 deletions tests/specifications/polymorphic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
openapi: 3.0.1
info:
title: API
version: V1
paths:
/demo:
get:
operationId: getPolymorphicResponse
responses:
'200':
description: ''
content:
application/json:
schema:
type: object
oneOf:
- $ref: '#/components/schemas/DescendantOne'
- $ref: '#/components/schemas/DescendantTwo'
components:
schemas:
DescendantOne:
type: object
allOf:
- $ref: '#/components/schemas/ParentType'
- type: object
properties:
value:
type: boolean
DescendantTwo:
type: object
allOf:
- $ref: '#/components/schemas/ParentType'
- type: object
properties:
value:
type: string
ParentType:
type: object
properties:
value:
type: object
key:
type: string
type:
type: string
enum:
- BOOLEAN
- STRING
discriminator:
propertyName: type

0 comments on commit 5748a40

Please sign in to comment.