Skip to content

Default values for DateTime/Date/Guid result in non-compileable code #7404

@WolfgangHG

Description

@WolfgangHG

What are you generating using Kiota, clients or plugins?

API Client/SDK

In what context or format are you using Kiota?

Windows executable

Client library/SDK language

Csharp

Describe the bug

I have a REST service developed with Swashbuckle (sample project below). The OpenAPI file is thus generated by Swashbuckle.
In the service data model, I have fields that have DefaultValue attributes:

public class WeatherForecast
{
  [DefaultValue(typeof(DateTime), "1900-01-01 00:00:00")]
  public DateTime DateValue { get; set; }

  [DefaultValue(typeof(DateOnly), "1900-01-01")]
  public DateOnly DateOnlyValue { get; set; }

  [DefaultValue(typeof(Guid), "00000000-0000-0000-0000-000000000000")]
  public Guid GuidValue { get; set; }

  [DefaultValue(typeof(TimeOnly), "00:00:00")]
  public TimeOnly TimeValue { get; set; }

  [DefaultValue(15)]
  public int TemperatureC { get; set; }

  public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

  [DefaultValue("Test")]
  public string Summary { get; set; }
}

This results in this OpenAPI description:

"schemas": {
  "WeatherForecast": {
    "type": "object",
    "properties": {
      "dateValue": {
        "type": "string",
        "format": "date-time",
        "default": "1900-01-01T00:00:00"
      },
      "dateOnlyValue": {
        "type": "string",
        "format": "date",
        "default": "1900-01-01"
      },
      "guidValue": {
        "type": "string",
        "format": "uuid",
        "default": "00000000-0000-0000-0000-000000000000"
      },
      "timeValue": {
        "type": "string",
        "format": "time",
        "default": "00:00:00"
      },
      "temperatureC": {
        "type": "integer",
        "format": "int32",
        "default": 15
      },
      "temperatureF": {
        "type": "integer",
        "format": "int32",
        "readOnly": true
      },
      "summary": {
        "type": "string",
        "default": "Test",
        "nullable": true
      }
    },
    "additionalProperties": false
  }
}

(format "time" is actually OpenAPI 3.1, while my sample is 3.0)

Kiota creates this class from this file:

  public Date? DateOnlyValue { get; set; }
  public DateTimeOffset? DateValue { get; set; }
  public Guid? GuidValue { get; set; }
  public Time? TimeValue { get; set; }
  public string? Summary { get; set; }
  public int? TemperatureC { get; set; }
  public int? TemperatureF { get; private set; }

  public WeatherForecast()
  {
      DateOnlyValue = "1900-01-01";
      DateValue = "1900-01-01T00:00:00";
      GuidValue = "00000000-0000-0000-0000-000000000000";
      Summary = "Test";
      TimeValue = "00:00:00";
  }

Most of the default value assignments don't compile:

Image

And I wonder why the int field value is not initialized.

Expected behavior

The generated code should probably be:

public WeatherForecast()
{
    DateOnlyValue = new Date(DateTimeOffset.Parse("1900-01-01").Date);
    DateValue = DateTimeOffset.Parse("1900-01-01T00:00:00");
    GuidValue = Guid.Parse("00000000-0000-0000-0000-000000000000");
    TemperatureC = 15;
    Summary = "Test";
    TimeValue = new Time(DateTime.ParseExact("00:00:00", "HH:mm:ss", null));
}

How to reproduce

This is the REST service project: ASPNETCoreSwaggerDefaults.zip

Open it and start it by launching the profile "WebApiSwaggerVersion".

The client is found here: KiotaDefaults.zip

Copy the "swagger.json" file to the directory, then create the client by invoking "generate_kiota_client.bat".

Open API description file

swagger.json

Kiota Version

1.30.0

Latest Kiota version known to work for scenario above?(Not required)

No response

Known Workarounds

No response

Configuration

No response

Debug output

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    CsharpPull requests that update .net codetype:bugA broken experience

    Type

    No type

    Projects

    Status

    In Progress 🚧

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions