swagger-api / swagger-api/swagger-codegen
[C#] Nullable type generated for required integer property in model
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
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
- Use the above petstore.yaml
- 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 - Observe that the
idproperty in theOrdermodel is generated aslong?instead oflong.
/// <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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the generated C# client with modules/swagger-codegen/src/test/resources/2_0/petstore.yaml and the documented Docker command. Inspect the C# generation path for how the required Order.id property is rendered. Done means the generated id property is long rather than long? while optional integer properties remain nullable.
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
- 42/100