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

[BUG] ELM: C# 8 nullable feature creates uncompilable (Maybe OneOf<PContactInfo>) #4450

Open
6 tasks
mattiasw2 opened this issue Nov 11, 2019 · 11 comments
Open
6 tasks

Comments

@mattiasw2
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
    No, it is the complete swagger.json
  • Have you validated the input using an OpenAPI validator (example)?
    Yes
  • What's the version of OpenAPI Generator used?
    4.2.0
  • Have you search for related issues/PRs?
    Yes.
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

As of C# 8, nullable references types have an anotation '?', and unless it is there, the reference cannot be null.

https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/nullable-reference-types

I have a plain DTO like this,

    public class AccResponse
    {
        // ReSharper disable once NotNullMemberIsNotInitialized
        [NotNull] [Required] public BaseResponse Common { get; set; } = new BaseResponse();
        // public string Id { get; set; }
        [Required] public string Name { get; set; } = "";
        public bool AllowGuest { get; set; }
        public PContactInfo? TechContactInfo { get; set; }
        public PContactInfo? AdminContactInfo { get; set; }
        public bool Blocked { get; set; }
        public bool Disabled { get; set; }
        public int MaximumAllowedNamedUsers { get; set; }
        public string? CreateUserEmailMessage { get; set; }
        public string? CreateUserEmailMessageSubject { get; set; }
    }

and the problem is

    public PContactInfo? TechContactInfo { get; set; }

It is actually exactly the same as

    public PContactInfo TechContactInfo { get; set; }

in C# 7 and earlier, i.e. since there is no [Required], that field can be null.

openapi-generator version

4.2.0

OpenAPI declaration file content or url
Command line used for generation

java -jar openapi-generator-cli-4.2.0.jar generate -g elm -i swagger.json -o src/Swagger/

Steps to reproduce

Just generate and look at the code. There is a warning for another problem,

swagger.zip

[main] INFO o.o.codegen.DefaultGenerator - Model ExcelFormattingBase not generated since it's a free-form object

but you can ignore that. I can simplify those objects.

Suggest a fix

Just make it be treated as if neither ? or [Required] is not there.

@auto-labeler
Copy link

auto-labeler bot commented Nov 11, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@mattiasw2
Copy link
Author

Notice that string? are correctly handled:

type alias AccResponse =
    { common : BaseResponse
    , name : String
    , allowGuest : Maybe (Bool)
    , techContactInfo : (Maybe OneOf<PContactInfo>)
    , adminContactInfo : (Maybe OneOf<PContactInfo>)
    , blocked : Maybe (Bool)
    , disabled : Maybe (Bool)
    , maximumAllowedNamedUsers : Maybe (Int)
    , createUserEmailMessage : (Maybe String)
    , createUserEmailMessageSubject : (Maybe String)
    }

@mattiasw2
Copy link
Author

The nullable mark ? is the problem. I removed it, and the swagger.json file lost the following. So, for now, I will just make sure I do not use that annotation in my DTO classes.

$ git diff swagger.json
diff --git a/sss6elm/swagger.json b/sss6elm/swagger.json
index b5e8f70..2dc258b 100644
--- a/sss6elm/swagger.json
+++ b/sss6elm/swagger.json
@@ -2786,33 +2786,13 @@

           "techContactInfo": {

-            "nullable": true,
-
-            "oneOf": [
-
-              {
-
-                "$ref": "#/components/schemas/PContactInfo"
-
-              }
-
-            ]
+            "$ref": "#/components/schemas/PContactInfo"

           },


@eriktim
Copy link
Contributor

eriktim commented Nov 12, 2019

Thanks for your report @mattiasw2. oneOf is currently not yet supported, see #4434. However, I checked it and it does not work when the oneOf is inline in that PR. You would have to create a new schema in components for it.

Two questions:

  • Why do you actually need a oneOf here. You only have one case;
  • Is there a reason you use nullable instead of required here?

For now using nullable and $ref should work.

@mattiasw2
Copy link
Author

The OneOf is generated by NSwag. I added this question there.

RicoSuter/NSwag#2435

@mattiasw2
Copy link
Author

When I look at the specification of OneOf at https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/ it seems to me that adding a OneOf with just one option is useless. I think in this case, you can just remove the OneOf and replace the expression by the single $ref inside.

@eriktim
Copy link
Contributor

eriktim commented Nov 16, 2019

Yes exactly. That's why I wonder why it is generated like take. Nevertheless it should work though. It should create a custom type with only one variant.

@mattiasw2
Copy link
Author

I think Rico from Nswag has says it should look like this, but I am not sure.

Currently, your generated code will be non-compilable, since the name of the type name includes & and ;

OneOf<PContactInfo>

@pekunicki
Copy link

Hi All, @mattiasw2 did you maybe find some workaround for that?
This oneOf for one nullable referenced object is slightly confusing.

@mattiasw2
Copy link
Author

@pekunicki I choose to not use the C# 8 nullable feature in my API. The reason I created this ticket is that Typescript handled it, and in the long-term I want ELM to support it too, since for a language like ELM, not-nullable is really important and useful.

@pekunicki
Copy link

pekunicki commented Mar 19, 2020

@mattiasw2 thanks, It seems like a more complex discussion, i.e. whether referenced objects can have nullable prop on the same level. See #5180

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

3 participants