question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Differentiate "required with default", "required nullable", and "optional" properties

See original GitHub issue

⚠️ I’m willing to work on this change myself. But before starting, I’d like to discuss it.

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

Here’s a repro: https://github.com/goodoldneon/datamodel-code-generator-Repro

OpenAPI allows you to specify properties as:

  • Required
  • Required with default
  • Required nullable
  • Unrequired

However, mashalling the pydantic models (e.g. BaseModel.dict()) will not differentiate them.

For example, let’s say we have the following OpenAPI schema:

Foo:
  type: object
  properties:
    required:
      type: integer
    required_default:
      type: integer
      default: 2
    required_nullable:
      type: integer
      nullable: true
    unrequired:
      type: integer
  required:
    - required
    - required_default
    - required_nullable

If I marshall the generated model into JSON, I expect that all of the required* properties will always exist but the unrequired property may not.

If I instantiate Foo like this:

Foo(required=1, required_nullable=None)

Then I’d expect the marshalled JSON to be:

{
  "required": 1,
  "required_default": 2,
  "required_nullable": null,
}

But that doesn’t seem to be possible.

Describe the solution you’d like

I thought I could do this with a new BaseModel.dict() argument, but the maintainers of pydantic rejected my PR:

https://github.com/samuelcolvin/pydantic/discussions/2837

They provided a (seemingly) good solution to the problem on their end. This makes me think the solution for datamodel-code-generator would be a new CLI arg that creates models like @PrettyWood’s example.

Describe alternatives you’ve considered

I considered making a change to pydantic but my PR was rejected.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
goodoldneoncommented, Jun 6, 2021

Sounds good! I’ll work on it this week

1reaction
goodoldneoncommented, Jan 3, 2022

@koxudaxi sorry I’ve slept on this for so long. I decided to give it another shot in #670. There’s still some work left, but I was hoping to get some feedback from you on what I have so far. Thanks for taking a look when you get the chance!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Optional and Nullable Properties in API Requests
Learn how optional and nullable properties can be used flexibly in combinations ... Required, Optional, and Nullable Properties in OpenAPI.
Read more >
Use of optional vs required nullable attributes in schema #275
i'm of the opinion that if an optional attribute does not have a value it should not be required to send in the...
Read more >
What does it mean for a property to be [Required] and nullable?
The reason for making a property nullable and marked with the [Required] attribute is to protect against under-posting attacks.
Read more >
Optional vs Nullable In Typescript/Angular - Upmostly
This says that the MyProperty is optional, and if no value is given, that the value will be set to “undefined”. It's an...
Read more >
Entity Properties - EF Core | Microsoft Learn
Required and optional properties ... A property is considered optional if it is valid for it to contain null . If null is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found