Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #3359
Closed
Open
Issue created Jul 14, 2019 by Administrator@rootContributor

[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?

Assignee
Assign to
Time tracking