swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
I was trying to generate asp.net core C# server code using swagger, but somehow maxLength, minLengthand patternvalidation 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 maxLengthetc
Description
I was trying to generate asp.net core C# server code using swagger, but somehow
maxLength
,minLength
andpattern
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
And the section of generated Car Model is :
As we can see that
[Required]
attribute is generated but no attribute formaxLength
etcCommand 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,
Related issues/PRs
Suggest a fix/enhancement