swagger-api / swagger-api/swagger-codegen

strange behaviour for primitive type string

Open
#8,165 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I have a swagger file with these definitions

definitions:
AssetItem:
type: string
VinylsCoverProperty:
type: object
additionalProperties: false
properties:
iv:
type: array
description: Invariant
items:
$ref: '#/definitions/AssetItem'

the C# code generated handle the iv property as a collection of AssetItem objects, instead of a collection of strings.
The problem is that deserialization throws an error because the string obtained in the response from the api cannot be casted to AssetItem object.

This is the AssetItem class generated:

    [DataContract]
    public partial class AssetItem :  IEquatable<AssetItem>, IValidatableObject
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AssetItem" /> class.
        /// </summary>
        [JsonConstructorAttribute]
        public AssetItem()
        {
        }
        
        /// <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 AssetItem {\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="input">Object to be compared</param>
        /// <returns>Boolean</returns>
        public override bool Equals(object input)
        {
            return this.Equals(input as AssetItem);
        }

        /// <summary>
        /// Returns true if AssetItem instances are equal
        /// </summary>
        /// <param name="input">Instance of AssetItem to be compared</param>
        /// <returns>Boolean</returns>
        public bool Equals(AssetItem input)
        {
            if (input == null)
                return false;

            return false;
        }

        /// <summary>
        /// Gets the hash code
        /// </summary>
        /// <returns>Hash code</returns>
        public override int GetHashCode()
        {
            unchecked // Overflow is fine, just wrap
            {
                int hashCode = 41;
                return hashCode;
            }
        }

        /// <summary>
        /// To validate all properties of the instance
        /// </summary>
        /// <param name="validationContext">Validation context</param>
        /// <returns>Validation Result</returns>
        IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
        {
            yield break;
        }
    }

As you can see, there are no member fields nor properties.

Swagger-codegen version

swagger-editor-3.2.5

Command line used for generation

Swagger Editor -> Generate Client -> csharp

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the supplied Swagger definitions through Swagger Editor → Generate Client → csharp, then inspect the generated AssetItem class and VinylsCoverProperty.iv type. Trace how the primitive string definition is represented during C# client generation. Done means AssetItem is treated as a string and the generated collection deserializes API string values without a cast error.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.