forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C++][Pistache] Compile error when nesting references (OpenAPITools#1…
…6711) (OpenAPITools#18586) When components/schema/<object> reference other objects, validate() was getting called on the referenced objects with no arguments. The return value was void, but checked as if it was a boolean value.
- Loading branch information
Showing
31 changed files
with
1,233 additions
and
374 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
bin/configs/cpp-pistache-server-cpp-pistache-nested-schema-refs.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
generatorName: cpp-pistache-server | ||
outputDir: samples/server/petstore/cpp-pistache-nested-schema-refs | ||
inputSpec: modules/openapi-generator/src/test/resources/3_0/nested-schema-refs.yaml | ||
templateDir: modules/openapi-generator/src/main/resources/cpp-pistache-server | ||
additionalProperties: | ||
useStructModel: "false" | ||
addExternalLibs: "true" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
modules/openapi-generator/src/test/resources/3_0/nested-schema-refs.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
openapi: 3.0.0 | ||
info: | ||
version: 1.0.0 | ||
title: Test swagger file | ||
|
||
paths: | ||
/pet: | ||
get: | ||
tags: | ||
- store | ||
operationId: getNestedObject | ||
responses: | ||
'200': | ||
description: successful operation | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/outerType' | ||
components: | ||
schemas: | ||
outerType: | ||
type: object | ||
required: | ||
- middle | ||
properties: | ||
middle: | ||
$ref: '#/components/schemas/middleType' | ||
middleType: | ||
type: object | ||
required: | ||
- inner | ||
properties: | ||
inner: | ||
$ref: '#/components/schemas/innerType' | ||
innerType: | ||
type: string | ||
|
3 changes: 3 additions & 0 deletions
3
samples/server/petstore/cpp-pistache-nested-schema-refs/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
build/ | ||
external/ | ||
pistache/ |
23 changes: 23 additions & 0 deletions
23
samples/server/petstore/cpp-pistache-nested-schema-refs/.openapi-generator-ignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# OpenAPI Generator Ignore | ||
# Generated by openapi-generator https://github.com/openapitools/openapi-generator | ||
|
||
# Use this file to prevent files from being overwritten by the generator. | ||
# The patterns follow closely to .gitignore or .dockerignore. | ||
|
||
# As an example, the C# client generator defines ApiClient.cs. | ||
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line: | ||
#ApiClient.cs | ||
|
||
# You can match any string of characters against a directory, file or extension with a single asterisk (*): | ||
#foo/*/qux | ||
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux | ||
|
||
# You can recursively match patterns against a directory, file or extension with a double asterisk (**): | ||
#foo/**/qux | ||
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux | ||
|
||
# You can also negate patterns with an exclamation (!). | ||
# For example, you can ignore all files in a docs folder with the file extension .md: | ||
#docs/*.md | ||
# Then explicitly reverse the ignore rule for a single file: | ||
#!docs/README.md |
14 changes: 14 additions & 0 deletions
14
samples/server/petstore/cpp-pistache-nested-schema-refs/.openapi-generator/FILES
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CMakeLists.txt | ||
README.md | ||
api/ApiBase.h | ||
api/StoreApi.cpp | ||
api/StoreApi.h | ||
impl/StoreApiImpl.cpp | ||
impl/StoreApiImpl.h | ||
main-api-server.cpp | ||
model/Helpers.cpp | ||
model/Helpers.h | ||
model/MiddleType.cpp | ||
model/MiddleType.h | ||
model/OuterType.cpp | ||
model/OuterType.h |
1 change: 1 addition & 0 deletions
1
samples/server/petstore/cpp-pistache-nested-schema-refs/.openapi-generator/VERSION
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7.6.0-SNAPSHOT |
38 changes: 38 additions & 0 deletions
38
samples/server/petstore/cpp-pistache-nested-schema-refs/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
cmake_minimum_required (VERSION 3.2) | ||
|
||
project(api-server) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pg -g3" ) | ||
|
||
include(ExternalProject) | ||
|
||
set(EXTERNAL_INSTALL_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/external) | ||
|
||
ExternalProject_Add(PISTACHE | ||
GIT_REPOSITORY https://github.com/pistacheio/pistache.git | ||
BUILD_IN_SOURCE true | ||
INSTALL_COMMAND meson setup build --prefix=${EXTERNAL_INSTALL_LOCATION} --libdir=lib && meson install -C build | ||
) | ||
|
||
ExternalProject_Add(NLOHMANN | ||
GIT_REPOSITORY https://github.com/nlohmann/json.git | ||
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DJSON_BuildTests=OFF | ||
) | ||
|
||
include_directories(${EXTERNAL_INSTALL_LOCATION}/include) | ||
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib) | ||
|
||
include_directories(model) | ||
include_directories(api) | ||
include_directories(impl) | ||
|
||
file(GLOB SRCS | ||
${CMAKE_CURRENT_SOURCE_DIR}/api/*.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/impl/*.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/model/*.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp | ||
) | ||
|
||
add_executable(${PROJECT_NAME} ${SRCS} ) | ||
add_dependencies(${PROJECT_NAME} PISTACHE NLOHMANN) | ||
target_link_libraries(${PROJECT_NAME} pistache pthread) |
48 changes: 48 additions & 0 deletions
48
samples/server/petstore/cpp-pistache-nested-schema-refs/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# REST API Server for Test swagger file | ||
|
||
## Overview | ||
This API Server was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. | ||
It uses the [Pistache](https://github.com/oktal/pistache) Framework. | ||
|
||
## Files organization | ||
The Pistache C++ REST server generator creates three folders: | ||
- `api`: This folder contains the handlers for each method specified in the OpenAPI definition. Every handler extracts | ||
the path and body parameters (if any) from the requests and tries to parse and possibly validate them. | ||
Once this step is completed, the main API class calls the corresponding abstract method that should be implemented | ||
by the developer (a basic implementation is provided under the `impl` folder) | ||
- `impl`: As written above, the implementation folder contains, for each API, the corresponding implementation class, | ||
which extends the main API class and implements the abstract methods. | ||
Every method receives the path and body parameters as constant reference variables and a reference to the response | ||
object, that should be filled with the right response and sent at the end of the method with the command: | ||
response.send(returnCode, responseBody, [mimeType]) | ||
- `model`: This folder contains the corresponding class for every object schema found in the OpenAPI specification. | ||
|
||
The main folder contains also a file with a main that can be used to start the server. | ||
Of course, is you should customize this file based on your needs | ||
|
||
## Installation | ||
First of all, you need to download and install the libraries listed [here](#libraries-required). | ||
|
||
Once the libraries are installed, in order to compile and run the server please follow the steps below: | ||
```bash | ||
mkdir build | ||
cd build | ||
cmake .. | ||
make | ||
``` | ||
|
||
Once compiled run the server: | ||
|
||
```bash | ||
cd build | ||
./api-server | ||
``` | ||
|
||
## Libraries required | ||
- [pistache](http://pistache.io/quickstart) | ||
- [JSON for Modern C++](https://github.com/nlohmann/json/#integration): Please download the `json.hpp` file and | ||
put it under the model/nlohmann folder | ||
|
||
## Namespaces | ||
org.openapitools.server.api | ||
org.openapitools.server.model |
39 changes: 39 additions & 0 deletions
39
samples/server/petstore/cpp-pistache-nested-schema-refs/api/ApiBase.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Test swagger file | ||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | ||
* | ||
* The version of the OpenAPI document: 1.0.0 | ||
* | ||
* | ||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* https://openapi-generator.tech | ||
* Do not edit the class manually. | ||
*/ | ||
/* | ||
* ApiBase.h | ||
* | ||
* Generalization of the Api classes | ||
*/ | ||
|
||
#ifndef ApiBase_H_ | ||
#define ApiBase_H_ | ||
|
||
#include <pistache/router.h> | ||
#include <memory> | ||
|
||
namespace org::openapitools::server::api | ||
{ | ||
|
||
class ApiBase { | ||
public: | ||
explicit ApiBase(const std::shared_ptr<Pistache::Rest::Router>& rtr) : router(rtr) {}; | ||
virtual ~ApiBase() = default; | ||
virtual void init() = 0; | ||
|
||
protected: | ||
const std::shared_ptr<Pistache::Rest::Router> router; | ||
}; | ||
|
||
} // namespace org::openapitools::server::api | ||
|
||
#endif /* ApiBase_H_ */ |
86 changes: 86 additions & 0 deletions
86
samples/server/petstore/cpp-pistache-nested-schema-refs/api/StoreApi.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* Test swagger file | ||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | ||
* | ||
* The version of the OpenAPI document: 1.0.0 | ||
* | ||
* | ||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* https://openapi-generator.tech | ||
* Do not edit the class manually. | ||
*/ | ||
|
||
#include "StoreApi.h" | ||
#include "Helpers.h" | ||
|
||
namespace org::openapitools::server::api | ||
{ | ||
|
||
using namespace org::openapitools::server::helpers; | ||
using namespace org::openapitools::server::model; | ||
|
||
const std::string StoreApi::base = ""; | ||
|
||
StoreApi::StoreApi(const std::shared_ptr<Pistache::Rest::Router>& rtr) | ||
: ApiBase(rtr) | ||
{ | ||
} | ||
|
||
void StoreApi::init() { | ||
setupRoutes(); | ||
} | ||
|
||
void StoreApi::setupRoutes() { | ||
using namespace Pistache::Rest; | ||
|
||
Routes::Get(*router, base + "/pet", Routes::bind(&StoreApi::get_nested_object_handler, this)); | ||
|
||
// Default handler, called when a route is not found | ||
router->addCustomHandler(Routes::bind(&StoreApi::store_api_default_handler, this)); | ||
} | ||
|
||
std::pair<Pistache::Http::Code, std::string> StoreApi::handleParsingException(const std::exception& ex) const noexcept | ||
{ | ||
try { | ||
throw; | ||
} catch (nlohmann::detail::exception &e) { | ||
return std::make_pair(Pistache::Http::Code::Bad_Request, e.what()); | ||
} catch (org::openapitools::server::helpers::ValidationException &e) { | ||
return std::make_pair(Pistache::Http::Code::Bad_Request, e.what()); | ||
} catch (std::exception &e) { | ||
return std::make_pair(Pistache::Http::Code::Internal_Server_Error, e.what()); | ||
} | ||
} | ||
|
||
std::pair<Pistache::Http::Code, std::string> StoreApi::handleOperationException(const std::exception& ex) const noexcept | ||
{ | ||
return std::make_pair(Pistache::Http::Code::Internal_Server_Error, ex.what()); | ||
} | ||
|
||
void StoreApi::get_nested_object_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) { | ||
try { | ||
|
||
|
||
try { | ||
this->get_nested_object(response); | ||
} catch (Pistache::Http::HttpError &e) { | ||
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what()); | ||
return; | ||
} catch (std::exception &e) { | ||
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e); | ||
response.send(errorInfo.first, errorInfo.second); | ||
return; | ||
} | ||
|
||
} catch (std::exception &e) { | ||
response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); | ||
} | ||
|
||
} | ||
|
||
void StoreApi::store_api_default_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) { | ||
response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist"); | ||
} | ||
|
||
} // namespace org::openapitools::server::api | ||
|
77 changes: 77 additions & 0 deletions
77
samples/server/petstore/cpp-pistache-nested-schema-refs/api/StoreApi.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Test swagger file | ||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | ||
* | ||
* The version of the OpenAPI document: 1.0.0 | ||
* | ||
* | ||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* https://openapi-generator.tech | ||
* Do not edit the class manually. | ||
*/ | ||
/* | ||
* StoreApi.h | ||
* | ||
* | ||
*/ | ||
|
||
#ifndef StoreApi_H_ | ||
#define StoreApi_H_ | ||
|
||
|
||
#include "ApiBase.h" | ||
|
||
#include <pistache/http.h> | ||
#include <pistache/router.h> | ||
#include <pistache/http_headers.h> | ||
|
||
#include <optional> | ||
#include <utility> | ||
|
||
#include "OuterType.h" | ||
|
||
namespace org::openapitools::server::api | ||
{ | ||
|
||
class StoreApi : public ApiBase { | ||
public: | ||
explicit StoreApi(const std::shared_ptr<Pistache::Rest::Router>& rtr); | ||
~StoreApi() override = default; | ||
void init() override; | ||
|
||
static const std::string base; | ||
|
||
private: | ||
void setupRoutes(); | ||
|
||
void get_nested_object_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); | ||
void store_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); | ||
|
||
/// <summary> | ||
/// Helper function to handle unexpected Exceptions during Parameter parsing and validation. | ||
/// May be overridden to return custom error formats. This is called inside a catch block. | ||
/// Important: When overriding, do not call `throw ex;`, but instead use `throw;`. | ||
/// </summary> | ||
virtual std::pair<Pistache::Http::Code, std::string> handleParsingException(const std::exception& ex) const noexcept; | ||
|
||
/// <summary> | ||
/// Helper function to handle unexpected Exceptions during processing of the request in handler functions. | ||
/// May be overridden to return custom error formats. This is called inside a catch block. | ||
/// Important: When overriding, do not call `throw ex;`, but instead use `throw;`. | ||
/// </summary> | ||
virtual std::pair<Pistache::Http::Code, std::string> handleOperationException(const std::exception& ex) const noexcept; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <remarks> | ||
/// | ||
/// </remarks> | ||
virtual void get_nested_object(Pistache::Http::ResponseWriter &response) = 0; | ||
|
||
}; | ||
|
||
} // namespace org::openapitools::server::api | ||
|
||
#endif /* StoreApi_H_ */ | ||
|
Oops, something went wrong.