Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cpprest] arrays of arrays or maps causes compilation error #7100

Closed
etherealjoy opened this issue Dec 1, 2017 · 11 comments
Closed

[cpprest] arrays of arrays or maps causes compilation error #7100

etherealjoy opened this issue Dec 1, 2017 · 11 comments

Comments

@etherealjoy
Copy link
Contributor

Description

When the Definition is an array of array or array of maps then the generated code is trying to use a method fromJson which is not present in the vector or map.

m_SystemStatus.clear();
if(multipart->hasContent(utility::conversions::to_string_t("SystemStatus")))
{

web::json::value jsonArray = web::json::value::parse
    (ModelBase::stringFromHttpContent(multipart->getContent(
      utility::conversions::to_string_t("SystemStatus"))));
for( auto& item : jsonArray.as_array() )
{
	if(item.is_null())
	{
		m_SystemStatus.push_back( std::vector<utility::string_t>(nullptr) );
	}
	else
	{
		std::vector<utility::string_t> newItem
              (std::vector<std::shared_ptr<utility::string_t>>());
		newItem->fromJson(item);
		m_SystemStatus.push_back( newItem );
	}
}

The code is trying to invoke fromJson from an array which is obviously wrong.

Swagger-codegen version

2.3.0
3.0.0

Swagger declaration file content or url

Array of maps

  IPAM:
    type: "object"
    properties:
      Driver:
        description: "Name of the IPAM driver to use."
        type: "string"
        default: "default"
      Config:
        description: "List of IPAM configuration options, specified as a map: 
        `{\"Subnet\": <CIDR>,  \"IPRange\": <CIDR>, \"Gateway\": <IP address>, \"AuxAddress\": 
        <device_name:IP address>}`"
        type: "array"
        items:
          type: "object"
          additionalProperties:
            type: "string"
      Options:
        description: "Driver-specific options, specified as a map."
        type: "array"
        items:
          type: "object"
          additionalProperties:
            type: "string"

Array of arrays

      DriverStatus:
        description: |
          Information specific to the storage driver, provided as
          "label" / "value" pairs.

          This information is provided by the storage driver, and formatted
          in a way consistent with the output of `docker info` on the command
          line.

          <p><br /></p>

          > **Note**: The information returned in this field, including the
          > formatting of values and labels, should not be considered stable,
          > and may change without notice.
        type: "array"
        items:
          type: "array"
          items:
            type: "string"
        example:
          - ["Backing Filesystem", "extfs"]
          - ["Supports d_type", "true"]
          - ["Native Overlay Diff", "true"]
Command line used for generation

java -jar /home/dev/swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i /home/dev/gen-test/yaml/docker-v132-subset.yaml -o /home/dev/gen-test/gen/ -l cpprest

Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

I could fix this for example, but I don't know how to perform member items datatype or container type check. If someone could hint me I could try fixing this.

This problem does not exist with the qt5cpp for example as the static method tries to use the recursive method to serialize or deserialize.

@etherealjoy
Copy link
Contributor Author

Another approach would also be interesting to hear.

@wing328 wing328 added this to the v2.4.0 milestone Jan 8, 2018
@wing328
Copy link
Contributor

wing328 commented Jan 8, 2018

I could fix this for example, but I don't know how to perform member items datatype or container type check. If someone could hint me I could try fixing this.

One way is

{{#isListContainer}}
{{#isListContainer}}
{{#isMapContainer}}
...
{{/isMapContainer}}
{{/isListContainer}}
{{/isListContainer}}

but it's not pretty.

Aother way I can think of is to update the datatype directly in postProcessOperations, postProcessModels.

@etherealjoy
Copy link
Contributor Author

I fixed this for Qt5 by inspecting the items but like you said it is not pretty.

@p-groarke
Copy link

p-groarke commented Feb 3, 2018

I'm having similar issues with vectors (on 2.3.1)

      no matching member function for call to 'toJson'
        json = ModelBase::toJson(contactIds);
               ~~~~~~~~~~~^~~~~~

      candidate function not viable: no known conversion from
      'std::vector<int32_t>' (aka 'vector<int>') to 'const utility::string_t' (aka
      'const basic_string<char, char_traits<char>, allocator<char> >') for 1st
      argument
    static web::json::value toJson( const utility::string_t& value );

etc...

Would you like me to open a different issue and/or link to my conan setup?

@etherealjoy
Copy link
Contributor Author

@p-groarke
Ok the issue is still the same in 2.4.0. You can link it to this one.
Have you thought of a possible solution by any chance?

@p-groarke
Copy link

@etherealjoy This is my first use of swagger-codegen and swagger, so I have no idea (yet). I'm digging in your generator :)

Will let you know/send a PR if I have a solution. The build scripts aren't up yet as they're probably broken, but if I can't find any solution I'll at least upload them so you can test and debug with a pretty good and large public swagger api.

@etherealjoy
Copy link
Contributor Author

@p-groarke
Yes, I would gladly test if you have some updates. I use the Docker Engine API to generate. It is fairly big and cover a lots of usecases.

@p-groarke
Copy link

@etherealjoy https://esi.tech.ccp.is/latest/swagger.json

Will upload my conan package soon.

@p-groarke
Copy link

@etherealjoy https://github.com/p-groarke/cpprest-esi
You'll need to add bincrafters remote and use --build missing. It is an absolute WIP ;)

The cmake I use (for conan) : https://github.com/p-groarke/cpprest-esi-cmake/blob/master/CMakeLists.txt

Cheers

@etherealjoy
Copy link
Contributor Author

Solved in #7571

@fanganpai
Copy link

I had the same problem in 2023 and tried multiple historical versions of swagger-codegen-cli and the problem persisted. Curious why? Hasn't MR fixed the vector nesting problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants