Skip to content

Conversation

rasheedja
Copy link

@rasheedja rasheedja commented Jul 29, 2024

Duplicate of #91 <-- Use this PR, it's more thorough

Modifies instance ToSchema a => ToSchema (Maybe a) to not ignore the Maybe. Related to #71 .

  • Generate a schema for a
  • Create a schema with the null type
  • Combine both schemas with a oneOf to emulate Maybe a (should we use anyOf instead?)
  • Follows behaviour of other Maybe instances, e.g., ToJSON

I went with the oneOf approach rather than using nullable as this is compatible with OpenAPI 3.1.

Without these changes, something like Maybe (String, String) would generate a schema like this:

"application/json;charset=utf-8": {
    "schema": {
        "items": [
            {
                "type": "string"
            },
            {
                "type": "string"
            }
        ],
        "maxItems": 2,
        "minItems": 2,
        "type": "array"
    }
}

And with these changes:

"application/json;charset=utf-8": {
    "schema": {
        "oneOf": [
            {
                "items": [
                    {
                        "type": "string"
                    },
                    {
                        "type": "string"
                    }
                ],
                "maxItems": 2,
                "minItems": 2,
                "type": "array"
            },
            {
                "type": "null"
            }
        ]
    }
}

@rasheedja rasheedja changed the title Add null to Maybe a schemas [wip] Add null to Maybe a schemas Jul 29, 2024
+ Generate a schema for `a`
+ Generate a schema with the `null` type
+ Combine both schemas with a `oneOf` to emulate `Maybe a`
+ Follows behaviour of other `Maybe` instances, e.g., `ToJSON`
@rasheedja rasheedja force-pushed the junaidr/maybes-and-nulls branch from 561d436 to 837a7b6 Compare July 29, 2024 13:58
@rasheedja rasheedja changed the title [wip] Add null to Maybe a schemas Add null to Maybe a schemas Jul 29, 2024
@stevladimir
Copy link
Contributor

I think this duplicates #91

@rasheedja
Copy link
Author

rasheedja commented Jul 29, 2024

@stevladimir Yep, it does, thanks for pointing that out. I'll switch this one to use anyOf after seeing the issue with nested types.

Your branch is more thorough, though. I'll try out your branch and test that it generates the expected specs for me

EDIT: It does, I'm using your branch for my use case right now.

@rasheedja rasheedja changed the title Add null to Maybe a schemas [Duplicate] Add null to Maybe a schemas Jul 29, 2024
@rasheedja rasheedja marked this pull request as draft July 29, 2024 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants