Skip to content

Commit

Permalink
fix(msw): fix broken mocks when using 'allOf' within 'anyOf' and usin…
Browse files Browse the repository at this point in the history
…g a shared schema (#1663)

* fix(msw): fix broken mocks when using 'allOf' within 'anyOf' and using a shared schema

* chore: add test case
  • Loading branch information
soartec-lab authored Oct 15, 2024
1 parent 86b63eb commit 87e1db6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/mock/src/faker/resolvers/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,25 @@ export const resolveMockValue = ({
isRef: true,
};

const newSeparator = newSchema.allOf
? 'allOf'
: newSchema.oneOf
? 'oneOf'
: 'anyOf';

const scalar = getMockScalar({
item: newSchema,
mockOptions,
operationId,
tags,
combine,
combine: combine
? {
separator:
combine.separator === 'anyOf' ? newSeparator : combine.separator,
includedProperties:
newSeparator === 'allOf' ? [] : combine.includedProperties,
}
: undefined,
context: {
...context,
specKey,
Expand Down Expand Up @@ -147,6 +160,10 @@ export const resolveMockValue = ({
});
}

if (scalar.value && newSchema.allOf && combine?.separator === 'anyOf') {
scalar.value = `{${scalar.value}}`;
}

return {
...scalar,
type: newSchema.type,
Expand Down
29 changes: 29 additions & 0 deletions tests/specifications/any-of.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Pet'
/any-of-included-all-of-and-shared-schema-pet:
get:
summary: Gets anyOf included allOf and shared schema pets
operationId: getAnyOfIncludedAllOfAndSharedSchemaPets
responses:
'200':
description: Pet
content:
application/json:
schema:
$ref: '#/components/schemas/CatDetail'
components:
schemas:
A:
Expand Down Expand Up @@ -68,3 +79,21 @@ components:
properties:
name:
type: string
CatDetail:
type: object
required:
- detail
properties:
detail:
anyOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/CatExpandDetail'
CatExpandDetail:
allOf:
- $ref: '#/components/schemas/Cat'
- type: object
required:
- url
properties:
url:
type: string

0 comments on commit 87e1db6

Please sign in to comment.