Bug Report Checklist
Description
Enums are generated in models when referencing an enum, instead of just referencing the type, causing collision, and build failure. The enum is generated twice.
openapi-generator version
4.0.3
OpenAPI declaration file content or url
openapi: '3.0.0'
info:
version: 1.0.0
title: API with Default Values
servers:
- url: http://localhost:8080
paths:
/test:
post:
operationId: testDefaults
requestBody:
required: true
content:
application/json:
schema:
'$ref': '#/components/schemas/Payload'
responses:
'200':
description: Ok
components:
schemas:
Payload:
type: object
properties:
stringProp:
type: string
default: foo
doubleProp:
type: number
format: double
default: 1.234
enumProp:
$ref: '#/components/schemas/EnumProp'
EnumProp:
type: string
enum: [One, Two, Three]
default: Two
Command line used for generation
openapi-generator generate -i OpenApi2Docs/openapi.yml -g aspnetcore -o codegen/labelingOA3/server -c serverConfig.json
{
"packageAuthors": "Test",
"packageTitle": "TestTitle",
"packageName": "Test.TestNamespace",
"buildTarget": "library",
"operationIsAsync": "true",
"operationResultTask": "true"
}
Steps to reproduce
Run command in cli
####Expected Output
/*
* API with Default Values
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using Newtonsoft.Json;
namespace Test.TestNamespace.Models
{
/// <summary>
///
/// </summary>
[DataContract]
public class Payload : IEquatable<Payload>
{
/// <summary>
/// Gets or Sets StringProp
/// </summary>
[DataMember(Name="stringProp", EmitDefaultValue=false)]
public string StringProp { get; set; }
/// <summary>
/// Gets or Sets DoubleProp
/// </summary>
[DataMember(Name="doubleProp", EmitDefaultValue=false)]
public double? DoubleProp { get; set; }
/// <summary>
/// Gets or Sets EnumProp
/// </summary>
[DataMember(Name="enumProp", EmitDefaultValue=false)]
public EnumProp? EnumProp { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("class Payload {\n");
sb.Append(" StringProp: ").Append(StringProp).Append("\n");
sb.Append(" DoubleProp: ").Append(DoubleProp).Append("\n");
sb.Append(" EnumProp: ").Append(EnumProp).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
/// <summary>
/// Returns the JSON string presentation of the object
/// </summary>
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
/// <summary>
/// Returns true if objects are equal
/// </summary>
/// <param name="obj">Object to be compared</param>
/// <returns>Boolean</returns>
public override bool Equals(object obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((Payload)obj);
}
/// <summary>
/// Returns true if Payload instances are equal
/// </summary>
/// <param name="other">Instance of Payload to be compared</param>
/// <returns>Boolean</returns>
public bool Equals(Payload other)
{
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return
(
StringProp == other.StringProp ||
StringProp != null &&
StringProp.Equals(other.StringProp)
) &&
(
DoubleProp == other.DoubleProp ||
DoubleProp != null &&
DoubleProp.Equals(other.DoubleProp)
) &&
(
EnumProp == other.EnumProp ||
EnumProp.Equals(other.EnumProp)
);
}
/// <summary>
/// Gets the hash code
/// </summary>
/// <returns>Hash code</returns>
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
var hashCode = 41;
// Suitable nullity checks etc, of course :)
if (StringProp != null)
hashCode = hashCode * 59 + StringProp.GetHashCode();
if (DoubleProp != null)
hashCode = hashCode * 59 + DoubleProp.GetHashCode();
hashCode = hashCode * 59 + EnumProp.GetHashCode();
return hashCode;
}
}
#region Operators
#pragma warning disable 1591
public static bool operator ==(Payload left, Payload right)
{
return Equals(left, right);
}
public static bool operator !=(Payload left, Payload right)
{
return !Equals(left, right);
}
#pragma warning restore 1591
#endregion Operators
}
}
####Actual Output
/*
* API with Default Values
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using Newtonsoft.Json;
namespace Test.TestNamespace.Models
{
/// <summary>
///
/// </summary>
[DataContract]
public class Payload : IEquatable<Payload>
{
/// <summary>
/// Gets or Sets StringProp
/// </summary>
[DataMember(Name="stringProp", EmitDefaultValue=false)]
public string StringProp { get; set; }
/// <summary>
/// Gets or Sets DoubleProp
/// </summary>
[DataMember(Name="doubleProp", EmitDefaultValue=false)]
public double? DoubleProp { get; set; }
/// <summary>
/// Gets or Sets EnumProp
/// </summary>
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum EnumProp
{
/// <summary>
/// Enum OneEnum for One
/// </summary>
[EnumMember(Value = "One")]
OneEnum = 1,
/// <summary>
/// Enum TwoEnum for Two
/// </summary>
[EnumMember(Value = "Two")]
TwoEnum = 2,
/// <summary>
/// Enum ThreeEnum for Three
/// </summary>
[EnumMember(Value = "Three")]
ThreeEnum = 3
}
/// <summary>
/// Gets or Sets EnumProp
/// </summary>
[DataMember(Name="enumProp", EmitDefaultValue=false)]
public EnumProp? EnumProp { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("class Payload {\n");
sb.Append(" StringProp: ").Append(StringProp).Append("\n");
sb.Append(" DoubleProp: ").Append(DoubleProp).Append("\n");
sb.Append(" EnumProp: ").Append(EnumProp).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
/// <summary>
/// Returns the JSON string presentation of the object
/// </summary>
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
/// <summary>
/// Returns true if objects are equal
/// </summary>
/// <param name="obj">Object to be compared</param>
/// <returns>Boolean</returns>
public override bool Equals(object obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((Payload)obj);
}
/// <summary>
/// Returns true if Payload instances are equal
/// </summary>
/// <param name="other">Instance of Payload to be compared</param>
/// <returns>Boolean</returns>
public bool Equals(Payload other)
{
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return
(
StringProp == other.StringProp ||
StringProp != null &&
StringProp.Equals(other.StringProp)
) &&
(
DoubleProp == other.DoubleProp ||
DoubleProp != null &&
DoubleProp.Equals(other.DoubleProp)
) &&
(
EnumProp == other.EnumProp ||
EnumProp.Equals(other.EnumProp)
);
}
/// <summary>
/// Gets the hash code
/// </summary>
/// <returns>Hash code</returns>
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
var hashCode = 41;
// Suitable nullity checks etc, of course :)
if (StringProp != null)
hashCode = hashCode * 59 + StringProp.GetHashCode();
if (DoubleProp != null)
hashCode = hashCode * 59 + DoubleProp.GetHashCode();
hashCode = hashCode * 59 + EnumProp.GetHashCode();
return hashCode;
}
}
#region Operators
#pragma warning disable 1591
public static bool operator ==(Payload left, Payload right)
{
return Equals(left, right);
}
public static bool operator !=(Payload left, Payload right)
{
return !Equals(left, right);
}
#pragma warning restore 1591
#endregion Operators
}
}
Bug Report Checklist
Description
Enums are generated in models when referencing an enum, instead of just referencing the type, causing collision, and build failure. The enum is generated twice.
openapi-generator version
4.0.3
OpenAPI declaration file content or url
Command line used for generation
openapi-generator generate -i OpenApi2Docs/openapi.yml -g aspnetcore -o codegen/labelingOA3/server -c serverConfig.json
Steps to reproduce
Run command in cli
####Expected Output
####Actual Output