Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(swr): error type is correct even when it's the same type as success type #1276

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
melloware marked this conversation as resolved.
Show resolved Hide resolved
);

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