openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[C++] ByteArray type is generated but unknown in C++

Open CyrilleBenard opened this issue 8 years ago • 5 comments

Description

The generator produces a #include "ByteArray.h" and ByteArray type usage whereas it does not exist in C++ I can't find what I'm suppose to DL or install to satisfy the compiler (?)

EDIT: Same issue with Object.h and Object type

Issue exists at least for languages

  • cpp-pistache-server
  • cpp-restsdk
openapi-generator version

3.0.3-SNAPSHOT

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Check ByteArray
servers:
  - url: http://localhost:8080
paths:
  /CheckByteArray:
    get:
      summary: Check Byte Array generation
      operationId: list
      tags:
        - Check
      responses:
        '200':
          description: Every things gonna be alright
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Content"
      default:
        description: unexpected error
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Content"

components:
  schemas:
    Bytes:
      format: byte
      type: string

    Content:
      type: object
      properties:
        type:
          $ref: '#/components/schemas/Bytes'
{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0",
    "title": "Check ByteArray"
  },
  "servers": [
    {
      "url": "http://localhost:8080"
    }
  ],
  "paths": {
    "/CheckByteArray": {
      "get": {
        "summary": "Check Byte Array generation",
        "operationId": "list",
        "tags": [
          "Check"
        ],
        "responses": {
          "200": {
            "description": "Every things gonna be alright",
            "headers": {
              "x-next": {
                "description": "A link to the next page of responses",
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Content"
                }
              }
            }
          }
        },
        "default": {
          "description": "unexpected error",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Content"
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Bytes": {
        "format": "byte",
        "type": "string"
      },
      "Content": {
        "type": "object",
        "properties": {
          "type": {
            "$ref": "#/components/schemas/Bytes"
          }
        }
      }
    }
  }
}
Command line used for generation

generate -i ./openapi.yaml -g cpp-pistache-server -o .

Steps to reproduce
g++ -c  -I./api -I./model -I./impl -Wall -g -std=c++11 -o obj/model/Content.o model/Content.cpp
In file included from model/Content.cpp:14:0:
model/Content.h:24:23: fatal error: ByteArray.h: No such file or directory

See also the model/Content.cpp content to identify some ByteArray type usage

Related issues/PRs

N/A

Suggest a fix/enhancement

CyrilleBenard avatar Jun 27 '18 15:06 CyrilleBenard

@CyrilleBenard It is missing a typeMapping in the like this one for Qt5 for example.

typeMapping.put("ByteArray", "QByteArray");

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java#L151

Model base has to add support for fromJson returning vector Problem is that you need to add support for all the vector types.

Model Mustache has to handle isByteArray Returning Maps is also similar issue.

I could try in the weekend if you need cpprest. But Qt5 already should have these supported already

Or you could try it and then make a PR if you cannot wait

etherealjoy avatar Jun 28 '18 07:06 etherealjoy

I wonder which type of mapping it would be "recommended" ? What's your mind and/or proposal ? :)

CyrilleBenard avatar Jun 28 '18 08:06 CyrilleBenard

Something like this for cpp-restsdk

typeMapping.put("ByteArray", "std::vector");

in the function below

@Override
    public String getTypeDeclaration(Schema p)

add something like this

        else if (ModelUtils.isByteArraySchema(p)) {
            return "std::vector<" + "char" + ">";

then in function below

    @Override
    public String toDefaultValue(Schema p) 
        } else if (ModelUtils.isByteArraySchema(p)) {
            return "std::vector<" + "char" + ">()";
        }

In modelbase-header.mustache You need to add the template function for the return vector

In model-source.mustache add a section to handle isByteArray and add additional guards in other sections

@stkrwork You know of something better?

etherealjoy avatar Jun 28 '18 08:06 etherealjoy

There is a problem in overloading the model base functions because they all accept the same argument. If we change it will breaking. :| This is the reason I used the Qt5 version. It needs some work to make it better but it works for a lot of use cases.

etherealjoy avatar Jun 28 '18 08:06 etherealjoy

Fixed with #497. I generated and made a quick test. I change the api to post instead and send a body with Content to see if serialization works. Looks Ok. I was wrong in saying ByteArray should be mapped to std::vector In most cases it should be string and for pistache is std::string

etherealjoy avatar Jul 07 '18 15:07 etherealjoy