OpenAPITools / OpenAPITools/openapi-generator

[BUG] [csharp-netcore] Constructor accepts default null values for strings but no null check in code

Open
#15,103 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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

See the example below. The generated constructor has the signature public Capability(string dlRate = default(string), string ulRate = default(string)). If application calls this constructor with only dlRate, ulRate will be set to null. However, within the class UlRate property is not nullable: public string UlRate { get; set; }

In Validate() method, we have this code: if (false == regexUlRate.Match(this.UlRate).Success)
This creates a runtime exception when UlRate is null.

openapi-generator version

openapi-generator-cli-6.5.0-20230329.141901-70.jar from https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/6.5.0-SNAPSHOT/

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: '1.0.0'
  title: 'Regex issue'
paths: {}
components:
  schemas:
    Capability:
      properties:
        dlRate:
          $ref: '#/components/schemas/BitRate'
        ulRate:
          $ref: '#/components/schemas/BitRate'
    BitRate:
      type: string
      pattern: '^\d+(\.\d+)? (bps|Kbps|Mbps|Gbps|Tbps)$'
Generation Details
java -jar lib/openapi-generator-cli.jar generate -i RegExp.yaml -g csharp-netcore -o RegExp --additional-properties=targetFramework=net7.0 --additional-properties=packageName=Com.Example --skip-validate-spec --openapi-normalizer SIMPLIFY_BOOLEAN_ENUM=true,SIMPLIFY_ANYOF_STRING_AND_ENUM_STRING=true,REMOVE_ANYOF_ONEOF_AND_KEEP_PROPERTIES_ONLY=true,ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE=true,SIMPLIFY_ONEOF_ANYOF=true
Steps to reproduce

As above

Related issues/PRs

#15101 is also a C# regex issue

Suggest a fix

If statement can check for non-null first:
if (this.UlRate != null && false == regexUlRate.Match(this.UlRate).Success)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided generation command with RegExp.yaml and inspect the generated Capability constructor and Validate() method. Reproduce the case where only dlRate is supplied; done means validation no longer throws when the optional ulRate value is null, with the generated output or a regression test demonstrating the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.