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

add method to update sub-schema in simple allOf case #89

Merged
merged 2 commits into from
Nov 15, 2023

Conversation

tharwan
Copy link

@tharwan tharwan commented Nov 9, 2023

This PR will try to merge the allOf sub-schema with the surround schema in a simple way. This addresses issues of otherwise missing properties like defaults. For example:

  {
    "default": "A",
    "allOf": [
      {
        "title": "all of enum",
        "description": "An enumeration.",
        "enum": [
          "A",
          "B",
          "C"
        ],
        "type": "string"
      }
    ]
  }

this would not render a dropdown with a default selection of A.

@dokempf
Copy link
Member

dokempf commented Nov 10, 2023

Did I understand correctly, that this code is happening only iff the number of subschemas for the allOf rule is 1? Does this happen in your use case? And would it be possible to just drop it from the input schema? I am asking because I am hesitant to add complex logic that alters schemas to enable use cases that can easily prevented. I was always prepared to flag some JSONSchema features as not supported for this project (and the allOf rule is quite a contender...)

@tharwan
Copy link
Author

tharwan commented Nov 10, 2023

Our use case is to display json schemas that originate from pydantic. Which uses allOf in many cases to refer to sub-models (compare with pydantic/pydantic#2592).

I think pydantic is an extremely common way in the python eco-system to deal with json and json schemas, so it would make sense to support it.

I am very much open to all suggestions how to simplify this.

Edit:
Example

class Status(Enum):
    A = "A"
    B = "B"
    C = "C"

class Model(BaseModel):
    status: Status = Status.A

Model.schema()

results in

{
  "title": "Model",
  "type": "object",
  "properties": {
    "status": {
      "default": "A",
      "allOf": [
        {
          "$ref": "#/definitions/Status"
        }
      ]
    }
  },
  "definitions": {
    "Status": {
      "title": "Status",
      "description": "An enumeration.",
      "enum": [
        "A",
        "B",
        "C"
      ]
    }
  }
}

which the above case is derived from. Note that the reference can be resolved prior with https://pypi.org/project/jsonref/

@dokempf
Copy link
Member

dokempf commented Nov 15, 2023

I am fine with supporting this as you have a valid use case.

@dokempf dokempf merged commit d68ac29 into ssciwr:main Nov 15, 2023
9 checks passed
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

Successfully merging this pull request may close these issues.

2 participants