swagger-api / swagger-api/swagger-codegen

Use 'const' for cpp getters when returning aggregate types

Open
#8,770 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

We are using swagger-codegen 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? (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.)

Swagger-codegen version

We are using swagger-maven-plugin v3.1.7
and swagger-generator v2.2.3

Swagger declaration file content or url
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;

import java.util.Collections;
import java.util.List;

@ApiModel
public class SampleRequest {
    private static final String MESSAGE_NAME = "messageName";
    private static final String PARAM_NAMES = "paramNames";

    @JsonProperty(MESSAGE_NAME)
    private final String messageName;

    @JsonProperty(PARAM_NAMES)
    private final List<String> paramNames;

    @JsonCreator
    public SampleRequest(
            @JsonProperty(value = MESSAGE_NAME, required = true) String messageName,
            @JsonProperty(value = PARAM_NAMES, required = true) List<String> paramNames) {
        this.messageName = messageName;
        this.paramNames = Collections.unmodifiableList(paramNames);
    }

    public String getMessageName() {
        return messageName;
    }

    public final List<String> getParamNames() {
        return paramNames;
    }
}
class  SampleRequest
    : public ModelBase
{
public:
 ...
    utility::string_t getMessageName() const; 
    std::vector<utility::string_t>& getParamNames();  // this getter doesn't have 'const'.
   
protected:
    utility::string_t m_MessageName;
    std::vector<utility::string_t> m_ParamNames;
};
Command line used for generation

Using the swagger-maven-plugin to generate the code. Please let me know if you need this.

Steps to reproduce

Please use the included sample Java code to generate a C++ class

Related issues/PRs

I looked around in the project, but did not find anything appropriate. Please let me know if I missed it.

Suggest a fix/enhancement

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

Use the supplied SampleRequest Java declaration to reproduce C++ generation through the swagger-maven-plugin and swagger-generator versions listed. Inspect the generated aggregate getter declaration and compare it with the primitive getter; done means aggregate getters are const-qualified while generation still succeeds for the sample.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.