swagger-api / swagger-api/swagger-codegen

C# codegen maxlength validation missing

Open
#9,774 1 comment 2 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

I was trying to generate asp.net core C# server code using swagger, but somehow maxLength, minLength and pattern validation are not appearing on generated c# class, I assume that it will be kind of attribute on properties like I am getting [Required] attribute for model validation.

Swagger-codegen version

2.4.6-SNAPSHOT

Swagger declaration file content or url
swagger: '2.0'
info:
  title: My Cars API
  description: Simple API for demonstrating json validation
  version: 1.0.0
host: localhost
schemes:
  - http
produces:
  - application/json
paths:
  /cars:
    post:
      summary: Add a new (dream) car
      description: |
        Adds a new car to the database.
      tags:
        - Cars
      parameters:
        - name: car
          schema:
            $ref: '#/definitions/Car'
          in: body
          required: true
          description: the new dream car
      responses:
        '200':
          description: Succeeded
        '500':
          description: unexpected error
definitions:
  Registration:
    type: object
    required:
    - state
    - plate_number
    properties:
      state:
        type: string
        description: Two letter state abbreviation code
        pattern: '^[A-Z]{2}$'
      plate_number:
        type: string
        description: The license plate number
        minLength: 1
        maxLength: 8
  Car:
    type: object
    required:
    - make
    - model
    - model_year
    - doors
    - vin
    - color
    properties:
      make:
        type: string
        description: The name of the vehicle manufacturer
      model:
        type: string
        description: The name of the vehicle model
        maxLength: 10
      model_year:
        type: string
        description: The manufacturers model year
        pattern: '^[0-9]{2,4}$'
        minLength: 2
      doors:
        type: integer
        description: The number of doors
        minimum: 2
        maximum: 5
      vin:
        type: string
        description: The vehicle VIN number
        pattern: '^[a-zA-Z0-9]{17}$'
      color:
        type: string
        description: Manufacturers vehicle color
      registration:
        $ref: '#/definitions/Registration'

And the section of generated Car Model is :

namespace IO.Swagger.Models
{ 
    /// <summary>
    /// 
    /// </summary>
    [DataContract]
    public partial class Car : IEquatable<Car>
    { 
        /// <summary>
        /// The name of the vehicle manufacturer
        /// </summary>
        /// <value>The name of the vehicle manufacturer</value>
        [Required]
        [DataMember(Name="make")]
        public string Make { get; set; }

        /// <summary>
        /// The name of the vehicle model
        /// </summary>
        /// <value>The name of the vehicle model</value>
        [Required]
        [DataMember(Name="model")]
        public string Model { get; set; }

        /// <summary>
        /// The manufacturers model year
        /// </summary>
        /// <value>The manufacturers model year</value>
        [Required]
        [DataMember(Name="model_year")]
        public string ModelYear { get; set; }

        /// <summary>
        /// The number of doors
        /// </summary>
        /// <value>The number of doors</value>
        [Required]
        [DataMember(Name="doors")]
        public int? Doors { get; set; }

        /// <summary>
        /// The vehicle VIN number
        /// </summary>
        /// <value>The vehicle VIN number</value>
        [Required]
        [DataMember(Name="vin")]
        public string Vin { get; set; }

        /// <summary>
        /// Manufacturers vehicle color
        /// </summary>
        /// <value>Manufacturers vehicle color</value>
        [Required]
        [DataMember(Name="color")]
        public string Color { get; set; }

        /// <summary>
        /// Gets or Sets Registration
        /// </summary>
        [DataMember(Name="registration")]
        public Registration Registration { get; set; }
    }
}

As we can see that [Required] attribute is generated but no attribute for maxLength etc

Command line used for generation

java -jar swagger-codegen-cli.jar generate -i mycar.json -l aspnetcore -o samples/server

Steps to reproduce

Define a schema property with maxLength attribute like this,

 model:
        type: string
        description: The name of the vehicle model
        maxLength: 10
Related issues/PRs
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 reproducing the issue with the provided Swagger definition and the java -jar swagger-codegen-cli.jar generate -i mycar.json -l aspnetcore -o samples/server command. Inspect the generated C# Car model and the aspnetcore code-generation path; done means the declared maxLength, minLength, and pattern validations appear in the generated model properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.