Open witoldsz opened 1 year ago
This simple fix seems to work, for me at least:
$ git diff
diff --git a/modules/openapi-generator/src/main/resources/csharp/validatable.mustache b/modules/openapi-generator/src/main/resources/csharp/validatable.mustache
index cf24f2f94fd..f0e167c41a7 100644
--- a/modules/openapi-generator/src/main/resources/csharp/validatable.mustache
+++ b/modules/openapi-generator/src/main/resources/csharp/validatable.mustache
@@ -75,7 +75,7 @@
{{^isByteArray}}
// {{{name}}} ({{{dataType}}}) pattern
Regex regex{{{name}}} = new Regex(@"{{{vendorExtensions.x-regex}}}"{{#vendorExtensions.x-modifiers}}{{#-first}}, {{/-first}}RegexOptions.{{{.}}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}});
- if (false == regex{{{name}}}.Match(this.{{{name}}}{{#isUuid}}.ToString(){{/isUuid}}).Success)
+ if (this.{{{name}}} != null && false == regex{{{name}}}.Match(this.{{{name}}}{{#isUuid}}.ToString(){{/isUuid}}).Success)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, must match a pattern of " + regex{{{name}}}, new [] { "{{{name}}}" });
}
if (this.{{{name}}} != null &&
was added before false == regex{{{name}}}.Match(…
Can you please file a PR with the suggested fix? Thanks.
Can you please file a PR with the suggested fix? Thanks.
https://github.com/OpenAPITools/openapi-generator/pull/16021
Bug Report Checklist
Description
The model code produced by the YAML file looks like this:
The code above will throw
The
this.targetAmount
isnull
(optional field) but it is pushed into aRegex#Match
method anyway.openapi-generator version
6.6.0 Additionaly checked with
csharp
codegen (formercsharp-core
) of https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/7.0.0-SNAPSHOT/openapi-generator-cli-7.0.0-20230703.163913-170.jarOpenAPI declaration file content or url
Generation Details
Related issues/PRs
Somehow related: #2760
Suggest a fix
Missing optional fields should not be validated against patterns (and possibly other formatting rules). Or maybe validation should always be protected with
if (this.{{{name}}} != null && ...
.