Openapi-generator: [BUG][C#] Additional null check before SequenceEqual

Created on 29 Apr 2019  路  4Comments  路  Source: OpenAPITools/openapi-generator

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] What's the version of OpenAPI Generator used?
  • [x] Have you search for related issues/PRs?
  • [x] 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 DataResponseBodys 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}};
         }

C-Sharp Bug

All 4 comments

馃憤 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.

Was this page helpful?
0 / 5 - 0 ratings