swagger-api / swagger-api/swagger-codegen

[CppRest] file properties of models not generated as shared_ptr<>

Open
#7,052 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: C++ Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

After a long time using my original version of the cpprest generator, I decided to migrate finally to the official swagger-codegen version. We are using a somehow non-open-api-standard compliant feature: having files as model properties. This used to work in my original codebase but seemed to be broken since issue #3773 was fixed. Instead of generating:

std::shared_ptr<HttpContent> getFile();
    void setFile(std::shared_ptr<HttpContent> value);

...the following source is generated:

HttpContent getFile();
    void setFile(HttpContent value);

This in advance leads to errors when serializing/deserializing as the ModelBase only provides overloads for the shared_ptr.

Swagger-codegen version

2.2.3

Swagger declaration file content or url

https://gist.github.com/Danielku15/27d3604077fd325ba4dacf475a8fc66a

Command line used for generation
java -cp "swagger-codegen-cli.jar" io.swagger.codegen.SwaggerCodegen generate^
        -l cpprest^
        -i "SwaggerSpec.json"^
        -o "Generated"^
        -c "config.json"
Steps to reproduce
  1. Take given swagger specification and generate a cpprest client
  2. Inspect the MyFileViewModel.cpp and notice the non-shared_ptr file property.
  3. Compile the sources and there will be compilation errors for MyFileViewModel::fromJson as it cannot call setFile(HttpContent) with a shared_ptr<HttpContent> parameter.
  4. Also a compile error on MyFileViewModel::toMultipart is shown as there is no ModelBase::toHttpContent(utility::string_t, HttpContent) overload. It needs a shared_ptr<HttpContent> as second parameter.
Related issues/PRs

https://github.com/swagger-api/swagger-codegen/issues/3773
https://github.com/swagger-api/swagger-codegen/pull/3876

Suggest a fix/enhancement

I expect that removing p instanceof FileProperty from the code follow source line will fix the issue by bringing back the shared_ptr<HttpContent>. Maybe @sdrichter can point out why it was added in a first place. His issue mentions something that there were issues on APIs which return a file object.

https://github.com/swagger-api/swagger-codegen/blob/8cc8c1eac6eba2770bca17ca6387bc0303756574/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CppRestClientCodegen.java#L308

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 in modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CppRestClientCodegen.java at the referenced line, then generate the client with the supplied Swagger declaration and cpprest command. Inspect MyFileViewModel.cpp and verify that file properties use shared_ptr, allowing fromJson and toMultipart to compile with the ModelBase overloads.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.