Coder Social home page Coder Social logo

Handle Nullable types about saunter HOT 4 CLOSED

m-wild avatar m-wild commented on June 18, 2024
Handle Nullable types

from saunter.

Comments (4)

neoaisac avatar neoaisac commented on June 18, 2024

Thinking of looking into this for Hacktoberfest. Any pointers?

from saunter.

m-wild avatar m-wild commented on June 18, 2024

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
}

from saunter.

m-wild avatar m-wild commented on June 18, 2024

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;
}

from saunter.

m-wild avatar m-wild commented on June 18, 2024

Closing in favor of #60

from saunter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.