OpenAPITools / OpenAPITools/openapi-generator

[BUG] [cpprestsdk] Wrong C++ code for enums containing models

Open
#10,151 1 comment 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
    Components:
      title: Components
      enum:
      - Components_1
      - Components_2
      - Components_4
      type: string
      description: An enumeration.

cpprestsdk code generated for the model shown above:

#include "Components.h"

namespace org {
namespace openapitools {
namespace client {
namespace model {



Components::Components()
{
}

Components::~Components()
{
}

void Components::validate()
{
    // TODO: implement validation
}

web::json::value Components::toJson() const
{
    web::json::value val = web::json::value::object();

    
    if (m_value == eComponents::Components__1) val = web::json::value::string(U(Components_1));
    if (m_value == eComponents::Components__2) val = web::json::value::string(U(Components_2));
    if (m_value == eComponents::Components__4) val = web::json::value::string(U(Components_4));

    return val;
}

bool Components::fromJson(const web::json::value& val)
{
    auto s = val.as_string();

    
    if (s == utility::conversions::to_string_t(Components_1)) m_value = eComponents::Components__1;
    if (s == utility::conversions::to_string_t(Components_2)) m_value = eComponents::Components__2;
    if (s == utility::conversions::to_string_t(Components_4)) m_value = eComponents::Components__4;
    return true;
}

void Components::toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& prefix) const
{
    utility::string_t namePrefix = prefix;
    if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t("."))
    {
        namePrefix += utility::conversions::to_string_t(".");
    }

    utility::string_t s;

    
    if (m_value == eComponents::Components__1) s = utility::conversions::to_string_t(Components_1);
    if (m_value == eComponents::Components__2) s = utility::conversions::to_string_t(Components_2);
    if (m_value == eComponents::Components__4) s = utility::conversions::to_string_t(Components_4);

    multipart->add(ModelBase::toHttpContent(namePrefix, s));
}

bool Components::fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& prefix)
{
    bool ok = true;
    utility::string_t namePrefix = prefix;
    if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t("."))
    {
        namePrefix += utility::conversions::to_string_t(".");
    }
    {
        utility::string_t s;
        ok = ModelBase::fromHttpContent(multipart->getContent(namePrefix), s);
        eComponents v;

        
        if (s == utility::conversions::to_string_t(Components_1)) v = eComponents::Components__1;
        if (s == utility::conversions::to_string_t(Components_2)) v = eComponents::Components__2;
        if (s == utility::conversions::to_string_t(Components_4)) v = eComponents::Components__4;

        setValue(v);
    }
    return ok;
}

Components::eComponents Components::getValue() const
{
   return m_value;
}

void Components::setValue(Components::eComponents const value)
{
   m_value = value;
}


}
}
}
}

Code like shown blows is not valid C++ code:

    if (m_value == eComponents::Components__1) s = utility::conversions::to_string_t(Components_1);

and the valid code would be:

    if (m_value == eComponents::Components__1) s = utility::conversions::to_string_t("Components_1");
openapi-generator version

I was able to reproduce it with the 5.2.1-SNAPSHOT version

OpenAPI declaration file content or url
    Components:
      title: Components
      enum:
      - Components_1
      - Components_2
      - Components_4
      type: string
      description: An enumeration.
Generation Details

I've used docker for the generating C++ code:

docker run -it -v MY_LOCAL_PATH:/local --network host --rm openapitools/openapi-generator-cli generate -i /local/openapi.json -g cpp-restsdk -o /local/generated
Steps to reproduce
  1. Generate C++ code.
  2. Verify that C++ code is not valid
Related issues/PRs
Suggest a fix

Code like shown blows is not valid C++ code:

    if (m_value == eComponents::Components__1) s = utility::conversions::to_string_t(Components_1);

and the valid code would be:

    if (m_value == eComponents::Components__1) s = utility::conversions::to_string_t("Components_1");

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

Reproduce the issue with the documented Docker command using the cpp-restsdk generator and the provided enum schema, then inspect the generated C++ output. Done means the generated enum serialization code uses valid string literals such as "Components_1" and the resulting C++ code is valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, docker, openapi
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.