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

Case when type is nullable #53

Open
nickcarenza opened this issue Aug 14, 2018 · 2 comments
Open

Case when type is nullable #53

nickcarenza opened this issue Aug 14, 2018 · 2 comments

Comments

@nickcarenza
Copy link

Could this multi-type

"type": [
  "string",
  "null"
],

be interpreted as *string?

same would go for

"oneOf": [
  {
    "type": "null"
  },
  {
    "$ref": "#/definitions/NameType"
  }
]
@a-h
Copy link
Owner

a-h commented Oct 17, 2018

Sorry for the late reply, but do you have a complete example?

@YenForYang
Copy link

A minimal example:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "id": "http://example.com/exampleschema.json",
    "title": "Title",
    "description": "Description",
    "type": "object",
    "properties": {
        "topics": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "score": {
                        "type": [
                            "null",
                            "number"
                        ]
                    }
                },
                "required": [
                    "score"
                ]
            }
        },
        "last_read": {"type": "null"},
        "rights": {
            "type": [
                "null",
                "string"
            ]
        },
        "resource_links": {
            "anyOf": [
                {"type": "null"},
                {
                    "type": "object",
                    "properties": {
                        "PropA": {"type": "string"},
                        "PropB": {"type": "string"}
                    }
                }
            ]
        },
        "virtual_pages": {
            "type": [
                "integer",
                "null"
            ]
        },
        "duration_seconds": {"type": "null"}
    },
    "required": [
        "duration_seconds",
        "last_read",
        "resource_links",
        "rights",
        "topics",
        "virtual_pages"
    ]
}

With schema-generator this gives something like:

// Title Description
type Title struct {
  DurationSeconds nil `json:"duration_seconds"` // error here
  LastRead nil `json:"last_read"`                             // error here also
  ResourceLinks interface{} `json:"resource_links"`
  Rights interface{} `json:"rights"`
  Topics []*TopicsItems `json:"topics"`
  VirtualPages interface{} `json:"virtual_pages"`
}

// TopicsItems
type TopicsItems struct {
  Score interface{} `json:"score"`
}

Basically nil can be changed to *string (see this SO question) and interface{} can be changed to *int, *float64, and *string in many of the above cases.

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

3 participants