OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Csharp] Validating DateTime format exception

Open
#21,452 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?

This is my first issue opened for an open-source project, so please let me know if I'm doing something wrong. Also I'm somewhat new in C# and using the OpenApi framework, so maybe I have misunderstood some things as well.

Description

When a class is generated that contains a DateTime object, that is specified by a patten, it seems as though a validation method is created for the class, which checks if the DateTime object follows the pattern. But the pattern is a string and the object is a DateTime, so the error CS1503: Argument 1: cannot convert from 'System.DateTime' to 'string' error is thrown.

To be more precise. The errornous Json part describing the API;

"Foo":{"title":"Foo","required":["myDateTime"],"type":"object","properties":{"myDateTime":{"pattern":"^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:00.000Z$","type":"string","description":"The time in UTC format","format":"date-time"}},"x-examples":{}}

The auto-generated validation method (using net9.0):

IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
    if (this.myDateTime != null) {
        // myDateTime (DateTime) pattern
        Regex regexMyDateTime = new Regex(@"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:00.000Z$", RegexOptions.CultureInvariant);
        if (!regexMyDateTime.Match(this.myDateTime).Success)
        {
            yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for myDateTime, must match a pattern of " + regexMyDateTime, new [] { "myDateTime" });
        }
    }
{
openapi-generator version

7.13.0

Suggest a fix

I don't know what the best solution for this would be. But I have 2 different thoughts of how to "fix" this (or atleast making it not throw and Exception);

  • Do not implement validation on DateTime objects in regards to pattern.
  • OR create the myDateTime object as a string instead of a DateTime object if the pattern specification is present.

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

Start by reproducing the supplied OpenAPI schema with the C# generator and inspect the generated IValidatableObject.Validate method for the DateTime property. Check how pattern validation is generated for date-time fields; done means the generated C# code compiles and handles the pattern without passing DateTime to a string-based regex.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.