Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nullable attribute #71

Open
wwwater opened this issue Dec 9, 2022 · 4 comments
Open

Add nullable attribute #71

wwwater opened this issue Dec 9, 2022 · 4 comments

Comments

@wwwater
Copy link

wwwater commented Dec 9, 2022

Hi,
in OpenAPI standard a field needs to be marked as nullable: true if it can contain null value. When not using omitNothingFields in Aeson (default setting), openapi3 lib doesn't mark these fields as nullable, but it should.

The function fromAesonOptions ignores omitNothingFields, probably it would be better to not ignore it but use to determine whether to add the nullable attribute or not.

What do you think?

@avandecreme
Copy link

I am facing this issue as well.
Relying on omitNothingFields is probably not enough because you may want to not have the same behavior for all Maybe fields of a given record. For example you may want to encode this:

data Foo = Foo
  { optionalNotNullable :: Maybe String
  , optionalNullable :: Maybe String
  , requiredNullable :: Maybe String
  , requiredNotNullable :: String
  }

Which should have a schema looking like this:

{
  "properties": {
    "optionalNotNullable": { "type": "string" },
    "optionalNullable": { "type": "string", "nullable": true },
    "requiredNullable": { "type": "string", "nullable": true },
    "requiredNotNullable":  { "type": "string" }
  },
  "required": ["requiredNullable", "requiredNotNullable"],
  "type": "object"
}

I can't think of a good suggestion on how to proceed though.

@bgohla
Copy link

bgohla commented Jul 14, 2023

One solution might be to use newtype wrappers for the field types, and typeclass instances, that implement each of the possible behaviors.

@stevladimir
Copy link
Contributor

Just to note: IIRC nullable keyword was dropped in the latest (3.1) OpenAPI specification, so it may be of little sense to add support for it now.

@pbrisbin
Copy link

Came here because I'm also currently being bitten by this. I think it's still worth tracking this feature even though 3.1 is dropping nullable because in 3.1 you would represent what used to be:

foo:
  type: string
  nullable: true

By instead doing:

foo:
  type:
    anyOf:
      - type: string
      - type: "null"

So there is still a feature here about changing the spec based on omitNothingFields; it's just that for 3.0 it's "add nullable" and for 3.1 it's "switch to anyOf with a type: null element".

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

No branches or pull requests

5 participants