Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #2760
Closed
Open
Issue created Apr 29, 2019 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[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 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}};
         }
Assignee
Assign to
Time tracking