swagger-api / swagger-api/swagger-codegen-generators

[C#] Missing property validation

Open
#582 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

Description

At some point, property validation as introduced in #3552 has not been generated anymore.

Swagger-codegen version

3.0.15

Swagger declaration file content or url
openapi: 3.0.0
info:
  version: '2.0'
  title: Test
components:
  schemas:
    modelResourceAttributes:
      type: object
      required:
        - fancyParam
        - otherParam
        - mode
      properties:
        name:
          description: A short name of the model.
          type: string
        fancyParam:
          description: The first important parameter.
          type: string
          enum:
            - Default
            - Default 2
        otherParam:
          description: Another important parameter.
          type: string
          enum:
            - Default
            - SomethingElse
        mode:
          description: Some integer.
          type: integer
          format: int32
          minimum: 1
          maximum: 5
Command line used for generation

java -DdebugModels -Dmodels -jar swagger-codegen-cli-3.0.15.jar generate -DtargetFramework=v4.5 -l csharp -i "<path-to-spec>"

Steps to reproduce
  1. Generate the C# files using the command line above
  2. Open the file ModelResourceAttributes.cs
Actual result
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)

has no logic for property mode.

Expected result
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)

has logic for property mode.

Related issues/PRs

#9774
#4338
#2663

Suggest a fix/enhancement

Since the validation code has not been changed AFAIK, I started to inspect the verbose output of the generator and it seems that the tag hasValidation which was still there in Swagger codegen version 2.x is not there anymore in Swagger codegen version 3.x. Instead, a new vendor extension x-has-validation has been introduced with #265.

We probably only have to replace hasValidation with vendorExtensions.x-has-validation in modelGeneric.mustache.

I will try to file a PR within the next days.

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 with handlebars/csharp/modelGeneric.mustache and compare its validation condition with the x-has-validation vendor extension described in the issue. Generate ModelResourceAttributes.cs from the provided OpenAPI schema using the stated CLI command, then verify that Validate includes logic for the mode property and its minimum and maximum constraints.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.