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

Required nullable objects are not allowed to be null #46

Open
bruno-f-cruz opened this issue Nov 6, 2024 · 0 comments · May be fixed by #47
Open

Required nullable objects are not allowed to be null #46

bruno-f-cruz opened this issue Nov 6, 2024 · 0 comments · May be fixed by #47

Comments

@bruno-f-cruz
Copy link
Contributor

Consider the following schema where both inner_optional and inner_required are nullable:

{
  "definitions": {
    "Object": {
      "properties": {
        "foo": {
          "title": "Foo",
          "type": "string"
        },
        "bar": {
          "title": "Bar",
          "type": "integer"
        }
      },
      "required": [
        "foo",
        "bar"
      ],
      "title": "Object",
      "type": "object"
    }
  },
  "properties": {
    "inner_optional": {
      "default": null,
      "oneOf": [
        {
          "$ref": "#/definitions/Object"
        },
        {
          "type": "null"
        }
      ]
    },
    "inner_required": {
      "oneOf": [
        {
          "$ref": "#/definitions/Object"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "inner_required"
  ],
  "title": "Container",
  "type": "object"
}

Deserialization using json fails if the "inner_required" is set to null but not the optional one, despite both evaluating to null. I am not sure this is a problem with sgen itself. Looking at the c# class definitions the only obvious difference is the Newtonsoft attribute Required. After some experimentation, I believe that the generator should use Newtonsoft.Json.Required.AllowNull instead of Newtonsoft.Json.Required.Always for these situations (ie.. when prop is nullable). See documentation here (https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Required.htm)
Find some useful examples attached include schema, csharp code, bonsai workflow and test json instances that reproduce the problem.
test_deser.zip

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 a pull request may close this issue.

1 participant