OpenAPITools / OpenAPITools/openapi-generator

[Bug][cpp-restsdk]Can not support customed content type on api-source.mustache

Open
#12,350 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • [ ✓ ] Have you provided a full/minimal spec to reproduce the issue?
  • [ ✓ ] Have you validated the input using an OpenAPI validator ([example](https://apidevtools.org/swagger-parser/online/))?
  • [ ✓ ] Have you tested with the latest master to confirm the issue still exists?
  • [ ✓ ] Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When an input json like below

"responses": {
                "200": {
                    "description": "The exchange container details were retrieved successfully.",
                    "content": {
                        "application/vnd.changeset+json": {
                            "schema": {
                                "$ref": "#/components/schemas/PaginatedSpaceResponse"
                            }
                        }
                    }
                },
                "400": {
                    "description": "The request is invalid.",
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/ErrorResponse"
                            }
                        }
                    }
                }
            }

Based on the template code ,

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-source.mustache

The destination code will not extract json value on response-200 json node , Because the response-200 json node has a content media type = "application/vnd.changeset+json" which is not a common content type.

Because on code , the extract code has hard coded the content type.

You can see below code :

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-source.mustache#L50-L95

The generated code is :

std::unordered_set<utility::string_t> localVarResponseHttpContentTypes;
localVarResponseHttpContentTypes.insert( utility::conversions::to_string_t("application/vnd.changeset+json") );

utility::string_t localVarResponseHttpContentType;

// use JSON if possible
if ( localVarResponseHttpContentTypes.size() == 0 )
{
	localVarResponseHttpContentType = utility::conversions::to_string_t("application/json");
}
// JSON
else if ( localVarResponseHttpContentTypes.find(utility::conversions::to_string_t("application/json")) != localVarResponseHttpContentTypes.end() )
{
	localVarResponseHttpContentType = utility::conversions::to_string_t("application/json");
}
// multipart formdata
else if( localVarResponseHttpContentTypes.find(utility::conversions::to_string_t("multipart/form-data")) != localVarResponseHttpContentTypes.end() )
{
	localVarResponseHttpContentType = utility::conversions::to_string_t("multipart/form-data");
}
else
{
	throw ApiException(400, utility::conversions::to_string_t("ExchangesApi->createExchange does not produce any supported media type"));
}

Here , the custome content type("application/vnd.changeset+json") is inserted to localVarResponseHttpContentTypes

{{#produces}}
    localVarResponseHttpContentTypes.insert( utility::conversions::to_string_t("{{{mediaType}}}") );
{{/produces}}

But here we will get ApiException:400... on later code , because {{{mediaType}}} not in the lists {"application/json","multipart/form-data","text/plain"}.

Suggested fix is :https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-source.mustache#L67-L70

{{#produces}} 
else if ( localVarResponseHttpContentTypes.find(utility::conversions::to_string_t("{{{mediaType}}}")) != localVarResponseHttpContentTypes.end() )

  {

    localVarResponseHttpContentType = utility::conversions::to_string_t("{{{mediaType}}}");

  }
 {{/produces}} 
openapi-generator version

master , 5.4.0

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 modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-source.mustache, especially lines 50-95, and reproduce the supplied response using application/vnd.changeset+json. Compare the generated media-type selection with the custom content type in localVarResponseHttpContentTypes. Done means generated C++ accepts that custom JSON media type instead of throwing ApiException:400.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.