-
-
Notifications
You must be signed in to change notification settings - Fork 539
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
C# generator: Generate C# 8 Nullable Reference Type (NRT) attributes #1069
Comments
It would be great to have this. If I understand right, this issue is only for client generation, so shouldn't the implementation of this not need to meddle with the NRT attributes at all and simply need to know when to syntactically add a |
Exactly, it’s probably not super trivial but it will be an opt-in setting so it will no break existing user and it doesnt matter if its not working 100% at the beginning. |
Any update on this? I was considering redistributing the generated C# client in an assembly, but was wondering if I should enable nullable reference types there or not. It would be easier in the meantime to generate |
Good idea, however adding |
Didn't think about that. Probably worth having an option to disable it, you're right. We then have four states:
Or maybe it's better to have an option like "C# output language level" and use |
Good idea but not sure about that, I think NRT would be the only thing which would check that - and then it's probably better to a have a specific option... for everything else i think we can just generate eg C# 6 code and there is just no downside (unlike in TS).
I think it would be better to have another class style here: |
I was thinking about it more as a safeguard to avoid using settings unavailable for the target language level, but OK for your NativeRecord suggestion. About the |
What about a boolean setting "GenerateNullableReferenceTypes" (default: false) and for now it just generates |
Probably a bit misleading if NRT are not really implemented. How hard would it be to implement NRT directly behind the flag ? |
I think it should be quite simple to implement - we already have it for typescript.. at least for the public api contract - not sure whether we have problems “inside” the methods etc but there we could just disable the checks at the beginning |
I think we only need to add a ? if the reference type is nullable and the setting is enabled here: I think the others in this method use Nullable<> and are already handled correctly - or what do you think? |
Not sure but this might already be enough: |
I don't see any other case where this might break something:
Problems will arise when used in a project if the code using that doesn't match the intent, but that's the whole point of enabling that, and it's opt-in. I don't see anything else problematic |
My thinking was that if you have warnaserror enabled and the flow analysis within the method gives warnings it would turn them into errors and thus wont compile - but i think we disable all warnings in the generated code so this might also be fine... |
I don't understand : If that's an opt-in feature, it would only break if you tell it to enable NRT, right ? |
My PR above probably is already enough because this resolver is used for method params, return param and POCO property types... the "problem" is - also with NRT in general - that it's only a compile time feature - so if the server returns wrong JSON (ie null but the property is not-nullable) then there will be no exception at runtime, this could be solved with this: https://github.com/RicoSuter/Namotion.Reflection#validate-nullability-c-8 but this is a topic we won't cover in this issue probably. |
For example if NRT is enabled and your param is non nullable but in the generated method we still check it against null this might give a warning (eg check not needed) which might be a problem... but it's something we need to try out and improve over time I think... |
Isn't there any check in the Json serializer that checks the [Required] and [JsonProperty] attributes and checks whether the deserialized json is valid w.r.t the specified attributes? |
Can you check my PR above (i.e. go through the changed class). Does it look good? |
Ah you are right! |
Should we go with this PR? As its a new, experimental setting (opt-in) I can merge and release that now and you can try it out and then we add tests + improve on that... what do you think? |
I've read your PR and didn't find anything wrong, but I don't have your knowledge of this project, and I don't know what impact it has.
OK, but I won't be able to test before Monday. |
Also, for my use case (NSwag code gen), would I need a new release of NSwag too ? |
I’d release njs + nswag with the update |
Ok do you think the name GenerateNullableReferenceTypes is good? |
I can't find a better name, and "Nullable Reference Types" is the way that feature is named in the C# language. I'm ok with that naming. |
Ref: #1195 |
Oops, I completely missed that the PR you were sending didn't include #nullable annotations, sorry |
Since this has been released, can it be closed now ? |
I get a whole bunch of compiler warnings from the generated Almost all of them can be resolved as marking the method with The rest of them are when there's a query parameter list of nullables, eg. |
@adamjones1 : The ConvertToString issues are covered by RicoSuter/NSwag#2959 I don't know about the |
Great, thanks. For the list ones, I get code generated like this for a method:
Here what's going in to |
Implemented in the PR. Do you have the ability to try in my PR and tell me if it work ? |
Just tried it, I get compiler errors because |
You're right, might have missed that. Let me fix that |
Does it work better now ? |
Looks good, no compiler or runtime errors/warnings for me 👍 |
* Reworked C# client response handling * Disallowed null responses if the response type is not declared as nullable Fixed the remark in #2944 * Fixed nullability on File I/O * Fixed nullability on ConvertToString * Fixed PR remarks * Avoid nullability issues in Query parameters Addresses RicoSuter/NJsonSchema#1069 (comment) * Fixed invalid code if array value type is not string
* Add support for using controllers' summary as tag description (#2949) * Add support for using controllers' summary as tag description * Ensure tag only added to document if there's description * Cleanup * Move code Co-authored-by: Rico Suter <mail@rsuter.com> * Fix contract, closes #2933 * Update README.md * Update README.md * Update README.md * Update README.md * Bump elliptic from 6.4.0 to 6.5.3 in /src/NSwag.Sample.NetCoreAngular (#2968) Bumps [elliptic](https://github.com/indutny/elliptic) from 6.4.0 to 6.5.3. - [Release notes](https://github.com/indutny/elliptic/releases) - [Commits](indutny/elliptic@v6.4.0...v6.5.3) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Fix for File Parameter interface not generated in some instances (#2972) - Split the Binary tests into 3 so each endpoint can be generated properly without missing the FileParameter interface - changed RequiresFileParameterInterface logic so it checks isBinary on arrays and nested objects Co-authored-by: Craig Ngu <Craig.Ngu@globalx.com.au> * Correct link in markdown for MS Build Targets (#2958) * Aligned OpenAPI code generation for file more with open API specification (#2896) * Reworked C# client response handling (#2959) * Reworked C# client response handling * Disallowed null responses if the response type is not declared as nullable Fixed the remark in #2944 * Fixed nullability on File I/O * Fixed nullability on ConvertToString * Fixed PR remarks * Avoid nullability issues in Query parameters Addresses RicoSuter/NJsonSchema#1069 (comment) * Fixed invalid code if array value type is not string * v13.7.0 (#2978) * v13.7.0 * Fix test Co-authored-by: Leon V <leon99@outlook.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: craigngu <42369784+craigngu@users.noreply.github.com> Co-authored-by: Craig Ngu <Craig.Ngu@globalx.com.au> Co-authored-by: Jedidiah May <33727409+portlandtn@users.noreply.github.com> Co-authored-by: Nicolas Fløysvik <nico-floysvik@hotmail.com> Co-authored-by: Jérémy VIGNELLES <jeremyVignelles@users.noreply.github.com>
Closed as implemented and released, please create new issues if there are bugs/problems with the current implementation. |
Add new setting for this feature.
The text was updated successfully, but these errors were encountered: