Skip to content
New issue

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

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

Already on GitHub? # to your account

[BUG] [aspnetcore] Duplicate Enums are being generated rather than enum type being referenced in model #3582

Closed
5 of 6 tasks
edag94 opened this issue Aug 7, 2019 · 2 comments

Comments

@edag94
Copy link

edag94 commented Aug 7, 2019

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
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
    }
}

@auto-labeler
Copy link

auto-labeler bot commented Aug 7, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@edag94 edag94 changed the title [BUG] [aspnetcore] Enums are generated in models when referencing an enum, causing collision [BUG] [aspnetcore] Duplicate Enums are being generated rather than enum type being referenced in model Aug 9, 2019
@edag94
Copy link
Author

edag94 commented Aug 26, 2019

#3622 This PR fixed this issue, thanks @frankyjuang

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

No branches or pull requests

1 participant