swagger-api / swagger-api/swagger-codegen

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

Open
#9,901 1 comment 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

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

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 with the cpprest templates under modules/swagger-codegen/src/main/resources/cpprest and compare them with the generated samples/client/petstore/cpprest/api/PetApi.cpp. Reproduce generation using modules/swagger-codegen/src/test/resources/2_0/petstore.yaml and the command shown in the issue; done means an application/x-www-form-urlencoded request is generated without the ApiException.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.