OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.77k stars 6.57k forks source link

[C++] Use 'const' for cpp getters even when the return type is an aggregate #1175

Open alotia opened 6 years ago

alotia commented 6 years ago
Description

We are using openapi-generator to generate C++ classes from Java. For primitive types, the C++ getter method has 'const' qualifier, but when aggregates are specified, the method does not have have the const qualifier. Can a 'const' be added for getter methods even when the return type is an aggregate? (I am not very familiar with C++, and maybe there is a reason this is not being done. And I am filing this on behalf of someone who mentioned that not having a const on these getters causes them some grief.)

Please Note: I had originally filed this issue against swagger-codegen (v2.2.3), and William Cheng had asked to try out the latest version of OpenAPI Generator, but it does not address the issue I had. He had then recommended that I file an issue in OpenAPI as well. For your reference, the swagger-codegen issue is: https://github.com/swagger-api/swagger-codegen/issues/8770

openapi-generator version

We are using openapi-generator-maven-plugin v3.3.0 and openapi-generator v3.3.0

OpenAPI declaration file content or url
Command line used for generation

Using the openapi-generator-maven-plugin to generate the code. Please let me know if you need any additional info

Steps to reproduce

Please use the included sample Java code to generate a C++ class. If you need any additional info, please let me know.

Related issues/PRs

https://github.com/swagger-api/swagger-codegen/issues/8770

Suggest a fix/enhancement
wing328 commented 6 years ago

cc @ravinikam (2017/07) @stkrwork (2017/07) @fvarose (2017/11) @etherealjoy (2018/02) @martindelille (2018/03)

MartinDelille commented 6 years ago

@alotia We should definitely have const qualifiers on getters.

etherealjoy commented 6 years ago

cpp-qt5-qhttpengine-server has const getter and const ref setters args

https://github.com/OpenAPITools/openapi-generator/blob/master/samples/server/petstore/cpp-qt5-qhttpengine-server/server/src/models/OAIOrder.h#L53

https://github.com/OpenAPITools/openapi-generator/blob/master/samples/server/petstore/cpp-qt5-qhttpengine-server/server/src/models/OAIOrder.h#L54

    QDateTime getShipDate() const;
    void setShipDate(const QDateTime &ship_date);

Sadly I fear breaking too much when applied to others.