Rust: Issue #13453 - Empty enum variant names (#13454)

This commit is contained in:
Jacob Halsey 2022-09-20 15:52:13 +01:00 committed by GitHub
parent 4f732c53e1
commit e6dd608897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 244 additions and 99 deletions

View File

@ -173,6 +173,11 @@ public abstract class AbstractRustCodegen extends DefaultCodegen implements Code
@Override
public String toEnumVarName(String name, String datatype) {
// Empty strings need to be mapped to "Empty"
// https://github.com/OpenAPITools/openapi-generator/issues/13453
if (Strings.isNullOrEmpty(name)) {
return "Empty";
}
// Rust Enum variants should be camel cased
return sanitizeIdentifier(name, CasingType.CAMEL_CASE, "variant", "enum variant", true);
}

View File

@ -117,6 +117,9 @@ public class AbstractRustCodegenTest {
Assert.assertEquals(fakeRustCodegen.toEnumVarName("SCREAMING_SNAKE_CASE", null), "ScreamingSnakeCase");
// Prefix is added when starting with a number
Assert.assertEquals(fakeRustCodegen.toEnumVarName("1_pending", null), "Variant1Pending");
// Empty strings need to be mapped to "Empty"
// https://github.com/OpenAPITools/openapi-generator/issues/13453
Assert.assertEquals(fakeRustCodegen.toEnumVarName("", null), "Empty");
}
@Test

View File

@ -812,9 +812,11 @@ components:
- $ref: '#/components/schemas/Baz'
- nullable: false
Baz:
description: Test handling of empty variants
enum:
- A
- B
- ""
type: string
TypeTesting:
description: Test handling of different field data types

View File

@ -1 +0,0 @@
4.0.3-SNAPSHOT

View File

@ -1,12 +0,0 @@
# InlineObject
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **String** | Updated name of the pet | [optional]
**status** | **String** | Updated status of the pet | [optional]
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@ -1,12 +0,0 @@
# InlineObject1
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**additional_metadata** | **String** | Additional data to pass to server | [optional]
**file** | [***std::path::PathBuf**](std::path::PathBuf.md) | file to upload | [optional]
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@ -8,14 +8,17 @@
* Generated by: https://openapi-generator.tech
*/
/// Baz : Test handling of empty variants
///
/// Test handling of empty variants
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Baz {
#[serde(rename = "A")]
A,
#[serde(rename = "B")]
B,
#[serde(rename = "")]
Empty,
}
@ -24,6 +27,7 @@ impl ToString for Baz {
match self {
Self::A => String::from("A"),
Self::B => String::from("B"),
Self::Empty => String::from(""),
}
}
}

View File

@ -8,14 +8,17 @@
* Generated by: https://openapi-generator.tech
*/
/// Baz : Test handling of empty variants
///
/// Test handling of empty variants
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Baz {
#[serde(rename = "A")]
A,
#[serde(rename = "B")]
B,
#[serde(rename = "")]
Empty,
}
@ -24,6 +27,7 @@ impl ToString for Baz {
match self {
Self::A => String::from("A"),
Self::B => String::from("B"),
Self::Empty => String::from(""),
}
}
}

View File

@ -8,14 +8,17 @@
* Generated by: https://openapi-generator.tech
*/
/// Baz : Test handling of empty variants
///
/// Test handling of empty variants
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Baz {
#[serde(rename = "A")]
A,
#[serde(rename = "B")]
B,
#[serde(rename = "")]
Empty,
}
@ -24,6 +27,7 @@ impl ToString for Baz {
match self {
Self::A => String::from("A"),
Self::B => String::from("B"),
Self::Empty => String::from(""),
}
}
}

View File

@ -8,14 +8,17 @@
* Generated by: https://openapi-generator.tech
*/
/// Baz : Test handling of empty variants
///
/// Test handling of empty variants
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Baz {
#[serde(rename = "A")]
A,
#[serde(rename = "B")]
B,
#[serde(rename = "")]
Empty,
}
@ -24,6 +27,7 @@ impl ToString for Baz {
match self {
Self::A => String::from("A"),
Self::B => String::from("B"),
Self::Empty => String::from(""),
}
}
}

View File

@ -1,32 +0,0 @@
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct InlineObject {
/// Updated name of the pet
#[serde(rename = "name", skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
/// Updated status of the pet
#[serde(rename = "status", skip_serializing_if = "Option::is_none")]
pub status: Option<String>,
}
impl InlineObject {
pub fn new() -> InlineObject {
InlineObject {
name: None,
status: None,
}
}
}

View File

@ -1,32 +0,0 @@
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct InlineObject1 {
/// Additional data to pass to server
#[serde(rename = "additionalMetadata", skip_serializing_if = "Option::is_none")]
pub additional_metadata: Option<String>,
/// file to upload
#[serde(rename = "file", skip_serializing_if = "Option::is_none")]
pub file: Option<std::path::PathBuf>,
}
impl InlineObject1 {
pub fn new() -> InlineObject1 {
InlineObject1 {
additional_metadata: None,
file: None,
}
}
}

View File

@ -0,0 +1,118 @@
/**
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI-Generator 6.1.0-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
#include "200_response.h"
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <stdexcept>
#include <regex>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "helpers.h"
using boost::property_tree::ptree;
using boost::property_tree::read_json;
using boost::property_tree::write_json;
namespace org {
namespace openapitools {
namespace server {
namespace model {
200_response::200_response(boost::property_tree::ptree const& pt)
{
fromPropertyTree(pt);
}
std::string 200_response::toJsonString(bool prettyJson /* = false */) const
{
std::stringstream ss;
write_json(ss, this->toPropertyTree(), prettyJson);
// workaround inspired by: https://stackoverflow.com/a/56395440
std::regex reg("\\\"([0-9]+\\.{0,1}[0-9]*)\\\"");
std::string result = std::regex_replace(ss.str(), reg, "$1");
return result;
}
void 200_response::fromJsonString(std::string const& jsonString)
{
std::stringstream ss(jsonString);
ptree pt;
read_json(ss,pt);
this->fromPropertyTree(pt);
}
ptree 200_response::toPropertyTree() const
{
ptree pt;
ptree tmp_node;
pt.put("name", m_Name);
pt.put("class", m_r_class);
return pt;
}
void 200_response::fromPropertyTree(ptree const &pt)
{
ptree tmp_node;
m_Name = pt.get("name", 0);
m_r_class = pt.get("class", "");
}
int32_t 200_response::getName() const
{
return m_Name;
}
void 200_response::setName(int32_t value)
{
m_Name = value;
}
std::string 200_response::getRClass() const
{
return m_r_class;
}
void 200_response::setRClass(std::string value)
{
m_r_class = value;
}
std::vector<200_response> create200_responseVectorFromJsonString(const std::string& json)
{
std::stringstream sstream(json);
boost::property_tree::ptree pt;
boost::property_tree::json_parser::read_json(sstream,pt);
auto vec = std::vector<200_response>();
for (const auto& child: pt) {
vec.emplace_back(200_response(child.second));
}
return vec;
}
}
}
}
}

View File

@ -0,0 +1,96 @@
/**
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI-Generator 6.1.0-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
/*
* 200_response.h
*
* Model for testing model name starting with number
*/
#ifndef 200_response_H_
#define 200_response_H_
#include <string>
#include <memory>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include "helpers.h"
namespace org {
namespace openapitools {
namespace server {
namespace model {
/// <summary>
/// Model for testing model name starting with number
/// </summary>
class 200_response
{
public:
200_response() = default;
explicit 200_response(boost::property_tree::ptree const& pt);
virtual ~200_response() = default;
200_response(const 200_response& other) = default; // copy constructor
200_response(200_response&& other) noexcept = default; // move constructor
200_response& operator=(const 200_response& other) = default; // copy assignment
200_response& operator=(200_response&& other) noexcept = default; // move assignment
std::string toJsonString(bool prettyJson = false) const;
void fromJsonString(std::string const& jsonString);
boost::property_tree::ptree toPropertyTree() const;
void fromPropertyTree(boost::property_tree::ptree const& pt);
/////////////////////////////////////////////
/// 200_response members
/// <summary>
///
/// </summary>
int32_t getName() const;
void setName(int32_t value);
/// <summary>
///
/// </summary>
std::string getRClass() const;
void setRClass(std::string value);
protected:
int32_t m_Name = 0;
std::string m_r_class = "";
};
std::vector<200_response> create200_responseVectorFromJsonString(const std::string& json);
template<>
inline boost::property_tree::ptree toPt<200_response>(const 200_response& val) {
return val.toPropertyTree();
}
template<>
inline 200_response fromPt<200_response>(const boost::property_tree::ptree& pt) {
200_response ret;
ret.fromPropertyTree(pt);
return ret;
}
}
}
}
}
#endif /* 200_response_H_ */