OpenAPITools / OpenAPITools/openapi-generator
[BUG] [C#] Validate method throws ArgumentNullException
Nobody has claimed this yet.
- 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 (example)?
- 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?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
The model code produced by the YAML file looks like this:
/// <summary>
/// To validate all properties of the instance
/// </summary>
/// <param name="validationContext">Validation context</param>
/// <returns>Validation Result</returns>
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
// paymentId (string) pattern
Regex regexpaymentId = new Regex(@"^[a-zA-Z0-9_-]{1,36}$", RegexOptions.CultureInvariant);
if (false == regexpaymentId.Match(this.paymentId).Success)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for paymentId, must match a pattern of " + regexpaymentId, new [] { "paymentId" });
}
// targetAmount (string) pattern
Regex regextargetAmount = new Regex(@"^[0-9]{1,10}([.][0-9]{2})?$", RegexOptions.CultureInvariant);
if (false == regextargetAmount.Match(this.targetAmount).Success) // EXCEPTION HERE
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for targetAmount, must match a pattern of " + regextargetAmount, new [] { "targetAmount" });
}
[...]
The code above will throw
Value cannot be null. (Parameter 'input')</h1><br/>System.ArgumentNullException: Value cannot be null. (Parameter 'input')
at System.Text.RegularExpressions.ThrowHelper.ThrowArgumentNullException(ExceptionArgument arg)
at System.Text.RegularExpressions.Regex.Match(String input)
at PblInitRequest.System.ComponentModel.DataAnnotations.IValidatableObject.Validate(ValidationContext validationContext)
The this.targetAmount is null (optional field) but it is pushed into a Regex#Match method anyway.
openapi-generator version
6.6.0
Additionaly checked with csharp codegen (former csharp-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.jar
OpenAPI declaration file content or url
openapi: 3.0.0
paths:
/api/tpp/payment-gate/init:
post:
tags:
- PBL
summary: xxSummary
operationId: pblInitRequest
description: xxDescription
parameters: []
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PblInitRequest'
responses:
'200':
description: >-
xxDescription
content:
application/json:
schema:
$ref: '#/components/schemas/GenericInitResponseSuccess'
components:
requestBodies:
PblInitRequest:
type: object
required:
- paymentId
properties:
paymentId:
type: string
pattern: /^[a-zA-Z0-9_-]{1,36}$/
description: Unique payment id used to reference it all through the process.
targetAmount:
type: string
pattern: /^[0-9]{1,10}([.][0-9]{2})?$/
description: ------------ THIS FIELD IS OPTIONAL ------------
Generation Details
TPP_YAML = main/wwwroot/tpp/tpp.yaml
java -jar generated/openapi-generator-cli.jar generate \
-i $(TPP_YAML) \
-g csharp-netcore \
--additional-properties targetFramework=net6.0 \
--additional-properties packageName=${TPP_PKG} \
--additional-properties netCoreProjectFile=true \
--additional-properties library=generichost \
--additional-properties modelPropertyNaming=original \
--additional-properties dateTimeFormat=yyyy-MM-ddTHH:mm:ss.fff\'Z\' \
-o generated/tpp__temp
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 && ....
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the csharp-netcore generation with the supplied YAML and inspect the generated model's IValidatableObject.Validate implementation. Done means an omitted optional targetAmount no longer causes ArgumentNullException during validation, while present patterned values remain validated.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100