OpenAPITools / OpenAPITools/openapi-generator

[BUG] [C++] [cpp-restsdk] OneOf Compilation with GCC failed

Open
#19,574 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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

Cpp Code with OneOfs on the property level within another schema can be generated with cpp-restsdk, but building with GCC produces the following errors:

------
 > [build 1/1] RUN cmake -DCMAKE_CXX_COMPILER=g++-13           -DCMAKE_BUILD_TYPE=Release           -DCMAKE_INSTALL_PREFIX=/tmp/install /tmp/source     && cmake --build . --target install -j$(nproc):
0.593 -- The CXX compiler identification is GNU 13.1.0
0.636 -- Detecting CXX compiler ABI info
1.215 -- Detecting CXX compiler ABI info - done
1.255 -- Check for working CXX compiler: /usr/bin/g++-13 - skipped
1.256 -- Detecting CXX compile features
1.257 -- Detecting CXX compile features - done
1.275 -- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11")
1.282 -- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libcrypto.so (found version "3.0.2")
1.283 CMake Warning (dev) at /usr/local/lib/python3.10/dist-packages/cmake/data/share/cmake-3.30/Modules/CMakeFindDependencyMacro.cmake:76 (find_package):
1.283   Policy CMP0167 is not set: The FindBoost module is removed.  Run "cmake
1.283   --help-policy CMP0167" for policy details.  Use the cmake_policy command to
1.283   set the policy and suppress this warning.
1.283 
1.283 Call Stack (most recent call first):
1.283   /usr/lib/x86_64-linux-gnu/cmake/cpprestsdk/cpprestsdk-config.cmake:20 (find_dependency)
1.283   CMakeLists.txt:38 (find_package)
1.283 This warning is for project developers.  Use -Wno-dev to suppress it.
1.283 
1.827 -- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.74.0/BoostConfig.cmake (found version "1.74.0") found components: random system thread filesystem chrono atomic date_time regex
1.830 CMake Warning (dev) at CMakeLists.txt:39 (find_package):
1.830   Policy CMP0167 is not set: The FindBoost module is removed.  Run "cmake
1.830   --help-policy CMP0167" for policy details.  Use the cmake_policy command to
1.830   set the policy and suppress this warning.
1.830 
1.830 This warning is for project developers.  Use -Wno-dev to suppress it.
1.830 
1.835 -- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.74.0/BoostConfig.cmake (found version "1.74.0")
1.840 -- Building client library for Linux/Unix
1.846 -- Configuring done (1.6s)
1.870 -- Generating done (0.0s)
1.871 -- Build files have been written to: /tmp/build
2.370 [  7%] Building CXX object CMakeFiles/example_client.dir/src/ApiException.cpp.o
2.377 [ 14%] Building CXX object CMakeFiles/example_client.dir/src/AnyType.cpp.o
2.378 [ 28%] Building CXX object CMakeFiles/example_client.dir/src/JsonBody.cpp.o
2.379 [ 35%] Building CXX object CMakeFiles/example_client.dir/src/HttpContent.cpp.o
2.379 [ 42%] Building CXX object CMakeFiles/example_client.dir/src/ApiClient.cpp.o
2.379 [ 42%] Building CXX object CMakeFiles/example_client.dir/src/ApiConfiguration.cpp.o
2.388 [ 50%] Building CXX object CMakeFiles/example_client.dir/src/model/MainClass_one_of_property.cpp.o
2.391 [ 57%] Building CXX object CMakeFiles/example_client.dir/src/ModelBase.cpp.o
2.394 [ 64%] Building CXX object CMakeFiles/example_client.dir/src/MultipartFormData.cpp.o
2.399 [ 71%] Building CXX object CMakeFiles/example_client.dir/src/model/MainClass.cpp.o
2.412 [ 78%] Building CXX object CMakeFiles/example_client.dir/src/Object.cpp.o
2.416 [ 85%] Building CXX object CMakeFiles/example_client.dir/src/model/OneOfClass.cpp.o
4.689 [ 92%] Building CXX object CMakeFiles/example_client.dir/src/model/OneOfClass2.cpp.o
5.990 In file included from /tmp/source/include/example_client/model/MainClass.h:22,
5.990                  from /tmp/source/src/model/MainClass.cpp:14:
5.990 /tmp/source/include/example_client/ModelBase.h: In instantiation of ‘static bool example::model::ModelBase::fromJson(const web::json::value&, std::shared_ptr<_Tp>&) [with T = example::model::MainClass_one_of_property]’:
5.990 /tmp/source/src/model/MainClass.cpp:58:38:   required from here
5.990 /tmp/source/include/example_client/ModelBase.h:285:30: error: no matching function for call to ‘example::model::MainClass_one_of_property::fromJson(const web::json::value&)’
5.990   285 |         ok = outVal->fromJson(val);
5.990       |              ~~~~~~~~~~~~~~~~^~~~~
5.992 In file included from /tmp/source/include/example_client/model/MainClass.h:24:
5.992 /tmp/source/include/example_client/model/MainClass_one_of_property.h:47:10: note: candidate: ‘template<class Target> bool example::model::MainClass_one_of_property::fromJson(const web::json::value&)’
5.992    47 |     bool fromJson(const web::json::value& json) {
5.992       |          ^~~~~~~~
5.992 /tmp/source/include/example_client/model/MainClass_one_of_property.h:47:10: note:   template argument deduction/substitution failed:
5.994 /tmp/source/include/example_client/ModelBase.h:285:30: note:   couldn’t deduce template parameter ‘Target’
5.994   285 |         ok = outVal->fromJson(val);
5.994       |              ~~~~~~~~~~~~~~~~^~~~~
6.167 /tmp/source/include/example_client/ModelBase.h: In instantiation of ‘static bool example::model::ModelBase::fromString(const utility::string_t&, std::shared_ptr<_Tp>&) [with T = example::model::MainClass_one_of_property; utility::string_t = std::__cxx11::basic_string<char>]’:
6.167 /tmp/source/include/example_client/ModelBase.h:375:22:   required from ‘static bool example::model::ModelBase::fromHttpContent(std::shared_ptr<example::model::HttpContent>, std::shared_ptr<_Tp>&) [with T = example::model::MainClass_one_of_property]’
6.167 /tmp/source/src/model/MainClass.cpp:90:41:   required from here
6.167 /tmp/source/include/example_client/ModelBase.h:271:30: error: no matching function for call to ‘example::model::MainClass_one_of_property::fromJson(web::json::value)’
6.167   271 |         ok = outVal->fromJson(web::json::value::parse(val));
6.167       |              ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6.167 /tmp/source/include/example_client/model/MainClass_one_of_property.h:47:10: note: candidate: ‘template<class Target> bool example::model::MainClass_one_of_property::fromJson(const web::json::value&)’
6.167    47 |     bool fromJson(const web::json::value& json) {
6.167       |          ^~~~~~~~
6.167 /tmp/source/include/example_client/model/MainClass_one_of_property.h:47:10: note:   template argument deduction/substitution failed:
6.167 /tmp/source/include/example_client/ModelBase.h:271:30: note:   couldn’t deduce template parameter ‘Target’
6.167   271 |         ok = outVal->fromJson(web::json::value::parse(val));
6.167       |              ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7.308 cc1plus: note: unrecognized command-line option ‘-Wno-unused-lambda-capture’ may have been intended to silence earlier diagnostics
7.331 gmake[2]: *** [CMakeFiles/example_client.dir/build.make:202: CMakeFiles/example_client.dir/src/model/MainClass.cpp.o] Error 1
7.331 gmake[2]: *** Waiting for unfinished jobs....
14.31 gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/example_client.dir/all] Error 2
14.31 gmake: *** [Makefile:136: all] Error 2
------

 1 warning found (use --debug to expand):
 - FromAsCasing: 'as' and 'FROM' keywords' casing do not match (line 1)
Dockerfile:32
--------------------
  31 |     
  32 | >>> RUN cmake -DCMAKE_CXX_COMPILER=${CXX_COMPILER} \
  33 | >>>           -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
  34 | >>>           -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} /tmp/source \
  35 | >>>     && cmake --build . --target install -j$(nproc)
  36 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c cmake -DCMAKE_CXX_COMPILER=${CXX_COMPILER}           -DCMAKE_BUILD_TYPE=${BUILD_TYPE}           -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} /tmp/source     && cmake --build . --target install -j$(nproc)" did not complete successfully: exit code: 2
openapi-generator version

Tried with latest docker image (v.7.8.0).

OpenAPI declaration file content or url

This is the minimal example that does not build (minimal_failing_example.yaml):

openapi: 3.0.0
info:
  title: Title
  version: 0.0.1
paths: {}
components:
  schemas:
    MainClass:
      properties:
        one_of_property:
          oneOf:
          - $ref: '#/components/schemas/OneOfClass'
          - $ref: '#/components/schemas/OneOfClass2'
    OneOfClass:
      type: object
      properties:
        uuid:
          type: string
    OneOfClass2:
      type: object
      properties:
        uuid2:
          type: string

While this is a minimal example of a oneOf on the schema level that does build (minimal_succeeding_example.yaml):

openapi: 3.0.0
info:
  title: Title
  version: 0.0.1
paths: {}
components:
  schemas:
    MainClass:
        oneOf:
        - $ref: '#/components/schemas/OneOfClass'
        - $ref: '#/components/schemas/OneOfClass2'
    OneOfClass:
      type: object
      properties:
        uuid:
          type: string
    OneOfClass2:
      type: object
      properties:
        uuid2:
          type: string
Generation Details

In Unix shell I applied the following commands to generate the code and compile it with GCC.

docker pull openapitools/openapi-generator-cli:latest;
dir=/oneOfExample/
rm -rf .$dir
docker run -v "$(pwd):/local" openapitools/openapi-generator-cli:latest generate \
    --minimal-update \
    -i /local/minimal_failing_example.yaml \
    -g cpp-restsdk -c /local/generator_options.yml \
    -o /local$dir \
    --additional-properties=generateAliasAsModel=true;
docker build --target build -t openapi-test . 

with the following generator options (generator_options.yml):

packageName: example_client
apiPackage: example.api
modelPackage: example.model
packageVersion: 0.0.1

This is the Dockerfile to build the C++ code with GCC (Dockerfile):

FROM --platform=linux/amd64 ubuntu:22.04 as setup

ENV DEBIAN_FRONTEND=noninteractive

#install python and pip
RUN apt-get update -y \
    && apt-get install -y python3.10 \
    && apt-get install -y python3-pip 

#install gcc11 and cmake
RUN apt-get update \
    && apt-get install -y software-properties-common \
    && add-apt-repository -y ppa:ubuntu-toolchain-r/test \
    && apt-get install -y gcc-13 \
    && apt-get install -y g++-13

RUN pip install cmake==3.30.2

RUN apt-get -y install libcpprest-dev

WORKDIR /tmp/build
COPY ./oneOfExample /tmp/source


FROM setup AS build

ARG CXX_COMPILER=g++-13
ARG BUILD_TYPE=Release
ARG INSTALL_PREFIX=/tmp/install

RUN cmake -DCMAKE_CXX_COMPILER=${CXX_COMPILER} \
          -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
          -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} /tmp/source \
    && cmake --build . --target install -j$(nproc)

Steps to reproduce
  1. Generate C++ Code from the openapi document "minimal_failing_example.yaml" with the latest master.
  2. Build with GCC -> this leads to the above-mentioned errors.
Related issues/PRs

This issue exists since v7.7.0.
Prior to v7.7.0 code generation and building does not lead to errors, but oneOf properties are not generated correctly.
In v7.6.0 oneOf properties are essentially generated as if they were in an allOf.

Issue #4239 was suggested to be due to inline-schema handling, which this example does not use.

Issue #6378 describes different faulty behavior in connection with oneOf, allOf and anyOf.

After the before-mentioned issues this fix for oneOfs introducing std::variant was introduced in v7.7.0 which has fixed some cases (see succeeding example) but leads to errors in others (see failing example).

Suggest a fix

The issue seems to be linked to 'from_json' function, which seems to be called without the template parameter.

OneOfs do work when they are on the highest level in a schema definition, which may point towards a possible solution.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running generate.sh with minimal_failing_example.yaml and generator_options.yml, then build the generated cpp-restsdk client with Dockerfile. Inspect the generated MainClass.cpp, MainClass_one_of_property.h, and ModelBase.h around the reported fromJson calls. Done means the failing property-level oneOf example compiles successfully with GCC while the schema-level example continues to build.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp, docker
Domain
build-system, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.