Skip to content

[BUG] Broken cpp-restsdk code is generated for POST methods returning a "file" type #5505

Open
@sorinansys

Description

@sorinansys

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

openapi-generator versions 4.1.3 to version 4.2.3 generate broken code.

openapi-generator version

Bug can be reproduced with the following versions: 4.1.3, 4.2.0, 4.2.1, 4.2.2 and 4.2.3
Bug cannot be reproduced with version 4.1.2

OpenAPI declaration file content or url

This is the content of the monitoring.json file:

{
  "x-generator": "NSwag v12.0.9.0 (NJsonSchema v9.13.10.0 (Newtonsoft.Json v10.0.0.0))","swagger" : "2.0",
  "info": {"title": "Monitoring","version": "1.0.0"},"consumes": ["application/json"],"produces": ["application/json"],
  "paths": {"/Monitoring/Publish/messages": {"post": {"tags": ["Publish"],"operationId": "RequestOptions",
        "responses": {
          "200": {
            "x-nullable": true,"description": "",
            "schema": {"type": "file"}
          }}}}}}
Command line used for generation

java -jar openapi-generator-cli.jar generate -i monitoring.json -g cpp-restsdk -o CppGenerated

Steps to reproduce

Note: Steps are presented Powershell Windows command liness in a reproducebug.ps1 file

STEP 1: Setup sandbox folder
a) Create empty folder(sandbox)
b) copy monitoring.json ( with content above ) file inside.
c) create CppGenerated empty folder inside sandbox

STEP 2. Set the open-api-generator version to be tested (e.g 4.1.3)
param( [string]$version = "4.1.3")

STEP 3. Download openapi-generator-cli.jar file for the requested version
Invoke-WebRequest -OutFile openapi-generator-cli.jar https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/$version/openapi-generator-cli-$version.jar

STEP 4. generate cpp-restsdk code
java -jar openapi-generator-cli.jar generate -i monitoring.json -g cpp-restsdk -o CppGenerated

STEP 5. Check declaration of class HttpContent in CppGenerated/HttpContent.h:
a) class has only an empty constructor i.e. HttpContent()
b) class does not have any method called fromJson()

STEP 6. Check how code that uses HttpContent class is generated in CppGenerated/api/PublishApi.cpp
a) check the instantiation of objects of class HttpContent
b) check if there are the method fromJson is called for HtppContent objects

STEP 7. Diagnose good/broken code generation
a) Steps 5a will generate empty constructor for HttpContent in the following versions:
4.1.0, 4.1.1, 4.1.2, 4.1.3 , 4.2.0 , 4.2.1 , 4.2.2. and 4.2.3
b) Steps 5b will generate HttpContent definitions without a method fromJson in the following versions:
4.1.0, 4.1.1, 4.1.2, 4.1.3 , 4.2.0 , 4.2.1 , 4.2.2. and 4.2.3
c) Steps 6a will generate code that uses an empty constructor for objects of type HttpContent (CORRECT CODE GENERATION)
4.1.0, 4.1.1, 4.1.2
c) Steps 6b will not invoke fromJson method for objects of type HttpContent (CORRECT CODE GENERATION)
4.1.0, 4.1.1, 4.1.2
c) Steps 6a will generate code that invokes a constructor with a string_t parameter for objects of type HttpContent (BROKEN CODE GENERATION)
4.1.3, 4.2.0 , 4.2.1, 4.2.2, 4.2.3
d) Steps 6b will generate code that invokes a fromJson method for objects of type HttpContent (BROKEN CODE GENERATION)
4.1.3, 4.2.0 , 4.2.1, 4.2.2, 4.2.3

Expected code generation (i.e. CORRECT CODE GENERATION) for HttpContent constructor invocation (observed in versions 4.1.0, 4.1.1 and 4.1.2)

HttpContent localVarResult;

Expected code generation (i.e. CORRECT CODE GENERATION) to set the data of HttpContent object (observed in versions 4.1.0, 4.1.1 and 4.1.2)

localVarResult.setData(stream);

Unexpected code generation (i.e. BROKEN CODE GENERATION) for HttpContent constructor invocation (observed in versions 4.1.3, 4.2.0 , 4.2.1, 4.2.2, 4.2.3)

HttpContent localVarResult(utility::conversions::to_string_t(""));

Unexpected code generation (i.e. BROKEN CODE GENERATION) to set the data of HttpContent object (observed in versions 4.1.3, 4.2.0 , 4.2.1, 4.2.2, 4.2.3)

localVarResult->fromJson(localVarJson);

Related issues/PRs
Suggest a fix

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions