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

[cpprest] Bug: using application/x-www-form-urlencoded leads to ApiException

Open cipold opened this issue 7 years ago • 1 comments

Description

When generating the cpprest client code for an API specifying consumes: application/x-www-form-urlencoded (like in the pestore.yaml) the generated client code looks like this: https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/cpprest/api/PetApi.cpp#L800

std::unordered_set<utility::string_t> consumeHttpContentTypes;
consumeHttpContentTypes.insert( utility::conversions::to_string_t("application/x-www-form-urlencoded") );

if (name)
{
    formParams[ utility::conversions::to_string_t("name") ] = ApiClient::parameterToString(*name);
}
if (status)
{
    formParams[ utility::conversions::to_string_t("status") ] = ApiClient::parameterToString(*status);
}

std::shared_ptr<IHttpBody> httpBody;
utility::string_t requestHttpContentType;

// use JSON if possible
if ( consumeHttpContentTypes.size() == 0 || consumeHttpContentTypes.find(utility::conversions::to_string_t("application/json")) != consumeHttpContentTypes.end() )
{
    requestHttpContentType = utility::conversions::to_string_t("application/json");
}
// multipart formdata
else if( consumeHttpContentTypes.find(utility::conversions::to_string_t("multipart/form-data")) != consumeHttpContentTypes.end() )
{
    requestHttpContentType = utility::conversions::to_string_t("multipart/form-data");
}
else
{
    throw ApiException(415, utility::conversions::to_string_t("PetApi->updatePetWithForm does not consume any supported media type"));
}

You can see that consumeHttpContentTypes only contains application/x-www-form-urlencoded and thus the last else branch with the ApiExcetpion is executed and this method never succeeds.

Swagger-codegen version

master / 2.4.11-SNAPSHOT

Swagger declaration file content or url

modules/swagger-codegen/src/test/resources/2_0/petstore.yaml

Command line used for generation

./run-in-docker.sh generate -t modules/swagger-codegen/src/main/resources/cpprest -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l cpprest -o samples/client/petstore/cpprest

Steps to reproduce
  • Run the command line
  • See defective generated example file

cipold avatar Dec 02 '19 12:12 cipold

When trying to work around this issue by using multipart/form-data issue #9902 came up. This is the issue we actually then fixed by modifying the template files. I will submit a PR which fixes #9902 but this issue still remains.

cipold avatar Dec 02 '19 12:12 cipold