swagger-api / swagger-api/swagger-codegen

[C#] Exception deserializing null for optional enum specified via reference property

Open
#4,586 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Given an object with an inline enum property "type" and a referenced enum "country", data like {"country": null} will throw an exception on deserialization, while data like {} and {"type": null} will work as expected.

Swagger-codegen version

2.2.2-SNAPSHOT

Swagger declaration file content or url
{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "Swagger Test"
    },
    "paths": {},
    "definitions": {
        "Address": {
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "enum": [
                        "inline1",
                        "inline2"
                    ]
                },
                "country": {
                    "$ref": "#/definitions/Country"
                }
            }
        },
        "Country": {
            "type": "string",
            "enum": [
                "Ref1",
                "Ref2"
            ]
        }
    }
}
Command line used for generation

http://editor.swagger.io/#/
and
java -jar ~/swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i test.json -l csharp -o test/

Steps to reproduce

Generate the code. Write a sample program like:

RestResponse rsp;
Address addr;

rsp = new RestResponse();
rsp.Content = "{}";
addr = (Address)Configuration.Default.ApiClient.Deserialize(rsp, typeof(Address));
Console.WriteLine(addr.Country);

rsp = new RestResponse();
rsp.Content = "{\"type\": null}";
addr = (Address) Configuration.Default.ApiClient.Deserialize(rsp, typeof(Address));
Console.WriteLine(addr.Type);

rsp = new RestResponse();
rsp.Content = "{\"country\": null}";
addr = (Address)Configuration.Default.ApiClient.Deserialize(rsp, typeof(Address));
Console.WriteLine(addr.Country);

The first two will work, and the last will throw an exception.

Related issues

https://github.com/swagger-api/swagger-codegen/pull/4145 fixes the compilation issue but still doesn't work if a null is actually passed in the message.

Suggest a Fix

It seems to me that the isEnum flag is not getting set for RefProperties in DefaultCodegen.fromProperty. Could also be the case that C# needs to check for some other property in modelGeneric.mustache to know whether to force a ? reference.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DefaultCodegen.fromProperty and the C# modelGeneric.mustache template, comparing how inline and referenced enums are represented. Generate the sample C# client and run the three deserialization cases from the issue. Done means a null referenced enum deserializes without an exception while the existing empty-object and inline-enum cases continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.