Datamodel-code-generator: Provide default value also for required fields

Created on 23 Sep 2020  路  4Comments  路  Source: koxudaxi/datamodel-code-generator

Is your feature request related to a problem? Please describe.

Assume the following specifications:

openapi: "3.0.0"
info:
  title: Sample API
  description: API description in Markdown.
  version: 1.0.0
servers: []
paths: {}
components:
  schemas:
    test:
      type: object
      properties:
        broken:
          type: string
          default: "a string"
          description: "dummy"
      required:
        - broken

Right now, the generated Field broken will be:

broken: str = Field(..., description='dummy')

This is in line with the OpenAPI specification but especially for the purpose of generating data this is pretty annoying, because you always have to provide default values that are already known from the specification.

Describe the solution you'd like

In case of required + default, the default should still be spliced into the field. I don't see how this could harm apart from providing convenience.

Describe alternatives you've considered

If the fear is that error situations are missed when parsing inputs, an optional command line flag could be used to realize this.

Additional context
Add any other context or screenshots about the feature request here.

enhancement released

Most helpful comment

I think this can be closed now. Thanks for implementing this.

All 4 comments

@languitar
Thank you for creating this issue.
Did you want an option like --use-default ?
This option sets default value on required field.

Did you want an option like --use-default ?
This option sets default value on required field.

If there's no chance to always set the defaults, that would be ok for me.

@languitar
I have released a new version as 0.5.35
you can give --use-default to the command-line option.

I think this can be closed now. Thanks for implementing this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

languitar picture languitar  路  3Comments

languitar picture languitar  路  3Comments

languitar picture languitar  路  3Comments

ioggstream picture ioggstream  路  6Comments

pdeligia picture pdeligia  路  4Comments