Skip to content

[C#] Nullable type generated for required integer property in model #12540

Open
@VBA-N7

Description

@VBA-N7

Description

When generating a C# client from a Swagger definition, properties marked as required in the schema and defined as type: integer with format: int64 are still generated as nullable types (long?) in the model.

Swagger Definition Snippet

definitions:
  Order:
    title: Pet Order
    description: An order for pets from the pet store
    type: object
    required:
      - id
    properties:
      id:
        type: integer
        format: int64
      petId:
        type: integer
        format: int64
      ...

Steps to Reproduce

  1. Use the above petstore.yaml
  2. Run the following Docker command:
    docker run --rm -v $(pwd):/local swaggerapi/swagger-codegen-cli-v3 generate \
      -i /local/petstore.yaml \
      -l csharp \
      -o /local/out/csharp-client
  3. Observe that the id property in the Order model is generated as long? instead of long.
        /// <summary>
        /// Initializes a new instance of the <see cref="Order" /> class.
        /// </summary>
        /// <param name="id">id (required).</param>
        /// <param name="petId">petId.</param>
        /// <param name="quantity">quantity.</param>
        /// <param name="shipDate">shipDate.</param>
        /// <param name="status">Order Status.</param>
        /// <param name="complete">complete (default to false).</param>
        public Order(long? id = default(long?), long? petId = default(long?), int? quantity = default(int?), DateTime? shipDate = default(DateTime?), StatusEnum? status = default(StatusEnum?), bool? complete = false)
        {
            // to ensure "id" is required (not null)
            if (id == null)
            {
                throw new InvalidDataException("id is a required property for Order and cannot be null");
            }
            else
            {
                this.Id = id;
            }
            this.PetId = petId;
            this.Quantity = quantity;
            this.ShipDate = shipDate;
            this.Status = status;
            // use default value if no "complete" provided
            if (complete == null)
            {
                this.Complete = false;
            }
            else
            {
                this.Complete = complete;
            }
        }
        
        /// <summary>
        /// Gets or Sets Id
        /// </summary>
        [DataMember(Name="id", EmitDefaultValue=false)]
        public long? Id { get; set; } // WHY NULLABLE?

        /// <summary>
        /// Gets or Sets PetId
        /// </summary>
        [DataMember(Name="petId", EmitDefaultValue=false)]
        public long? PetId { get; set; }

        /// <summary>
        /// Gets or Sets Quantity
        /// </summary>
        [DataMember(Name="quantity", EmitDefaultValue=false)]
        public int? Quantity { get; set; }

        /// <summary>
        /// Gets or Sets ShipDate
        /// </summary>
        [DataMember(Name="shipDate", EmitDefaultValue=false)]
        public DateTime? ShipDate { get; set; }


        /// <summary>
        /// Gets or Sets Complete
        /// </summary>
        [DataMember(Name="complete", EmitDefaultValue=false)]
        public bool? Complete { get; set; }
...

Expected Behavior

The id property should be generated as long (non-nullable) since it is marked as required in the schema.

Observed Behavior

The id property is generated as long? (nullable), which causes unnecessary nullability checks and does not align with the schema definition.

Swagger Codegen Version

  • Version: 3.x

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions