Skip to content

Commit 36fc899

Browse files
authored
feat: add missing required state for query param with $ref (#1308)
1 parent 656223e commit 36fc899

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

.changeset/six-aliens-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"swagger-typescript-api": patch
3+
---
4+
5+
add missing required state for query param with $ref

src/schema-routes/schema-routes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ export class SchemaRoutes {
206206
lodash.each(parameters, (parameter) => {
207207
const refTypeInfo =
208208
this.schemaParserFabric.schemaUtils.getSchemaRefType(parameter);
209+
209210
let routeParam = null;
210211

211212
if (refTypeInfo?.rawTypeData.in && refTypeInfo.rawTypeData) {
@@ -217,6 +218,10 @@ export class SchemaRoutes {
217218
...refTypeInfo.rawTypeData,
218219
...(refTypeInfo.rawTypeData.schema || {}),
219220
};
221+
222+
if (parameter?.required && !routeParam.required) {
223+
routeParam.required = parameter.required;
224+
}
220225
} else {
221226
if (!parameter.in) return;
222227

tests/spec/propertyNames/__snapshots__/basic.test.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,11 @@ export type FooBarBazNoAdditionalPropertiesPrimitive = object;
4444
export interface FooBarBazNested {
4545
dataCount?: Record<KekEnum, number>;
4646
}
47+
48+
export type ProductId = string;
49+
50+
export interface GetProductsParams {
51+
productId: ProductId;
52+
}
4753
"
4854
`;

tests/spec/propertyNames/basic.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ describe("basic", async () => {
2424
output: tmpdir,
2525
silent: true,
2626
generateClient: false,
27-
generateRouteTypes: true,
27+
generateRouteTypes: false,
28+
extractRequestBody: true,
29+
extractRequestParams: true,
2830
});
2931

3032
const content = await fs.readFile(path.join(tmpdir, "schema.ts"), {

tests/spec/propertyNames/schema.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ info:
55
security:
66
- registryCookieAuth: []
77
- cookieAuth: [ ]
8+
paths:
9+
/api/v1/products:
10+
get:
11+
operationId: getProducts
12+
tags:
13+
- Products
14+
summary: ASDfdsafdsafdsaf
15+
parameters:
16+
- in: query
17+
required: true
18+
$ref: '#/components/parameters/ProductIdQueryParam'
19+
responses:
20+
'200':
21+
description: KO
822
components:
923
schemas:
1024
FooBarBazRoot:
@@ -53,3 +67,32 @@ components:
5367
- dadd
5468
- xzxzxz
5569
- axzxe
70+
ProductId:
71+
type: string
72+
parameters:
73+
LimitQueryParam:
74+
name: limit
75+
in: query
76+
schema:
77+
type: integer
78+
OffsetQueryParam:
79+
name: offset
80+
in: query
81+
schema:
82+
type: integer
83+
ProductIdPathParam:
84+
name: productId
85+
in: path
86+
required: true
87+
schema:
88+
$ref: '#/components/schemas/ProductId'
89+
ProductIdQueryParam:
90+
name: productId
91+
in: query
92+
schema:
93+
$ref: '#/components/schemas/ProductId'
94+
SearchQueryParam:
95+
name: search
96+
in: query
97+
schema:
98+
type: string

0 commit comments

Comments
 (0)