[ASP .NET Core] Support default values for model properties.
Created by: james-beer
Description
Generated models in ASP .NET Core projects do not have default values for properties using the default
schema element. It appears this is supported in some other generators, e.g. the Python Flask generator.
openapi-generator version
master
branch.
OpenAPI declaration
openapi: '3.0.0'
info:
version: 1.0.0
title: API with Default Values
servers:
- url: http://localhost:8080
paths:
/test:
post:
operationId: testDefaults
requestBody:
required: true
content:
application/json:
schema:
'$ref': '#/components/schemas/Payload'
responses:
'200':
description: Ok
components:
schemas:
Payload:
type: object
properties:
stringProp:
type: string
default: foo
doubleProp:
type: number
format: double
default: 1.234
enumProp:
type: string
enum: [One, Two, Three]
default: Two
Current ASP .NET Core codegen
Command:
./run-in-docker.sh generate -i ./test/openapi.yaml -o ./test/aspnetcore -g aspnetcore --additional-properties aspnetCoreVersion=2.2
Model properties (no initialization):
[DataContract]
public partial class Payload : IEquatable<Payload>
{
/// <summary>
/// Gets or Sets StringProp
/// </summary>
[DataMember(Name="stringProp", EmitDefaultValue=false)]
public string StringProp { get; set; }
/// <summary>
/// Gets or Sets DoubleProp
/// </summary>
[DataMember(Name="doubleProp", EmitDefaultValue=false)]
public double? DoubleProp { get; set; }
/// <summary>
/// Gets or Sets EnumProp
/// </summary>
[DataMember(Name="enumProp", EmitDefaultValue=false)]
public EnumPropEnum? EnumProp { get; set; }
}
Python Flask codegen (for comparison)
Command:
./run-in-docker.sh generate -i ./test/openapi.yaml -o ./test/python -g python-flask
Model ctor signature:
class Payload(Model):
def __init__(self, string_prop='foo', double_prop=1.234, enum_prop='Two'):
Enhancement
I've added property initialization to the model.mustache
templates in a local branch. Should I raise a PR?