Skip to content

Schema.json_schema() does not return properties for a dict of dicts where first level key can be any string #329

Description

@WillGibson

We want to auto generate a reference from our schema and have been playing with the following which looks promising in conjunction with Schema.json_schema() and then us parsing the JSON into a more friendly display format...

Schema( {
    Literal("application", description="The name of your application. Letters, numbers, hyphens and underscores are allowed."): str
})

Our schema include several dicts of dicts where the first level key can be any string. E.g...

Schema( {
    "dict-of-dicts-any-name-allowed": {
        str: {
            "property-1": str
        }
    }
})

There seems to be an issue with Schema.json_schema() where, for these, it does not return any properties. E.g...

{
  "type": "object",
  "properties": {
    "dict-of-dicts-any-name-allowed": {
      "type": "object",
      "properties": {},
      "required": [],
      "additionalProperties": true

    }
  },
  ...
}

I have created a gist so that you can see this easily first hand.

Is there something we should be doing to get Schema.json_schema() to return the properties for these dicts?

If this is not currently supported, would you be open to us submitting a pull request to add support for it?

If we are submitting a pull request, would the following approach be suitable for the expected output (based on the example above)?

{
  "type": "object",
  "properties": {
    "dict-of-dicts-any-name-allowed": {
      "type": "object",
      "properties": {
        "<string_key>": {
          "type": "object",
          "properties": {
            "property-1": {
              "type": "string"
            }
          },
          "required": [
            "property-1"
          ],
          "additionalProperties": false
        }
      },
      "required": [],
      "additionalProperties": false
    }
  },
  ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions