Created by: JulianGmp
This PR includes a variety of smaller changes, as well as implementing model validation for the Pistache C++ server.
List of changes:
-
Implemented validation of model data through mustache templates, this validation includes...
- enum values (both “inline” enums and enums as their own schema)
-
date
anddate-time
formatted strings as perfull-date
/date-time
in RFC3339 - string length (min and max) using
std::string::length
- numeric validations for
- min value (exclusive and non-exclusive)
- max value (exclusive and non-exclusive)
- multipleOf (Note this may cause issues for float/double, see https://swagger.io/docs/specification/data-models/data-types/ section “Multiples”)
- Array length (min and max)
- Unique array elements
- Recursive validation for array elements, this includes all checks mentioned above
-
Generate arrays with
uniqueItems: true
asstd::vector
s. The current implementation used a type calledSet
which did not exist and caused build issues. The standard vector was chosen above a set to ensure the order of items remains intact (see https://github.com/OpenAPITools/openapi-generator/pull/5466#issuecomment-593305650); -
Implement the
==
operator (and!=
) for model classes. This is needed for checking if an array only has unique elements. -
Change get functions for array type. They now just return a vector, instead of a const reference to the vector. This copies the underlying vector, which is expensive. My justification is: returning a const reference may still allow the caller to modify the individual elements of the vector, thus the get function may not be declared as
const
. This currently causes the issue that you have to use a const cast in your API impl, since the model is always passed by const reference. By copying the vector the get function can safely be declared as const, and the original data cannot be modified. -
Create virtual methods
handleParsingException
andhandleOperationException
for API classes. These will map a thrown exception to an HTTP status code and a response. The intended use is to overload these if needed, to ensure a certain return structure for error cases that are handled in the API class (such as a json parsing error). -
Made the API classes'
base
propertystatic const
. That way you can read the base path without having to create an instance of the API. -
Smaller visual changes, for example one line namespace declarations.
-
Minor cleanups, for example passing an
int32_t
by value, not by const reference.
Caveats
- The template possibly generates some dead code arrays, see the discussion in https://github.com/OpenAPITools/openapi-generator/pull/9251#discussion_r624503266.
- The template does not validate all string formats, currently only
date
anddate-time
. Custom regex checking could be possible, but has not been implemented in this PR, See #1170.
PR checklist
-
Read the contribution guidelines. -
Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community. -
Run the following to build the project and update samples: ./mvnw clean package ./bin/generate-samples.sh ./bin/utils/export_docs_generators.sh
./bin/generate-samples.sh bin/configs/java*
. For Windows users, please run the script in Git BASH. -
File the PR against the correct branch: master
,5.1.x
,6.0.x
-
If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
C++ technical committee: @ravinikam @stkrwork @etherealjoy @MartinDelille @muttleyxd