swagger-api / swagger-api/swagger-codegen

C# codegen with pattern is not null-safe

Open
#11,710 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Generating C# code from a Swagger file, with regular expressions, is incorrect.

We are consuming a Swagger file from a third-party. The model is very complex and contains regular expressions.

Our issue occurs in the generated method

IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)

For objects which are null the regex blows up, for example:

            // SomeProp (string) pattern
            Regex regexSomeProp = new Regex(@"^[A-Z0-9]{4,4}[A-Z]{2,2}[A-Z0-9]{2,2}[A-Z0-9]{3,3}$", RegexOptions.CultureInvariant);
            if (false == regexSomeProp.Match(this.SomeProp).Success)
            {
                yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for SomeProp, must match a pattern of " + regexSomeProp, new [] { "SomeProp" });
            }

In this instance, this.SomeProp is null which causes an ArgumentNullException.

The Swagger spec is also ambiguous.

https://swagger.io/docs/specification/data-models/data-types/#string

Note that an empty string "" is a valid string unless minLength or pattern is specified.

The payload being validated has the property SomeProp not present, so it effect it is null as opposed to empty.

The vendor we are integrating with is of the position null != empty and therefore they are not in violation of the contract.

Swagger-codegen version

2.4.23

Swagger declaration file content or url

Sorry, I can't share any more than I have (which includes the regex) - this Swagger is confidential and I can't reproduce it.

Command line used for generation

java -jar swagger-codegen-cli.jar generate -i swagger.json -l csharp

Steps to reproduce

Any Swagger with a pattern where the backing property is null will repro this.

Related issues/PRs

N/A

Suggest a fix/enhancement

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 generating C# code with the command shown from a Swagger schema containing a patterned string property that is absent or null, then inspect the generated IValidatableObject.Validate method. Done means validation of that property no longer raises ArgumentNullException when its value is null, while still handling the pattern case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.