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

Handle Nullable types #20

Closed
m-wild opened this issue Aug 4, 2020 · 4 comments
Closed

Handle Nullable types #20

m-wild opened this issue Aug 4, 2020 · 4 comments
Labels
💡 enhancement New feature or request

Comments

@m-wild
Copy link
Collaborator

m-wild commented Aug 4, 2020

See #16

Also, we should probably be handling Nullable differently to other generics...
Json Schema allows for { "type": "null" }, see: https://json-schema.org/understanding-json-schema/reference/null.html
Which means we could detect System.Nullable and set the property as { "type": [ "T", "null" ] }

@m-wild m-wild added 💡 enhancement New feature or request help wanted labels Aug 4, 2020
@neoaisac
Copy link

neoaisac commented Oct 2, 2020

Thinking of looking into this for Hacktoberfest. Any pointers?

@m-wild
Copy link
Collaborator Author

m-wild commented Oct 2, 2020

We generate a Document which contains Messages, each message has a Payload.

https://github.com/tehmantra/saunter/blob/279f51fce99847009b43badb522428638e3b6688/src/Saunter/Generation/DocumentGenerator.cs#L253

This ISchema is in JSON-Schema format.

When we set the Schema.Type property (which is currently a string) by reflecting types in the SchemaGenerator.

https://github.com/tehmantra/saunter/blob/279f51fce99847009b43badb522428638e3b6688/src/Saunter/Generation/SchemaGeneration/SchemaGenerator.cs#L31:L49

An example of a simple schema representing a non-nullable int:

{
    "type": "integer"
}

An example representing a nullable int:

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

We need to make Schema.Type render as either a string "T", or if the type is nullable, an array containing [ "T", "null" ].

We can optionally/additionally support the OpenAPI spec which defines an additional property nullable: bool
Example of nullable int in OpenAPI:

{
    "type": "integer",
    "nullable": true
}

@m-wild
Copy link
Collaborator Author

m-wild commented Oct 2, 2020

This is implemented using a [Flags] enum in Newtonsoft.Json.Schema
https://github.com/JamesNK/Newtonsoft.Json.Schema/blob/master/Src/Newtonsoft.Json.Schema/JSchemaType.cs

Same as NJsonSchema
https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema/JsonObjectType.cs

I suppose this is the easiest way to be able to set this as one or more value.

// for non-nullable primitives
Type = JsonSchemaType.Integer;

// for nullable-primitives
Type = JsonSchemaType.Integer | JsonSchemaType.Null;

This also allows for setting the nullable type separate from the underlying type;

var type = JsonObjectType.Integer;

//... elsewhere
if (nullable) {
    type = type | JsonObjectType.Null;
}

@m-wild
Copy link
Collaborator Author

m-wild commented Jul 19, 2021

Closing in favor of #60

@m-wild m-wild closed this as completed Jul 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💡 enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants