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

Class defaults are not honored #41

Open
bruno-f-cruz opened this issue Jun 18, 2024 · 0 comments
Open

Class defaults are not honored #41

bruno-f-cruz opened this issue Jun 18, 2024 · 0 comments

Comments

@bruno-f-cruz
Copy link
Contributor

bruno-f-cruz commented Jun 18, 2024

Consider the following schema (for readability I will also add the corresponding class defined in Pydantic)

from pydantic import BaseModel, Field
import json
from typing import Optional


class Bar(BaseModel):
    value: int = Field(default=0, validate_default=True)
    label: str = Field(default='default', validate_default=True)
    nullable: Optional[str] = Field(default=None)


class Foo(BaseModel):
    foo_label: str = Field(default='foo_default_label', validate_default=True)
    bar_w_default: Bar = Field(default=Bar(), validate_default=True)


class Container(BaseModel):
    nullable_foo: Optional[Foo] = Field(default=None)
    foo_w_defaults: Foo = Field(default=Foo(), validate_default=True)

and the corresponding json-schema:

{
    "definitions": {
        "Bar": {
            "properties": {
                "value": {
                    "default": 0,
                    "title": "Value",
                    "type": "integer"
                },
                "label": {
                    "default": "default",
                    "title": "Label",
                    "type": "string"
                },
                "nullable": {
                    "anyOf": [
                        {
                            "type": "string"
                        },
                        {
                            "type": "null"
                        }
                    ],
                    "default": null,
                    "title": "Nullable"
                }
            },
            "title": "Bar",
            "type": "object"
        },
        "Foo": {
            "properties": {
                "foo_label": {
                    "default": "foo_default_label",
                    "title": "Foo Label",
                    "type": "string"
                },
                "bar_w_default": {
                    "allOf": [
                        {
                            "$ref": "#/definitions/Bar"
                        }
                    ],
                    "default": {
                        "value": 0,
                        "label": "default",
                        "nullable": null
                    }
                }
            },
            "title": "Foo",
            "type": "object"
        }
    },
    "properties": {
        "nullable_foo": {
            "anyOf": [
                {
                    "$ref": "#/definitions/Foo"
                },
                {
                    "type": "null"
                }
            ],
            "default": null
        },
        "foo_w_defaults": {
            "allOf": [
                {
                    "$ref": "#/definitions/Foo"
                }
            ],
            "default": {
                "foo_label": "foo_default_label",
                "bar_w_default": {
                    "label": "default",
                    "nullable": null,
                    "value": 0
                }
            }
        }
    },
    "title": "Container",
    "type": "object"
}

After running the sgen application, the corresponding csharp classes do not honor the defaults of objects from the json. For instance, calling Container will initialize the corresponding properties as nullable_foo and foo_w_defaults as null instead of following the defaults. For instance, one would expect that the foo_w_defaults property follows:

            "default": {
                "foo_label": "foo_default_label",
                "bar_w_default": {
                    "label": "default",
                    "nullable": null,
                    "value": 0
                }

This probably happens because the constructors of these classes are not explicitly initializing the variables with any value and instead rely on the default value given by the compiler (null).

public partial class Container
    {
    
        private NullableFoo _nullableFoo;
    
        private Foo _fooWDefaults;
    
        public Container()
        {
        }
    ...

Using the constructor operators in Bonsai and printing it to the console returns:

Container { nullable_foo = , foo_w_defaults =  }
Foo { foo_label = foo_default_label, bar_w_default =  }
Bar { value = 0, label = default, nullable =  }
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

No branches or pull requests

1 participant