OpenAPITools / OpenAPITools/openapi-generator

[BUG][C++][Pistache] Array of enumerated strings generates code that won't compile

Open
#10,324 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)?
  • 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

Forgive me, this is my first Github issue ever.

I noticed that when using an array of enumerated strings, the generated code fails to compile.

The below example will dictate why.

        operatingMode:
          type: array
          items:
            type: string
            enum: ['A', 'B','C','D','E','F']

The below line success = value.validate(msg, currentValuePath) && success; added will fail to compile as vector obviously has no validate method.

    if (operatingModeIsSet())
    {
        const std::vector<std::string>& value = m_OperatingMode;
        const std::string currentValuePath = _pathPrefix + ".operatingMode";
                
        #pragma region MY_COMMENTS
        // This line is generated erroneously, and causes compilation problems
        #pragma endregion MY_COMMENTS

        success = value.validate(msg, currentValuePath) && success;

        #pragma region MY_COMMENTS
        // Correctly identifies this member as an array/vector
        #pragma endregion MY_COMMENTS

        { // Recursive validation of array elements
            const std::string oldValuePath = currentValuePath;
            int i = 0;
            for (const std::string& value : value)
            { 
                const std::string currentValuePath = oldValuePath + "[" + std::to_string(i) + "]";
                        
        //...

I believe this issue lay here in the file model-validation-body.mustache

        {{^isString}}{{#allowableValues.enumVars.0.value}}

        success = value.validate(msg, currentValuePath) && success;

My current workaround is doing the following, but I believe this is ultimately not the cleanest and my mustache skills leave much to be desired.

        {{^isString}}{{#allowableValues.enumVars.0.value}}
        {{^isArray}}
        success = value.validate(msg, currentValuePath) && success;
        {{/isArray}}
openapi-generator version
  • openapi-generator-cli-5.2.1.jar

Though I believe this to be a problem in master in general.

OpenAPI declaration file content or url
openapi: 3.0.0
servers:
  - url: 'http://localhost:8080/v2'
info:
  description: >-
    This is a sample rest server. For this sample, you can use the api key
    `special-key` to test the authorization filters.
  version: 1.0.0
  title: OpenAPI
  license:
    name: Apache-2.0
    url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
tags:
  - name: test
    description: Functions related to test interactions
paths:
  /test/status:
    post:
      tags:
        - test
      summary: Sends a test status message
      description: 'sends test data'
      operationId: sendtestStatus
      responses:
        '200':
          description: successful operation
        '405':
          description: Invalid input
      requestBody:
        $ref: '#/components/requestBodies/testStatus'
externalDocs:
  description: Find out more about Swagger
  url: 'http://swagger.io'
components:
  requestBodies:
    testStatus:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/testStatus'
            x-body-name: test_status
      description: testStatus object that needs to be added
      required: true
  securitySchemes:
    api_key:
      type: apiKey
      name: api_key
      in: header
  schemas:
    testStatus:
      title: testStatus
      description: A tests status
      type: object
      properties:
        operatingState:
          type: string
          enum: ['1', '2','3','4','5','6']
        operatingMode:
          type: array
          items:
            type: string
            enum: ['A', 'B','C','D','E','F']
      xml:
        name: testStatus
Generation Details

My Dockerfile with some information redacted.

ARG OPENAPI_GENERATOR_VERSION=5.2.1
ARG OPENAPI_SCHEMA=api.yml
ARG OPENAPI_IMPL=cpp-pistache-server
ARG EXTRA_ARGS_TO_GENERATOR=

FROM ${REPO}ubuntu:18.04 as generator

RUN apt-get update && apt-get install -y wget default-jre && rm -rf /var/lib/apt/lists/

ARG ARTIFACTORY_MAVEN_REPO
ARG OPENAPI_GENERATOR_VERSION
ARG OPENAPI_SCHEMA
ARG EXTRA_ARGS_TO_GENERATOR
ARG OPENAPI_IMPL

RUN wget "${ARTIFACTORY_MAVEN_REPO}/org/openapitools/openapi-generator-cli/${OPENAPI_GENERATOR_VERSION}/openapi-generator-cli-${OPENAPI_GENERATOR_VERSION}.jar" -O openapi-generator-cli.jar

COPY mustache/* /openapi/codegen/templates/
COPY ${OPENAPI_SCHEMA} /openapi/

RUN mkdir src-gen && java -jar openapi-generator-cli.jar generate -i /openapi/${OPENAPI_SCHEMA} -g ${OPENAPI_IMPL} -o src-gen/ -t /openapi/codegen/templates ${EXTRA_ARGS_TO_GENERATOR}
Steps to reproduce
  • Generate source from provided api using command java -jar openapi-generator-cli.jar generate -i /openapi/api.yml -g cpp-pistache-server -o src-gen/
  • Try to compile, fail due to this issue
  • Add this fix to CMakeLists.txt
ExternalProject_Add(PISTACHE
    # TRACK THIS BUG REPORT https://github.com/OpenAPITools/openapi-generator/issues/9148
    GIT_TAG 0d09ab982edd03bc8457fa6351af7d0c4a135da2
  • See compilation failure
Related issues/PRs
  • []
Suggest a fix

Maybe this as referenced above?

        {{^isArray}}
        success = value.validate(msg, currentValuePath) && success;
        {{/isArray}}

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-pistache-server/model-validation-body.mustache and the provided OpenAPI schema, then reproduce with the documented generate command. Check the generated C++ for an array of enum strings and confirm compilation succeeds while scalar enum validation remains intact.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.