swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[CPPREST][CPP] Several Compilation Errors, setTags method missing

Open solvingj opened this issue 9 years ago • 50 comments

Description

There are several methods which are causing compilation errors.
fromJson fromMultipart setTags

While I am able to find fromJson and fromMultipart, I cannot find the method setTags method anywhere. Interestingly, I generated Petstore and Petstore on Heroku samples and this method was not referenced anywhere in that code. Perhaps those specs don't have custom tags and my spec does.

Swagger-codegen version

http://editor.swagger.io http://editor2.swagger.io

Swagger declaration file content or url

Here is the JSON Swagger spec: https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-iothub/2017-01-19/swagger/iothub.json

Command line used for generation

http://editor.swagger.io http://editor2.swagger.io

Both produce this method setTags()

Steps to reproduce
  1. Generate the code from the provided swagger spec using the web editor.
  2. Open file model\Resource.cpp
  3. Navigate to line 166 and find :

setTags( newItem );

This method does not exist anywhere in any version of the the Microsoft CPPREST library: https://github.com/Microsoft/cpprestsdk Nor does it exist in the generated sources.

Related issues

I could find no previous github issue referencing the setTags method.

Suggest a Fix

I noticed that the QT5 sample for petstore also references the setTags method. In researching, I saw that the CPPREST generator was probably based off the QT5 generator to some extent. It seems possible that the generation of setTags() was copied from the QT5 generator but without the code that generates an implementation of the method. Meanwhile, perhaps none of the sample specs used for testing happen to feature custom tags, so it never got generated during testing and never discovered. All very speculative, I'm really not sure.

solvingj avatar Jun 17 '17 13:06 solvingj

I have posted the Azure IOTHub SDK with the issues here:

https://github.com/solvingJ/azure-iothub-sdk-cpprest

solvingj avatar Jun 17 '17 14:06 solvingj

@solvingJ I would suggest you to try the latest master to see if you can still repeat the issue as there're 9 PRs for CppRest since last stable release (which is used by the Swagger Editors)

wing328 avatar Jun 17 '17 17:06 wing328

I saw that but I wasn't sure which branch/version the swagger editors were built from. Furthermore, I only ever generated from the web interface. I'm not really an expert developer, so I don't know that I'm capable of compiling and building the generator and running it locally (or at least, not without spending a day on it and asking someone for help). When do you think the new generator code will be live on online editor?

solvingj avatar Jun 17 '17 18:06 solvingj

@solvingJ what about using the pre-built Swagger Codegen CLI Docker image: https://github.com/swagger-api/swagger-codegen#public-pre-built-docker-images?

When do you think the new generator code will be live on online editor?

We hope to do it by the end of July (it was already postponed from May 31st)

wing328 avatar Jun 18 '17 03:06 wing328

I actually did use the docker pre-built Swagger Codegen CLI image last night, and was able to generate. It still has the same issues. Will try to get post specific errors soon.

Here is the branch with the latest: https://github.com/solvingJ/azure-iothub-sdk-cpprest/blob/codegen-2_2_3-from-snapshot-via-docker

solvingj avatar Jun 18 '17 13:06 solvingj

I have the same issue, I tried to generate the Swagger spec of the Azure IoT Hub and it generates a lot of errors where it tries to call fromMultiPart on an std::map. Not only in the 2.2 but also in the current 2.2.3 version:

azure-iothub-sdk-cpprest\model\IotHubProperties.cpp(337): error C2039: "toMultipart": Ist kein Element von "std::map<utility::string_t,std::shared_ptr<io::swagger::client::model::MessagingEndpointProperties>,std:: less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>" [build\libs\azure-iothub-sdk-cpprest\CppRestSwaggerClient.vcxproj]

It seems to me like it's generating the wrong type there.

klemens-morgenstern avatar Jun 18 '17 13:06 klemens-morgenstern

It seems possible that the generation of setTags() was copied from the QT5 generator but without the code that generates an implementation of the method.

Yup, I think likely that's the case so we'll need to remove the code copied from the QT5 templates.

The cpprest templates can be found in https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen/src/main/resources/cpprest

@solvingJ @klemens-morgenstern May I know if you've time to contribute a PR to clean up the auto-generated code for CPPREST generator?

wing328 avatar Jun 18 '17 17:06 wing328

I'm a not experienced enough to be helpful. I barely know any C++, and don't understand how the code-generation in swagger codegen works really. It seems like this will require someone who understands both . Sorry.

solvingj avatar Jun 18 '17 18:06 solvingj

I digged into this issue a little bit deeper and it turns out I was wrong about copying the code from QT5 templates. The issue was due to missing "setter" for properties that are "array": https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/cpprest/model-source.mustache#L405-L411

Using the Petstore store example, the Pet model has the property tags, which is an array of string: https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/cpprest/model/Pet.cpp#L296-L309

My question is what would the setter look like so that I can add it back to the model template for cpprest?

wing328 avatar Jun 19 '17 02:06 wing328

#5877 Fixed the setTags method.
Per @stkrwork

I added my changes to the cpprest template, which should fix the settag problem, but there is still the issue with the toMultipart, toJson, and fromJson function. The cpprest template will require some repairs.

Still looking for help implementing these.

solvingj avatar Jun 19 '17 16:06 solvingj

@solvingJ I've merged @stkrwork PR into master. Can you please pull the latest docker image to give it another try and list out what still needs to be fixed?

wing328 avatar Jun 19 '17 17:06 wing328

@stkrwork already tested cpprest for us, check my comments above, looks like still broken are: toMultipart, toJson, and fromJson

solvingj avatar Jun 19 '17 17:06 solvingj

Can you share a bit more details? Did you get compilation errors? If yes, please provide them. The steps you provided previously are very helpful

1. Generate the code from the provided swagger spec using the web editor.
2. Open file model\Resource.cpp
3. Navigate to line 166 and find :
setTags( newItem );

wing328 avatar Jun 19 '17 17:06 wing328

FYI, I have been working on this for the past hour. Will continue.

solvingj avatar Jun 19 '17 18:06 solvingj

Here are some examples of the errors which still exist

azure-iothub-sdk-cpprest\model\EventHubConsumerGroupInfo.cpp(46): error C2664: 'web::json::value io::swagger::client::model::ModelBase::toJson(bool)': cannot convert argument 1 from 'const std::map<utility::string_t,utility::string_t,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' to 'const utility::string_t &'

azure-iothub-sdk-cpprest\model\EventHubConsumerGroupInfo.cpp(67): error C2227: left of '->fromJson' must point to class/struct/union/generic type

azure-iothub-sdk-cpprest\model\EventHubConsumerGroupInfo.cpp(93): error C2819: type 'std::map<utility::string_t,utility::string_t,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' does not have an overloaded member 'operator ->'

azure-iothub-sdk-cpprest\model\Resource.cpp(122): error C2039: 'toMultipart': is not a member of 'std::map<utility::string_t,utility::string_t,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'

solvingj avatar Jun 19 '17 20:06 solvingj

I might look into this today

stkrwork avatar Jun 20 '17 07:06 stkrwork

The template is only recognizing the isListContainer and isPrimitiveType when generating, and a default generation template. As tags in this case is a map, the isMapContainer needs to be checked. I am implementing that change currently, which means it should work then. The other complexTypes are handled ok with the default, I think.

The petstore example models don't have a map datatype, so therefore this error could never be seen in the samples.

The question is, how do you want to represent the map in the json? In form of this? [ { "key":"testkey", "value":"testval" } ]

Or would another format be better?

stkrwork avatar Jun 20 '17 08:06 stkrwork

The petstore example models don't have a map datatype, so therefore this error could never be seen in the samples.

Right, the cpprest petstore samples (based on petstore.yaml) only covers the basic. We've created petstore-with-fake-endpoints-models-for-testing.yaml to cover a lot more edge cases such as map datatype and map of map as well.

wing328 avatar Jun 20 '17 08:06 wing328

For a first step, I am implementing a way for using maps and not maps of maps, as maps of maps will require refactoring of the modelbase as well, as everything needs to be moved into new functions for lists and maps.

stkrwork avatar Jun 20 '17 09:06 stkrwork

For a first step, I am implementing a way for using maps and not maps of maps, as maps of maps will require refactoring of the modelbase as well, as everything needs to be moved into new functions for lists and maps.

Yup, that's totally fine (I'm not saying you need to fix all the edge cases covered by petstore-with-fake-endpoints-models-for-testing.yaml)

wing328 avatar Jun 20 '17 09:06 wing328