Skip to content

Commit

Permalink
fix(msw): correctly generate ref'd examples (#1459)
Browse files Browse the repository at this point in the history
Co-authored-by: Alfred Jonsson <[email protected]>
  • Loading branch information
AllieJonsson and Alfred Jonsson authored Jun 17, 2024
1 parent f580f5d commit f296491
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/mock/src/msw/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,12 @@ export const getResponsesMockDefinition = ({
context.output.override?.mock?.useExamples ||
mockOptions?.useExamples
) {
const exampleValue =
let exampleValue =
example ||
originalSchema?.example ||
Object.values(examples || {})[0]?.value ||
Object.values(examples || {})[0] ||
originalSchema?.examples?.[0];
exampleValue = exampleValue?.value ?? exampleValue;
if (exampleValue) {
acc.definitions.push(
transformer
Expand Down
18 changes: 18 additions & 0 deletions tests/configs/swr.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,22 @@ export default defineConfig({
},
},
},
examples: {
output: {
target: '../generated/swr/examples/endpoints.ts',
schemas: '../generated/swr/examples/model',
client: 'swr',
mock: {
generateEachHttpStatus: true,
type: 'msw',
useExamples: true,
},
},
input: {
target: '../specifications/examples.yaml',
override: {
transformer: '../transformers/add-version.js',
},
},
},
});
50 changes: 50 additions & 0 deletions tests/specifications/examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
openapi: 3.0.0
info:
title: Sample API
version: 0.0.0
license:
name: MIT
url: https://opensource.org/licenses/MIT

paths:
/users:
get:
operationId: 'get-users'
responses:
'200':
description: A JSON array of user names
content:
application/json:
schema:
type: array
items:
type: string
examples:
default:
$ref: '#/components/examples/GetUsersResponseExample'
/users2:
get:
operationId: 'get-users-2'
responses:
'200':
description: A JSON array of user names
content:
application/json:
schema:
type: array
items:
type: string
examples:
default:
value:
- userA
- userB
- userC

components:
examples:
GetUsersResponseExample:
value:
- userD
- userE
- userF

0 comments on commit f296491

Please sign in to comment.