diff --git a/compiler/src/model/build-model.ts b/compiler/src/model/build-model.ts index 7c87f19fd0..c3e196ffa7 100644 --- a/compiler/src/model/build-model.ts +++ b/compiler/src/model/build-model.ts @@ -285,6 +285,10 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int // validate body // the body can either be a value (eg Array or an object with properties) if (bodyValue != null) { + // Propagate required body value nature based on TS question token being present. + // Overrides the value set by spec files. + mapping.requestBodyRequired = !(bodyMember as PropertySignature).hasQuestionToken() + if (bodyValue.kind === 'instance_of' && bodyValue.type.name === 'Void') { assert(bodyMember as Node, false, 'There is no need to use Void in requets definitions, just remove the body declaration.') } else { @@ -299,6 +303,7 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int !type.path.map(p => p.codegenName ?? p.name).concat(type.query.map(p => p.codegenName ?? p.name)).includes(tags.codegen_name), `The codegen_name '${tags.codegen_name}' already exists as a property in the path or query.` ) + type.body = { kind: 'value', value: bodyValue, diff --git a/compiler/src/steps/validate-rest-spec.ts b/compiler/src/steps/validate-rest-spec.ts index ba811c6cba..35f9fdf8e4 100644 --- a/compiler/src/steps/validate-rest-spec.ts +++ b/compiler/src/steps/validate-rest-spec.ts @@ -122,6 +122,10 @@ export default async function validateRestSpec (model: model.Model, jsonSpec: Ma if (requestProperties.body === Body.noBody && spec.body != null && spec.body.required === true) { errors.addEndpointError(endpoint.name, 'request', `${endpoint.request.name}: should have a body definition`) } + + if (spec.body != null && spec.body.required === true && spec.body.required !== endpoint.requestBodyRequired) { + errors.addEndpointError(endpoint.name, 'request', ': should not be an optional body definition') + } } }