Skip to content

Commit 2b13247

Browse files
committed
fix(openapi-v3): set required to true for path parameters
#1531
1 parent abb1dc4 commit 2b13247

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

packages/openapi-v3/src/controller-spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ function resolveControllerSpec(constructor: Function): ControllerSpec {
121121
* }
122122
* ```
123123
*/
124-
operationSpec.parameters = params.filter(p => p != null);
124+
operationSpec.parameters = params.filter(p => p != null).map(p => {
125+
// Per OpenAPI spec, `required` must be `true` for path parameters
126+
if (p.in === 'path') {
127+
p.required = true;
128+
}
129+
return p;
130+
});
125131
}
126132

127133
debug(' processing requestBody for method %s', op);

packages/openapi-v3/test/integration/operation-spec.integration.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ describe('operation arguments', () => {
3737
parameters: [
3838
{name: 'type', in: 'query', schema: {type: 'string'}},
3939
{name: 'token', in: 'header', schema: {type: 'string'}},
40-
{name: 'location', in: 'path', schema: {type: 'string'}},
40+
{
41+
name: 'location',
42+
in: 'path',
43+
required: true,
44+
schema: {type: 'string'},
45+
},
4146
],
4247
requestBody: {
4348
content: {
@@ -60,6 +65,7 @@ describe('operation arguments', () => {
6065
},
6166
},
6267
};
63-
expect(getControllerSpec(MyController)).to.eql(expectedSpec);
68+
const spec = getControllerSpec(MyController);
69+
expect(spec).to.eql(expectedSpec);
6470
});
6571
});

packages/openapi-v3/test/unit/decorators/param/param-path.decorator.unit.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('Routing metadata for parameters', () => {
1616
const expectedParamSpec = {
1717
name: 'name',
1818
in: 'path',
19+
required: true,
1920
schema: {
2021
type: 'string',
2122
},
@@ -33,6 +34,7 @@ describe('Routing metadata for parameters', () => {
3334
const expectedParamSpec = {
3435
name: 'name',
3536
in: 'path',
37+
required: true,
3638
schema: {
3739
type: 'number',
3840
},
@@ -50,6 +52,7 @@ describe('Routing metadata for parameters', () => {
5052
const expectedParamSpec = {
5153
name: 'name',
5254
in: 'path',
55+
required: true,
5356
schema: {
5457
type: 'integer',
5558
format: 'int32',
@@ -72,6 +75,7 @@ describe('Routing metadata for parameters', () => {
7275
{
7376
name: 'name',
7477
in: 'path',
78+
required: true,
7579
schema: {
7680
type: 'boolean',
7781
},
@@ -89,6 +93,7 @@ describe('Routing metadata for parameters', () => {
8993
const expectedParamSpec = {
9094
name: 'name',
9195
in: 'path',
96+
required: true,
9297
schema: {
9398
type: 'integer',
9499
format: 'int64',
@@ -107,6 +112,7 @@ describe('Routing metadata for parameters', () => {
107112
const expectedParamSpec = {
108113
name: 'name',
109114
in: 'path',
115+
required: true,
110116
schema: {
111117
type: 'number',
112118
format: 'float',
@@ -125,6 +131,7 @@ describe('Routing metadata for parameters', () => {
125131
const expectedParamSpec = {
126132
name: 'name',
127133
in: 'path',
134+
required: true,
128135
schema: {
129136
type: 'number',
130137
format: 'double',
@@ -143,6 +150,7 @@ describe('Routing metadata for parameters', () => {
143150
const expectedParamSpec = {
144151
name: 'name',
145152
in: 'path',
153+
required: true,
146154
schema: {
147155
type: 'string',
148156
format: 'byte',
@@ -161,6 +169,7 @@ describe('Routing metadata for parameters', () => {
161169
const expectedParamSpec = {
162170
name: 'name',
163171
in: 'path',
172+
required: true,
164173
schema: {
165174
type: 'string',
166175
format: 'binary',
@@ -179,6 +188,7 @@ describe('Routing metadata for parameters', () => {
179188
const expectedParamSpec = {
180189
name: 'name',
181190
in: 'path',
191+
required: true,
182192
schema: {
183193
type: 'string',
184194
format: 'date',
@@ -197,6 +207,7 @@ describe('Routing metadata for parameters', () => {
197207
const expectedParamSpec = {
198208
name: 'name',
199209
in: 'path',
210+
required: true,
200211
schema: {
201212
type: 'string',
202213
format: 'date-time',
@@ -215,6 +226,7 @@ describe('Routing metadata for parameters', () => {
215226
const expectedParamSpec = {
216227
name: 'name',
217228
in: 'path',
229+
required: true,
218230
schema: {
219231
type: 'string',
220232
format: 'password',

packages/openapi-v3/test/unit/decorators/param/param.decorator.unit.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ describe('Routing metadata for parameters', () => {
4444
@param({
4545
name: 'id',
4646
in: 'path',
47+
required: true,
4748
})
4849
id: string,
4950
@param({
@@ -80,6 +81,7 @@ describe('Routing metadata for parameters', () => {
8081
type: 'string',
8182
},
8283
in: 'path',
84+
required: true,
8385
})
8486
.withParameter({
8587
name: 'name',

packages/rest/test/unit/parser.unit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('operationArgsParser', () => {
2929
name: 'id',
3030
type: 'number',
3131
in: 'path',
32+
required: true,
3233
},
3334
]);
3435
const route = givenResolvedRoute(spec, {id: 1});

0 commit comments

Comments
 (0)