Skip to content

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

Open

Description

@mattiasw2

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.

Activity

  1. auto-labeler commented on Nov 11, 2019

    @auto-labeler

    👍 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.

  2. mattiasw2 commented on Nov 11, 2019

    @mattiasw2
    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)
        }
    
    
  3. mattiasw2 commented on Nov 11, 2019

    @mattiasw2
    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"
    
               },
    
    
    
  4. eriktim commented on Nov 12, 2019

    @eriktim
    Contributor

    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.

  5. mattiasw2 commented on Nov 13, 2019

    @mattiasw2
    Author

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

    RicoSuter/NSwag#2435

  6. mattiasw2 commented on Nov 13, 2019

    @mattiasw2
    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.

  7. eriktim commented on Nov 16, 2019

    @eriktim
    Contributor

    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.

  8. mattiasw2 commented on Nov 16, 2019

    @mattiasw2
    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>

  9. pekunicki commented on Mar 18, 2020

    @pekunicki

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

  10. mattiasw2 commented on Mar 18, 2020

    @mattiasw2
    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.

  11. pekunicki commented on Mar 19, 2020

    @pekunicki

    @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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions