-
-
Notifications
You must be signed in to change notification settings - Fork 152
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
Support nullable value types #127
Support nullable value types #127
Conversation
I like this as a starting point. It doesn't add Further thinking (and perhaps a secondary option) is needed around doing this for nullable ref types. I'll ask around the JSON Schema circles to see if people have feelings about this. Feel free to join me their Slack (it's on their website). |
@JMPSequeira what do you think of adding a I would like your generator to add |
I'll add type null to nullable value types. I think the NotNullAttribute should also cover Nullable, specially after adding null to the type array. |
So here's what I'm thinking:
This means that you can have something like {
"properties": {
"foo": { "type": [ "integer", "null" ]
},
"required": [ "foo" ]
} which would allow an explicitly included null value such as |
Ok I can see that working. I suggest also having a NullAttribute to override when nullability == Disabled. |
Regarding all the changes being implemented I have one question. This poses an issue as there is no way to control the nullability of the root schema, Attributes are only read from Members and even if that wasn't the case there's also the issue with types from assemblies where one cannot apply attributes. For example: var builder = new JsonSchemaBuilder();
var schema = builder.FromType<List<string>>(configuration: new()
{
Nullability = Nullability.AllowForAllTypes
}); I propose having an extra param on Something in the lines of: public static JsonSchemaBuilder FromType<T>(this JsonSchemaBuilder builder, SchemaGeneratorConfiguration? configuration = null, bool? rootAsNullable = null)
{
return FromType(builder, typeof(T), configuration, rootAsNullable);
} Let me know your thoughts on this. |
…tter consider override attribute
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this got us a lot closer. There are a few concerns, which I've commented on. I'm not asking for you to update this, though. Please have a look at #128. I've pulled in a lot of your work. Thanks for the help.
{ | ||
var valueType = Type; | ||
|
||
if ((context.Configuration.Nullability & Nullability.AllowForReferenceTypes) != 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a .HasFlag()
method on enums that should help here.
|
||
namespace Json.Schema.Generation.Generators | ||
{ | ||
internal abstract class BaseReferenceTypeGenerator : ISchemaGenerator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Baking this into the generator class hierarchy doesn't really help with extensibility. If someone wants to create their own generator, they'd have to inherit from this class (but they can't because it's internal) or handle this logic on their own.
/// <summary> | ||
/// Default value. Type `null` will not be applied to schema. | ||
/// </summary> | ||
Disabled = 1 << 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be the default value, so just 0.
/// <summary> | ||
/// Gets or sets the application of type `null` to schema. | ||
/// </summary> | ||
public Nullability Nullability { get; set; } = Nullability.Disabled; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That allows you to not set this.
Closing this one, too, as I'm going to merge #128. Thanks for your work on this. |
Created as an alternative approach to #124.