Skip to content

[csharp][netcore] Do not validate enum parameters#9594

Merged
wing328 merged 1 commit intoOpenAPITools:masterfrom
Blackclaws:csharp-enum-validation-fix
Jun 3, 2021
Merged

[csharp][netcore] Do not validate enum parameters#9594
wing328 merged 1 commit intoOpenAPITools:masterfrom
Blackclaws:csharp-enum-validation-fix

Conversation

@Blackclaws
Copy link
Copy Markdown
Contributor

This fixes #9586 . We do not wish to validate enum type parameters further as they are already constrained by their possible values so any validation is superfluous.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master, 5.1.x, 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@mandrean (2017/08) @frankyjuang (2019/09) @shibayan (2020/02) @Blackclaws (2021/03) @lucamazzanti (2021/05)

{{^isEnum}}
{{#maxLength}}
// {{{name}}} ({{{dataType}}}) maxLength
if(this.{{{name}}} != null && this.{{{name}}}.Length > {{maxLength}})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of not validating for enum params. What about if:
this.{{{name}}}.ToString().Length > {{maxLength}} so that it will work for other case like:

	uuid_property:
      description: This is description
      type: string
      format: uuid
      maxLength: 36

Else, it will also break at: UuidProperty.Length > 36

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what you mean, are you saying strings with format: uuid are not String objects and thus need the toString() call? Looks like a separate issue if that's what you meant.

Here the fix simply skips adding validation checks if the property specifies an enum, which is a very reliable and future-proof way, me thinks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strings with format: uuid are of Guid type in c#. The comment is valid however and worth some thought. If there is a simple way to keep validation and still have the enum behavior that might be worth exploring. However having validation for enum parameters would mean that they might break at some point in the future when one decides to use the forbidden values for the first time instead of breaking immediately.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I meant to say is Strings with format: uuid are of Guid type in c#.
And, as the above validations like MaxLength/ MinLength are specific to string type. Its better to convert to string type using ToString() before validating the property.
Similar to that I found in ruby-client -> https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/ruby-client/partial_model_generic.mustache#L302 ( to_s converts to string IIUC)

Besides, it might be separate issue but the gist of the bug is same.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guids really don't have the same issue however. Reason being that it performs its own validation. Of course the same applies you could technically have the validation parameters in the spec I guess. It just makes no sense whatsoever.

@wing328 wing328 added this to the 5.2.0 milestone Jun 3, 2021
@wing328 wing328 merged commit 439a1a9 into OpenAPITools:master Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][C#][csharp-netcore] maxLength + enum = generated code doesn't build

4 participants