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

[REQ][csharp-netcore] nullable value types support #9374

Closed
lucamazzanti opened this issue Apr 30, 2021 · 2 comments
Closed

[REQ][csharp-netcore] nullable value types support #9374

lucamazzanti opened this issue Apr 30, 2021 · 2 comments

Comments

@lucamazzanti
Copy link
Contributor

I want to open that feature request to recap some features\PR I see in the project.

I would understand the behavior with required and nullable options and nullable value types in the models codegen.
Moving from Swagger I have a breaking change I want to understand.
In Swagger we have that all the value type fields of a model are nullable, here not.
I see in the migration guideline a few informations about nullables.

example

The specification:

Pet:
    type: object
    required:
      - id
      - name
    properties:
      id:
        type: integer
      name:
        type: string
      age:
        type: integer
      owner:
        type: string      

Here the Swagger version:

public class Pet
{
    public long? Id {get; set; }
    public string Name {get; set; }
    public long? Age {get; set; }
    public string Owner {get; set; }
}

In this version everything is nullable to better specify a default value when data is missing in serialization\deserialization. It's useful, but having required, validated fields managed always as nullable is not so much good.

Here the OpenApi version:

public class Pet
{
    public long Id {get; set; }
    public string Name {get; set; }
    public long Age {get; set; }
    public string Owner {get; set; }
}

In this version it's difficult to manage not required fields, when the default value is available or not? You cannot know it.

Here my expected version:

public class Pet
{
    public long Id {get; set; }
    public string Name {get; set; }
    public long? Age {get; set; }
    public string Owner {get; set; }
}

Here the not required value type Age is proposed as nullable, the required Id not.

Must I change the specification generating supporting definitions such x-nullable?
I understand the difference between required and nullable, they have different meanings, but in C# an optional value type seems right to be managed as nullable.

Here past issues involved:

Thank you all.

People involved: @Blackclaws @devhl-labs @DavidJeppesen-Seges

@lucamazzanti
Copy link
Contributor Author

I will understand how to generate x-nullable: true and nullable: true in the server part.

@lucamazzanti
Copy link
Contributor Author

My issue was to have a NetFramework WebApi project with Swagger, and the OpenApi client generator reading the server documentation, missing of x-nullable informations.

So I added server side a Swagger SchemaFilter to produce for not required value types the missing informations.

public class NullableSchemaFilter : ISchemaFilter
{
  public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
  {
    if (schema.properties == null) return;
    
    var optionalValueTypeProperties = schema.properties
    .Where(i =>
  	  (i.Value.type == "string" && i.Value.format == "date") ||
  	  (i.Value.type == "string" && i.Value.format == "date-time") ||
  	  (i.Value.type == "string" && i.Value.format == "uuid") ||
  	  i.Value.type == "number" ||
  	  i.Value.type == "integer" ||
  	  i.Value.type == "boolean")
    .Where(i => !schema.required?.Contains(i.Key) ?? false);
    
    foreach (var property in optionalValueTypeProperties)
    {
      property.Value.vendorExtensions.Add("nullable", true);
      property.Value.vendorExtensions.Add("x-nullable", true);
    }
  }
}

Now the optional value types are decorated with x-nullable and the vanilla OpenApi codegen manage them as nullable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant