Skip to content

Commit

Permalink
fix(swr): error type is correct even when it's the same type as succe…
Browse files Browse the repository at this point in the history
…ss type (#1276)

Co-authored-by: Alfred Jonsson <alfred.jonsson@decerno.se>
  • Loading branch information
AffeJonsson and Alfred Jonsson committed Mar 21, 2024
1 parent 5fe09c3 commit 6e521b5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/core/src/getters/res-req-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { keyword } from 'esutils';
import type { ValueIteratee } from 'lodash';
import uniqBy from 'lodash.uniqby';

import {
MediaTypeObject,
ReferenceObject,
Expand Down Expand Up @@ -48,6 +50,7 @@ export const getResReqTypes = (
name: string,
context: ContextSpecs,
defaultType = 'unknown',
uniqueKey: ValueIteratee<ResReqTypesValue> = 'value',
): ResReqTypesValue[] => {
const typesArray = responsesOrRequests
.filter(([_, res]) => Boolean(res))
Expand Down Expand Up @@ -213,7 +216,7 @@ export const getResReqTypes = (

return uniqBy(
typesArray.flatMap((it) => it),
'value',
uniqueKey,
);
};

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/getters/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const getResponse = ({
operationName,
context,
'void',
(type) => type.key.startsWith('2') + type.value,
);

const filteredTypes = contentType
Expand Down
11 changes: 11 additions & 0 deletions tests/configs/swr.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,15 @@ export default defineConfig({
target: '../specifications/enums.yaml',
},
},
errors: {
output: {
target: '../generated/swr/errors/endpoints.ts',
schemas: '../generated/swr/errors/model',
client: 'swr',
mock: true,
},
input: {
target: '../specifications/errors.yaml',
},
},
});
37 changes: 37 additions & 0 deletions tests/specifications/errors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Swagger Errors
license:
name: MIT
paths:
/error:
get:
summary: Get an Error Item
operationId: createItems
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/Item'
'400':
description: Error
content:
application/json:
schema:
$ref: '#/components/schemas/Item'
default:
description: Error
content:
application/json:
schema:
$ref: '#/components/schemas/Item'
components:
schemas:
Item:
type: object
properties:
prop:
type: string

0 comments on commit 6e521b5

Please sign in to comment.