[BUG][C#] Additional null check before SequenceEqual
Created by: fschlag
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
The Equals
method in generated classes does a SequenceEqual
when comparing objects (including fields of the object) with each other. If the input object has a field that potentially can be null, there is an additional null check required to avoid ArgumentNullException
when passing null
to Enumerable.SequenceEqual method.
Enumerable.SequenceEqual Method description at Microsoft
openapi-generator version
4.0.0-SNAPSHOT
OpenAPI declaration file content or url
openapi: 3.0.0
info:
version: 1.0.0
title: Test
paths:
/data:
get:
description: ..
operationId: getData
responses:
"200":
description: ...
content:
application/json:
schema:
$ref: "#/components/schemas/DataResponseBody"
components:
schemas:
DataResponseBody:
required:
- testprop
properties:
testprop:
type: string
anotherprop: # This could be null
type: object
additionalProperties:
$ref: "#/components/schemas/SomeComponent"
SomeComponent:
type: object
properties:
test1:
type: string
This generates an Equals
method in DataResponseBody.cs
like the following:
public bool Equals(DataResponseBody input)
{
if (input == null)
return false;
return
(
this.Testprop == input.Testprop ||
(this.Testprop != null &&
this.Testprop.Equals(input.Testprop))
) &&
(
this.Anotherprop == input.Anotherprop ||
this.Anotherprop != null &&
this.Anotherprop.SequenceEqual(input.Anotherprop)
);
}
Command line used for generation
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:latest generate -i /local/input.yml -g csharp -o /local/out
Steps to reproduce
Just generate the code and compare two DataResponseBody
s where one has Anotherprop
set to null
.
this.Anotherprop.SequenceEqual(input.Anotherprop)
will throw an ArgumentNullException
.
Related issues/PRs
Suggest a fix
I edited the following mustache templates to fix the bug locally, but I need someone with more experience to merge this into the master branch. I ran bin/csharp-petstore-all.sh
but I had a lot of modified files afterwards. Maybe someone can help!
modules/openapi-generator/src/main/resources/csharp-netcore/modelGeneric.mustache:
--- a/modules/openapi-generator/src/main/resources/csharp-netcore/modelGeneric.mustache
+++ b/modules/openapi-generator/src/main/resources/csharp-netcore/modelGeneric.mustache
@@ -193,6 +193,7 @@
(
this.{{name}} == input.{{name}} ||
this.{{name}} != null &&
+ input.{{name}} != null &&
this.{{name}}.SequenceEqual(input.{{name}})
){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{/vars}}{{^vars}}{{#parent}}base.Equals(input){{/parent}}{{^parent}}false{{/parent}}{{/vars}};
{{/useCompareNetObjects}}
modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache:
--- a/modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache
+++ b/modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache
@@ -171,6 +171,7 @@ this.{{name}} = {{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}};
(
this.{{name}} == input.{{name}} ||
this.{{name}} != null &&
+ input.{{name}} != null &&
this.{{name}}.SequenceEqual(input.{{name}})
){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{/vars}}{{^vars}}{{#parent}}base.Equals(input){{/parent}}{{^parent}}false{{/parent}}{{/vars}};
}