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
4.0.0-SNAPSHOT
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)
);
}
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:latest generate -i /local/input.yml -g csharp -o /local/out
Just generate the code and compare two DataResponseBodys where one has Anotherprop set to null.
this.Anotherprop.SequenceEqual(input.Anotherprop) will throw an ArgumentNullException.
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}};
}
馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
@fschlag thanks for reporting the issue and suggesting a fix. Please file a PR with the updated mustache template and then we can help you out with the sample update.
I opened the PR #2768
Thanks for the PR, which has been merged into master.