Skip to content

[Suggestion] Generated model properties should respect nullable and required options #52

Description

@gratzl-dev

Hello!

Currently all component properties are treated as non-nullable required fields, regardless of the required schema configurations and only setting fields nullable if nullable is explicitly set to "true".

I would suggest treating all fields as nullable, non required except when specifically specified as such.

For example, the output for this LoginDTO

      "LoginDTO": {
        "type": "object",
        "properties": {
          "username": {
            "type": "string"
          },
          "password": {
            "type": "string"
          }
        }
      },

currently is

    @JsonKey(name: LoginDTO.usernameKey) required String username,
    @JsonKey(name: LoginDTO.passwordKey) required String password,

even though neither of the fields are set as required in the spec

With my suggestion it would be generated to this:

    @JsonKey(name: LoginDTO.usernameKey) String? username,
    @JsonKey(name: LoginDTO.passwordKey) String? password,

And to generate the required properties we would use a spec such as:

      "LoginDTO": {
        "type": "object",
        "properties": {
          "username": {
            "type": "string"
          },
          "password": {
            "type": "string"
          }
        },
        "required": ["username", "password"]
      },

otherwise these fields should be treated as non required and nullable.

One exception to this would be if "nullable" is explicitly set to "false"

For example, this

      "LoginDTO": {
        "type": "object",
        "properties": {
          "username": {
            "type": "string",
            "nullable": false
          },
          "password": {
            "type": "string"
          }
        },
        "required": [
          "password"
        ]
      },

would then result to

    @JsonKey(name: LoginDTO.usernameKey) String username,
    @JsonKey(name: LoginDTO.passwordKey) required String password,

Thoughts or suggestions on this?

I also modified the code in my fork to behave as suggested.
Maybe make the behaviour configurable?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions