Skip to content

jsonschema import does not interpret defaults for objects #1794

Open
@tvandinther

Description

@tvandinther

Bug Report

1. Minimal reproduce step (Required)

Given the following schema.json:

{
  "type": "object",
  "properties": {
    "foo": {
      "type": "object",
      "properties": {
        "bar": {
          "type": "string",
          "default": "baz"
        }
      },
      "required": ["bar"]
    }
  },
  "required": ["foo"]
}

kcl import schema.json --mode jsonschema generates:

"""
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""

schema Schema:
    r"""
    Schema

    Attributes
    ----------
    foo : SchemaFoo, required
    """

    foo: SchemaFoo

schema SchemaFoo:
    r"""
    SchemaFoo

    Attributes
    ----------
    bar : str, required, default is "baz"
    """

    bar: str = "baz"


And create a main.k of

Schema{}

2. What did you expect to see? (Required)

kcl run main.k schema.k produces

foo:
  bar: baz

and the contents of schema.k to be

"""
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""

schema Schema:
    r"""
    Schema

    Attributes
    ----------
    foo : SchemaFoo, required
    """

    foo: SchemaFoo = SchemaFoo{}

schema SchemaFoo:
    r"""
    SchemaFoo

    Attributes
    ----------
    bar : str, required, default is "baz"
    """

    bar: str = "baz"


Note the line

foo: SchemaFoo = SchemaFoo{}

where we instantiate the default version of the sub schema. If anything in the sub schema is required but has no default, the standard error will continue to throw. A suitable fix for me would be to make the default behaviour of the jsonschema import to create an instance of a schema as the default value for all schemas in a schema. Otherwise, it is not possible to use this import for creating nested structures with default values.

3. What did you see instead (Required)

kcl run main.k schema.k produces

attribute 'foo' of Schema is required and can't be None or Undefined

4. What is your KCL components version? (Required)

❯ kcl version
0.10.9-linux-amd64

Metadata

Metadata

Assignees

Labels

designfeatlang-designIssues or PRs related to kcl language design and KEPs

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions