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

Nullable nested object support #177

Open
siviae opened this issue Dec 7, 2023 · 0 comments
Open

Nullable nested object support #177

siviae opened this issue Dec 7, 2023 · 0 comments

Comments

@siviae
Copy link

siviae commented Dec 7, 2023

Hello! Thanks a lot for your work.

Right now I am trying to use this tool during my work and I have a problem with using oneOf to support passing null json values. For example, when I use the following schema:

{
  "title": "Repro",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "nested": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "$ref": "#/definitions/Nested"
        }
      ]
    }
  },
  "definitions": {
    "Nested": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        }
      },
      "required": [
        "name"
      ]
    }
  }
}

I've got the following output:

type Nested struct {
	// Name corresponds to the JSON schema field "name".
	Name string `json:"name" yaml:"name" mapstructure:"name"`
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *Nested) UnmarshalJSON(b []byte) error {
	var raw map[string]interface{}
	if err := json.Unmarshal(b, &raw); err != nil {
		return err
	}
	if v, ok := raw["name"]; !ok || v == nil {
		return fmt.Errorf("field name in Nested: required")
	}
	type Plain Nested
	var plain Plain
	if err := json.Unmarshal(b, &plain); err != nil {
		return err
	}
	*j = Nested(plain)
	return nil
}

type Repro struct {
	// Nested corresponds to the JSON schema field "nested".
	Nested interface{} `json:"nested,omitempty" yaml:"nested,omitempty" mapstructure:"nested,omitempty"`
}

which is very inconvenient to use, since json.Unmarshal does not know, which type to use for nested field.

When I use the following schema:

{
  "title": "Repro",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "nested": {
      "$ref": "#/definitions/Nested"
    }
  },
  "definitions": {
    "Nested": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        }
      },
      "required": [
        "name"
      ]
    }
  }
}

I have the result I want, which is

type Nested struct {
	// Name corresponds to the JSON schema field "name".
	Name string `json:"name" yaml:"name" mapstructure:"name"`
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *Nested) UnmarshalJSON(b []byte) error {
	var raw map[string]interface{}
	if err := json.Unmarshal(b, &raw); err != nil {
		return err
	}
	if v, ok := raw["name"]; !ok || v == nil {
		return fmt.Errorf("field name in Nested: required")
	}
	type Plain Nested
	var plain Plain
	if err := json.Unmarshal(b, &plain); err != nil {
		return err
	}
	*j = Nested(plain)
	return nil
}

type Repro struct {
	// Nested corresponds to the JSON schema field "nested".
	Nested *Nested `json:"nested,omitempty" yaml:"nested,omitempty" mapstructure:"nested,omitempty"`
}

however, I want the {"nested": null} json object to be valid both in terms of schema and the parser, not just the parser.

I understand that full support of oneOf requires a lot of work, however I think that supporting this special case could be quite easy.

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