Skip to content

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

@kdeyev

Description

@kdeyev

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");

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