Skip to content

Commit 0f41f4f

Browse files
authored
Compiler: fix required body value (#2463)
* propagate question mark for body values from the TS spec to prevent discrepancy with the json spec * make linter happy
1 parent a20d260 commit 0f41f4f

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

compiler/src/model/build-model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int
285285
// validate body
286286
// the body can either be a value (eg Array<string> or an object with properties)
287287
if (bodyValue != null) {
288+
// Propagate required body value nature based on TS question token being present.
289+
// Overrides the value set by spec files.
290+
mapping.requestBodyRequired = !(bodyMember as PropertySignature).hasQuestionToken()
291+
288292
if (bodyValue.kind === 'instance_of' && bodyValue.type.name === 'Void') {
289293
assert(bodyMember as Node, false, 'There is no need to use Void in requets definitions, just remove the body declaration.')
290294
} else {
@@ -299,6 +303,7 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int
299303
!type.path.map(p => p.codegenName ?? p.name).concat(type.query.map(p => p.codegenName ?? p.name)).includes(tags.codegen_name),
300304
`The codegen_name '${tags.codegen_name}' already exists as a property in the path or query.`
301305
)
306+
302307
type.body = {
303308
kind: 'value',
304309
value: bodyValue,

compiler/src/steps/validate-rest-spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ export default async function validateRestSpec (model: model.Model, jsonSpec: Ma
122122
if (requestProperties.body === Body.noBody && spec.body != null && spec.body.required === true) {
123123
errors.addEndpointError(endpoint.name, 'request', `${endpoint.request.name}: should have a body definition`)
124124
}
125+
126+
if (spec.body != null && spec.body.required === true && spec.body.required !== endpoint.requestBodyRequired) {
127+
errors.addEndpointError(endpoint.name, 'request', ': should not be an optional body definition')
128+
}
125129
}
126130
}
127131

0 commit comments

Comments
 (0)