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

Use nullable reference for AnyType if required #1289

Merged
merged 1 commit into from
Dec 16, 2020

Conversation

jacobpovar
Copy link
Contributor

Hi. When using C# code generation from openapi3.0 format and given the following :

  • GenerateNullableAnnotations option is enabled,
  • and type of nullable object isn't specified:
propertyName:
  type: object
  nullable: true

then Nswag generates code with object type for propertyName, not object?.

Changing AnyType to object? in code generation settings doesn't help either since it results with following code:

public object? PropertyName {get;set;} = new object?();

which can't be compiled.

This PR changes type resolver behavior by append ? to AnyType.

@RicoSuter
Copy link
Owner

RicoSuter commented Dec 1, 2020

@jeremyVignelles Can you do a review?
It looks good to me.

@RicoSuter
Copy link
Owner

@jacobpovar Can you also add some tests covering this?

@jeremyVignelles
Copy link
Contributor

Does the code still generate = new object(); ?

I don't think it's the right thing to return(should be null), but that would likely break things to change it now.

If there is = new object();, can it still be null or will it just be skipped by deserialization?
If it can't be null, there's no point of making the type nullable

@jacobpovar
Copy link
Contributor Author

@RicoSuter @jeremyVignelles thank you for reviewing this request

@RicoSuter sure, added couple of tests.

@jeremyVignelles

An object will have new object() initializer only if it's required.
when reading jsonschema from C# class then both optional and required fields will have = default! initializer.

when reading schema from json (the difference is that properties have explicit object type now) an optional field still has = default! initializer and will remain null.
a required field will have new object() initializer as it had before my changes.

So, if I'm not mistaken, If property is not set, it will remain null after deserialization. If it's present, then it won't be null.

@jeremyVignelles
Copy link
Contributor

I didn't check but that sounds good 🙂 Thanks for the explanations

@RicoSuter RicoSuter merged commit bc82834 into RicoSuter:master Dec 16, 2020
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

Successfully merging this pull request may close these issues.

3 participants