OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.1k stars 6.39k forks source link

[BUG] [cpprestsdk] multipart/related not correctly handled #3512

Open CyrilleBenard opened 5 years ago

CyrilleBenard commented 5 years ago

Bug Report Checklist

Description

The generator produces the code with the expected http content type but the corresponding code has not been generated, so that an exception is raised.

Generated code looks like :

pplx::task<utility::string_t> SMContextsCollectionApi::postSmContexts(std::shared_ptr<Inline_object> inlineObject)
{
 ../..
    std::unordered_set<utility::string_t> localVarConsumeHttpContentTypes;
    localVarConsumeHttpContentTypes.insert( utility::conversions::to_string_t("multipart/related") );

    std::shared_ptr<IHttpBody> localVarHttpBody;
    utility::string_t localVarRequestHttpContentType;

    // use JSON if possible
    if ( localVarConsumeHttpContentTypes.size() == 0 || localVarConsumeHttpContentTypes.find(utility::conversions::to_string_t("application/json")) != localVarConsumeHttpContentTypes.end() )
    {
        localVarRequestHttpContentType = utility::conversions::to_string_t("application/json");
        web::json::value localVarJson;

        localVarJson = ModelBase::toJson(inlineObject);
        localVarHttpBody = std::shared_ptr<IHttpBody>( new JsonBody( localVarJson ) );
    }
    // multipart formdata
    else if( localVarConsumeHttpContentTypes.find(utility::conversions::to_string_t("multipart/form-data")) != localVarConsumeHttpContentTypes.end() )
    {
        localVarRequestHttpContentType = utility::conversions::to_string_t("multipart/form-data");
        std::shared_ptr<MultipartFormData> localVarMultipart(new MultipartFormData);

        if(inlineObject.get())
        {
            inlineObject->toMultipart(localVarMultipart, utility::conversions::to_string_t("inlineObject"));
        }

        localVarHttpBody = localVarMultipart;
        localVarRequestHttpContentType += utility::conversions::to_string_t("; boundary=") + localVarMultipart->getBoundary();
    }
    else
    {
        throw ApiException(415, utility::conversions::to_string_t("SMContextsCollectionApi->postSmContexts does not consume any supported media type"));
    }
../..
}

"multipart/related" is set correctly but just behind its set, there is no corresponding code so that the ApiException (415) is raised

openapi-generator version

4.0.0-SNAPSHOT - hash 4b414d81d49ae21447d944cca1693a11bb0140dd

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: Check generation of cpprest
  description: Internal ref filename is check_multipart-related.yaml

servers:
  - url: http://localhost:8080

paths:
  /sm-contexts:
    post:
      summary:  Create SM Context
      tags:
        - SM contexts collection
      operationId: PostSmContexts
      requestBody:
        description: representation of the SM context to be created in the SMF
        required: true
        content:
          multipart/related:
            schema:
              type: object
              properties: # Request parts
                dataMessage:
                  type: string

      responses:
        '200':
          description: Everythings gonna be alright
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Content"
        default:
          description: unexpected error

components:
  schemas:
    Content:
      type: string
Command line used for generation

openapi-generator-cli.sh generate -i ./openapi.yaml -g cpp-restsdk -o gen-cpp

Steps to reproduce

Just generate

Related issues/PRs

N/A

Suggest a fix

Generate the corresponding code (>_<) :joy:

auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

CyrilleBenard commented 5 years ago

After having a look on what's this kind of media type, I noticed that the generator produces a multipart/form-data treatment (class, method, code) and I wonder why ?

In my really small openapi file example, there is no multipart/form-data referenced so, is there a confusion between these two media types (multipart/form-data vs multipart/related) ? Does the expected code is really close to the current one, so that we can reuse almost part of the current generated code ? I got really no experience on that :confused:

Any help would be appreciated :)