AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::oas Namespace Reference

OpenAPI namespace. More...

Classes

struct  AdditionalProperties
 Schema additionalProperties value. More...
struct  Components
 OpenAPI 3.0 components object (schemas, parameters, request bodies and responses). More...
struct  GeneratedModels
 Generated models. More...
class  GeneratorError
 Models generation error. More...
struct  Info
 OpenAPI 3.0 info object. More...
struct  MediaTypeObject
 OpenAPI 3.0 media type object. More...
struct  ModelOptions
 Models generation options. More...
struct  OpenAPI
 OpenAPI 3.0 document (models and endpoints parts). More...
struct  Operation
 OpenAPI 3.0 operation object. More...
struct  Parameter
 OpenAPI 3.0 parameter object. More...
struct  PathItem
 OpenAPI 3.0 path item object. More...
struct  RequestBody
 OpenAPI 3.0 request body object. More...
struct  ResponseObject
 OpenAPI 3.0 response object. More...
struct  Scalar
 JSON scalar value (enumeration value). More...
struct  Schema
 OpenAPI 3.0 schema object. More...

Functions

anch::oas::GeneratedModels generateModels (const anch::oas::OpenAPI &api, const anch::oas::ModelOptions &options)
anch::oas::OpenAPI parse (std::istream &input)

Detailed Description

OpenAPI namespace.

It contains OpenAPI 3.0 document parsing and code generation (models).

Function Documentation

◆ generateModels()

anch::oas::GeneratedModels anch::oas::generateModels ( const anch::oas::OpenAPI & api,
const anch::oas::ModelOptions & options )

Generate C++ models and their anch-json mappers from the document schemas ( components.schemas ).

Mapping:

  • string → std::string ( anch::UUID for uuid format, anch::date::DateTime for date-time format, anch::date::Date for date format), integer → int32_t / int64_t , number → float / double , boolean → bool ;
  • objects with properties and allOf compositions → structures, inline objects get a name built from their path (<Model><Property>) ;
  • string enumerations → enum class (unknown values are rejected on deserialization) ;
  • array → std::vector , objects with additionalProperties schema only → std::map<std::string,T> ;
  • optional and nullable properties → std::optional , arrays and maps are empty when absent ;
  • required properties are registered with anch::val::required (absent or not nullable null value is refused on deserialization) ;
  • minLength , maxLength , minimum , maximum (exclusive or not), multipleOf , minItems , maxItems , uniqueItems , minProperties , maxProperties and non string enum → anch-json value constraints (array items and map values constraints with anch::val::items ) ;
  • pattern → anch::val::pattern (invalid ECMAScript patterns are ignored with a warning), string format (which is not mapped to a type) → anch::val::format ;
  • models have a default operator== ;
  • recursive references → std::shared_ptr ;
  • other schemas (primitives, arrays, maps) → aliases ( using ).

Not supported (warning, or error in strict mode): oneOf / anyOf , free-form objects, nested containers (array of arrays, ...), references outside #/components/schemas .

With server and client options, anch-rest endpoints are generated from the document paths: service interface, its registration in anch::rest::Api and typed client (see endpoints generation).

Parameters
apithe OpenAPI document
optionsthe generation options
Returns
the generated header, source and warnings
Exceptions
anch::oas::GeneratorErrorinvalid options, unknown reference or unsupported schema in strict mode
Here is the call graph for this function:

◆ parse()

anch::oas::OpenAPI anch::oas::parse ( std::istream & input)

Parse OpenAPI 3.0 document (JSON format). Unknown keywords are ignored.

Parameters
inputthe input stream to read
Returns
the document
Exceptions
anch::json::MappingErrorinvalid JSON or document structure
std::invalid_argumentthe document is not an OpenAPI 3.0 document
Here is the call graph for this function: