OpenAPITools / OpenAPITools/openapi-generator

[C++-Qt] qHash: invalid initialization of reference in autogenerated code

Open
#17,739 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When generating code using the cpp-qt-client for Qt5 in version 6 or 7, compiling will lead to errors of type

[ 11%] Building CXX object CMakeFiles/QtOpenAPIClient.dir/OAIOrder.cpp.o
In file included from /cpp/client/OAIOrder_dimensions.h:25:0,
                 from /cpp/client/OAIOrder.h:23,
                 from /cpp/client/OAIOrder.cpp:12:
/cpp/client/OAIObject.h: In function 'uint OpenAPI::qHash(const OpenAPI::OAIObject&, uint)':
/cpp/client/OAIObject.h:72:34: error: invalid initialization of reference of type 'const OpenAPI::OAIObject&' from expression of type 'QJsonObject'
     return qHash(obj.asJsonObject(), seed);
                  ~~~~~~~~~~~~~~~~^~
/cpp/client/OAIObject.h:71:1: note: in passing argument 1 of 'uint OpenAPI::qHash(const OpenAPI::OAIObject&, uint)'
 qHash(const OAIObject& obj, uint seed = 0) noexcept{
 ^~~~~
make[2]: *** [CMakeFiles/QtOpenAPIClient.dir/build.make:76: CMakeFiles/QtOpenAPIClient.dir/OAIOrder.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/QtOpenAPIClient.dir/all] Error 2
openapi-generator version

I have experienced this with the openapi-generator-cli available in the openapitools/openapi-generator-cli:v6.6.0 and v7.2.0 images.
With v5.4.0 I got other errors preventing me from compiling.
I can confirm that v4.3.1 works with my configuration and setup.

I compile on openSUSE Leap 15.5 using CMake, and I am pretty sure I have all the required packages available.

OpenAPI declaration file content or url

I follow the steps described in the Qt example found at
https://www.qt.io/resources/videos/qt-and-openapi-swagger-a-tutorial-dev-des-2021, or
https://blog.basyskom.com/2021/qml-qt-quick-and-swagger-openapi-tutorial.

The specification file is:
https://github.com/basysKom/swagger_qt_demo/blob/182e70418779e07998d48a99ff7247f23ae7d183/swagger.yaml

Command line used for generation

To generate, I use

docker run --rm -v ${PWD}:/local/ openapitools/openapi-generator-cli:v7.2.0 generate -i /local/swagger.yaml -g cpp-qt-client -o /local/generated/cpp/
Steps to reproduce
  • Download the file from the demo (see above)
  • Generate the Qt code
  • Build a docker image based on openSUSE Leap 15.5, with make, cmake, and all the required Qt devel packages (most likely, the error also occurs with a different setup)
  • run the image with the generated code mounted
    docker run -ti -v ${PWD}/generated/cpp/:/cpp/ my-docker-image:15.5 /usr/bin/env bash
    
  • create a build directory
    mkdir $HOME/apibuild/
    cd $HOME/apibuild/
    
  • generate the C++ code, specify the Qt version (not sure how I am supposed to set $QT_VERSION_MAJOR else)
    cmake -DQT_VERSION_MAJOR=5 /cpp/client/
    
  • try compiling
    make
    

My expectation is that this would create a static library libQtOpenAPIClient.a in C++ that I can use in my project. And it does that for version 4.3.1, but not 6 or 7.

This might have to do with Qt5 (which I use), and Qt6 differing in the return type of qHash: uint in Qt5, and size_t in Qt6.

Note that changing the autogenerated code from

inline
#if QT_VERSION < 0x060000
uint
#else
size_t
#endif
qHash(const OAIObject& obj, uint seed = 0) noexcept{
    return qHash(obj.asJsonObject(), seed);
}

to

inline
#if QT_VERSION < 0x060000
uint
#else
size_t
#endif
qHash(const OAIObject& obj, uint seed = 0) noexcept{
    return qHash(obj, seed);    // no .asJsonObject()
}

at least results in the library being compiled successfully using above settings. I cannot verify right now whether or not the resulting library behaves correctly.

Related issues/PRs

My suspicion is that this has to do with #11621, but I can't prove that.

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

Reproduce the failure with the linked swagger.yaml, the cpp-qt-client generator, and the CMake and make commands described in the issue. Start by inspecting generated cpp/client/OAIObject.h and its qHash implementation, then compare the generated output with the working 4.3.1 result. Done means the generated Qt5 client builds successfully into libQtOpenAPIClient.a without manual edits.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.