Skip to content

[BUG][csharp-netcore][csharp] minLength on a date-formatted string causes compile errors due to checking length of an DateTime object #20650

@juho-turunen-tampuuri

Description

@juho-turunen-tampuuri
Description

OpenAPI document with date or date-time formatted string with minLength restriction causes compile errors.

Same for maxLength.

format: date causes openapi-generator to generate DateTime or DateTimeOffset types but minLength causes .Length comparison which doesn't compile.

Generated code snippet

// DecisionDate (DateTimeOffset) minLength
if (this.DecisionDate != null && this.DecisionDate.Length < 1) //compile error 'DateTimeOffset' does not contain a definition for 'Length'
{
    yield return new ValidationResult("Invalid value for DecisionDate, length must be greater than 1.", new [] { "DecisionDate" });
}

// ConfirmationDate (DateTimeOffset) minLength
if (this.ConfirmationDate != null && this.ConfirmationDate.Length < 1) //compile error 'DateTimeOffset' does not contain a definition for 'Length'
{
    yield return new ValidationResult("Invalid value for ConfirmationDate, length must be greater than 1.", new [] { "ConfirmationDate" });
}

Related to RicoSuter/NSwag#4302 and OAI/OpenAPI-Specification#2827.

See the above NSwag issue for workaround if using NSwag.

openapi-generator version

7.11.0, tried also wit 7.9.0

OpenAPI declaration file content or url

Snippet:

MyObject:
 type: object
 additionalProperties: false
 required:
 - decisionDate
 - confirmationDate
 properties:
   decisionDate:
     type: string
     format: date
     minLength: 1
   confirmationDate:
     type: string
     format: date
     minLength: 1
Generation Details

Using npm @openapitools/openapi-generator-cli@2.16.3

npx openapi-generator-cli generate -i swagger.yaml -g csharp --additional-properties targetFramework=netstandard2.0 --additional-properties useDateTimeOffset=true --additional-properties disallowAdditionalPropertiesIfNotPresent=false

useDateTimeOffset=false doesn't fix doesn't fix the error, same check is done against DateTime

Suggest a fix

In my opinion if string format is set to date or date-time Length shouldn't be checked.

Even though having minLenght on a date is a bit unnecessary the minLength restriction is done against the type of the property, not the format, and is valid in OpenAPI Specification. At least that's how I read this comment: OAI/OpenAPI-Specification#2827 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions