dedoc/scramble

[PRO] Any of is generated instead of "or null"

Closed this issue · 2 comments

image

From:

public ?MediaUploadDto $coverImage;

MediaUploadDto is a Laravel-Data class.

All ?string, ?int, ?etc. cases are "or null", even collections:

    #[DataCollectionOf(MediaUploadDto::class)]
    public ?DataCollection $images;
image

Hey @manuel-watchenterprise,

Any nullable collections are documented like array schemas:

{
  "type": ["array", "null"],
  "items": {"$ref": "#/components/schemas/SomeRef"}
}

You can see that null is used in type property: JSON Schema allows doing that. This gives you a nice documentation, something that looks like SomeRef[] or null.

Simple types, like ?string or ?int can also be defined using this schema which will give you string or null in docs:

{
  "type": ["string", "null"]
}

When we need to make any object reference nullable, not array, the only way to do it is to use anyOf (or oneOf):

{
  "anyOf": [
    {"type": "null"},
    {"$ref": "#/components/schemas/SomeRef"}
  ]
}

This is logically 100% identical (here I may be mistaken about 100%, but it is the same in terms of logic - both null and SomeRef will pass the validation) to that way how nullable arrays are defined. However, in Stoplight Elements, you can see that the UI is different - there is that dropdown menu.

So sadly this duality is something inherited from OpenAPI spec itself and is not related to the generation side.

Feel free to re-open if I missed something.

Thank you for the clarification!