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

Possible regression of __modify_schema__ for enum fields #1576

Closed
therefromhere opened this issue May 28, 2020 · 2 comments · Fixed by #1581
Closed

Possible regression of __modify_schema__ for enum fields #1576

therefromhere opened this issue May 28, 2020 · 2 comments · Fixed by #1581
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@therefromhere
Copy link
Contributor

therefromhere commented May 28, 2020

Bug

A side-effect of #1432 is that __modify_schema__ on enum subclasses is still affecting the field schema, rather than the enumeration schema.

I'd like it if __modify_schema__ applied to the resulting enum model. Does that seem reasonable?

In my use case I'm subclassing enum to add a tsEnumNames property, so that I could generate proper typescript enums from the resulting Json schema (using json-schema-to-typescript - see bcherny/json-schema-to-typescript#15 ).

Using latest master 2eb62a3 (or d560c57 from #1562)

import enum
import pydantic


class TsCompatibleEnum(enum.Enum):
  """Enum subclass that adds tsEnumNames to enumeration fields
  for compatibility with json-schema-to-typescript

  See https://github.com/bcherny/json-schema-to-typescript/pull/15
  """
  @classmethod
  def __modify_schema__(cls, field_schema):
    field_schema["tsEnumNames"] = list(e.name for e in cls)


class MyEnum(TsCompatibleEnum):
  """Enumeration - note that the names and values are different"""
  FOO = "foo"
  BAR = "bar"


class MyModel(pydantic.BaseModel):
  my_field: MyEnum = MyEnum.FOO


print(MyModel.schema_json(indent=2))

Outputs:

{
  "title": "MyModel",
  "type": "object",
  "properties": {
    "my_field": {
      "$ref": "#/definitions/MyEnum",
      "tsEnumNames": [
        "FOO",
        "BAR"
      ]
    }
  },
  "definitions": {
    "MyEnum": {
      "title": "MyEnum",
      "description": "Enumeration - note that the names and values are different",
      "enum": [
        "foo",
        "bar"
      ]
    }
  }
}

vs with pydantic 1.5.1:

{
  "title": "MyModel",
  "type": "object",
  "properties": {
    "my_field": {
      "title": "My Field",
      "default": "foo",
      "enum": [
        "foo",
        "bar"
      ],
      "tsEnumNames": [
        "FOO",
        "BAR"
      ]
    }
  }
}

I'd like to get this instead:

{
  "title": "MyModel",
  "type": "object",
  "properties": {
    "my_field": {
      "$ref": "#/definitions/MyEnum"
    }
  },
  "definitions": {
    "MyEnum": {
      "title": "MyEnum",
      "description": "Enumeration - note that the names and values are different",
      "enum": [
        "foo",
        "bar"
      ],
      "tsEnumNames": [
        "FOO",
        "BAR"
      ]
    }
  }
}
@therefromhere therefromhere added the bug V1 Bug related to Pydantic V1.X label May 28, 2020
therefromhere added a commit to therefromhere/pydantic that referenced this issue May 31, 2020
therefromhere added a commit to therefromhere/pydantic that referenced this issue May 31, 2020
@samuelcolvin
Copy link
Member

I'm confused, is this the same or linked to #1552?

@therefromhere
Copy link
Contributor Author

therefromhere commented Jun 5, 2020 via email

samuelcolvin added a commit that referenced this issue Jun 29, 2020
…#1581)

* Apply __modify_schema__ on enum schema rather than fields that use it

Resolves #1576

* tweak test

* correct linting :-(

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants