C# codegen with pattern is not null-safe
Description
Generating C# code from a Swagger file, with regular expressions, is incorrect.
We are consuming a Swagger file from a third-party. The model is very complex and contains regular expressions.
Our issue occurs in the generated method
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
For objects which are null the regex blows up, for example:
// SomeProp (string) pattern
Regex regexSomeProp = new Regex(@"^[A-Z0-9]{4,4}[A-Z]{2,2}[A-Z0-9]{2,2}[A-Z0-9]{3,3}$", RegexOptions.CultureInvariant);
if (false == regexSomeProp.Match(this.SomeProp).Success)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for SomeProp, must match a pattern of " + regexSomeProp, new [] { "SomeProp" });
}
In this instance, this.SomeProp is null which causes an ArgumentNullException.
The Swagger spec is also ambiguous.
https://swagger.io/docs/specification/data-models/data-types/#string
Note that an empty string "" is a valid string unless minLength or pattern is specified.
The payload being validated has the property SomeProp not present, so it effect it is null as opposed to empty.
The vendor we are integrating with is of the position null != empty and therefore they are not in violation of the contract.
Swagger-codegen version
2.4.23
Swagger declaration file content or url
Sorry, I can't share any more than I have (which includes the regex) - this Swagger is confidential and I can't reproduce it.
Command line used for generation
java -jar swagger-codegen-cli.jar generate -i swagger.json -l csharp
Steps to reproduce
Any Swagger with a pattern where the backing property is null will repro this.
Related issues/PRs
N/A