OpenAPITools / OpenAPITools/openapi-generator
[BUG][C++][Pistache] required field is meaningless in requestBodies
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Similar to https://github.com/OpenAPITools/openapi-generator/issues/5880 , required parameters in request bodies are not added to the validate function.
openapi-generator version
6.2.1
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: Test example
version: 0.0.1
paths:
/data:
put:
tags:
- Data
summary: My request
description: |-
Allows operator to import data.
operationId: importData
requestBody:
$ref: "#/components/requestBodies/ImportDataDTO"
responses:
"201":
description: |-
201 Data Imported
content:
application/json:
schema:
properties:
msg:
type: string
components:
schemas:
MyDataObj:
description: Coordinates
properties:
x:
type: number
format: double
y:
type: number
format: double
z:
type: number
format: double
required:
- x
- y
- z
requestBodies:
ImportDataDTO:
required: true
content:
application/json:
schema:
properties:
name:
type: string
dataDTO:
$ref: "#/components/schemas/MyDataObj"
required:
- name
- dataDTO
Generation Details
openapi-generator-cli generate -i example.yaml -g cpp-pistache-server
This will produce:
bool ImportData_request::validate(std::stringstream& msg, const std::string& pathPrefix) const
{
bool success = true;
const std::string _pathPrefix = pathPrefix.empty() ? "ImportData_request" : pathPrefix;
return success;
}
I would expect something along the lines of :
bool ImportData_request::validate(std::stringstream& msg, const std::string& pathPrefix) const
{
bool success = true;
const std::string _pathPrefix = pathPrefix.empty() ? "ImportData_request" : pathPrefix;
if (!m_DataDTO.validate()) {
msg << _pathPrefix << ": DataDTO is invalid;";
success = false;
}
return success;
}
Steps to reproduce
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/5880
Suggest a fix
I'm not familiar with the mustache syntax but modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache should be modified to something like:
diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache
index ad38f52d535..776049e6efc 100644
--- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache
+++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache
@@ -60,7 +60,11 @@ bool {{classname}}::validate(std::stringstream& msg, const std::string& pathPref
const std::string currentValuePath = _pathPrefix + ".{{nameInCamelCase}}";
{{> model-validation-body }}
}
- {{/hasValidation}}{{/isArray}}{{/vars}}{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}
+ {{/hasValidation}}{{#required}}{{#isModel}}
+ if (!m_{{name}}.validate()) {
+ msg << _pathPrefix << ": {{name}} is invalid;";
+ success = false;
+ }{{/isModel}}{{/required}}{{/isArray}}{{/vars}}{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}
if (!m_value.validate(msg))
{
success = false;
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the openapi-generator-cli command with the provided YAML and inspect modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache. Compare the generated ImportData_request::validate function with the expected output. Done means required model fields in the request body are validated and invalid data is reported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend-api-design, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100