Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 6 tasks
kdeyev opened this issue Aug 13, 2021 · 1 comment
Open
5 of 6 tasks

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

kdeyev opened this issue Aug 13, 2021 · 1 comment

Comments

@kdeyev
Copy link

kdeyev commented Aug 13, 2021

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");
@srgbtl
Copy link
Contributor

srgbtl commented Oct 19, 2021

Should be fixed by #10531

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants