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

Compiler: fix required body value #2463

Merged
merged 2 commits into from
Mar 25, 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: 5 additions & 0 deletions compiler/src/model/build-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int
// validate body
// the body can either be a value (eg Array<string> 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 {
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/steps/validate-rest-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}
}

Expand Down
Loading