Repository: pydantic/pydantic

Issue:
Schema generated for `Literal[MyEnum.MEMBER]` is too lax?
### Checks

* [x] I added a descriptive title to this issue
* [x] I have searched (google, github) for similar issues and couldn't find anything
* [x] I have read and followed [the docs](https://pydantic-docs.helpmanual.io/) and still think this is a bug

<!-- Sorry to sound so draconian, but every second saved replying to issues is time spend improving pydantic :-) -->

# Bug

When creating a model with a property typed as `Literal[MyEnum.MEMBER]`, Pydantic generates a schema that is too lax, describing the property as being any value of the enum instead of a specific one.

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.8.1
            pydantic compiled: True
                 install path: /tmp/tmp.iid8D0iv22/.venv/lib/python3.9/site-packages/pydantic
               python version: 3.9.2 (default, Feb 20 2021, 18:40:11)  [GCC 10.2.0]
                     platform: Linux-5.11.6-arch1-1-x86_64-with-glibc2.33
     optional deps. installed: ['typing-extensions']
```

```py
from typing import Literal
from enum import Enum

from pydantic import BaseModel


class MyEnum(str, Enum):
    FOO = "foo"
    BAR = "bar"


class ModelWithLiteralEnumMemberAndFullEnum(BaseModel):
    kind: Literal[MyEnum.FOO]
    possible_kinds: MyEnum  # This property is there as a workaround for #2534

print(ModelWithLiteralEnumMemberAndFullEnum.schema_json())  # This schema is too lax
```

This gives the following schema:
```json
{
  "title": "ModelWithLiteralEnumMemberAndFullEnum",
  "type": "object",
  "properties": {
    "kind": {
      "title": "Kind",
      "$ref": "#/definitions/MyEnum"
    },
    "possible_kinds": {
      "$ref": "#/definitions/MyEnum"
    }
  },
  "required": [
    "kind",
    "possible_kinds"
  ],
  "definitions": {
    "MyEnum": {
      "title": "MyEnum",
      "description": "An enumeration.",
      "enum": [
        "foo",
        "bar"
      ],
      "type": "string"
    }
  }
}
```

On the other hand, when typing the property as `Literal[MyEnum.MEMBER.value]`, the correct JSON schema is generated, accepting only the specific value. This solution though has the drawback of placing the value in the object instead of the enum member (so you lose `is MyEnum.FOO` comparisons).
```py
from typing import Literal
from enum import Enum

from pydantic import BaseModel


class MyEnum(str, Enum):
    FOO = "foo"
    BAR = "bar"


class ModelWithLiteralEnumValue(BaseModel):
    kind: Literal[MyEnum.FOO.value]

print(ModelWithLiteralEnumValue.schema_json())  # But this one is specific about what value is accepted
```
which gives this schema:
```json
{
  "title": "ModelWithLiteralEnumValue",
  "type": "object",
  "properties": {
    "kind": {
      "title": "Kind",
      "enum": [
        "foo"
      ],
      "type": "string"
    }
  },
  "required": [
    "kind"
  ]
}
```

This depends on #2534 being fixed, because we cannot even put a property typed as `Literal[MyEnum.MEMBER]` alone without the `.schema()` method crashing.

You can also look at a more complete example in different Pydantic versions there: https://gist.github.com/nymous/686d38ba1aa150fefe6da6d78b1cb441 (same gist as #2534).


Comments:

Patch:
diff --git a/pydantic/schema.py b/pydantic/schema.py
--- a/pydantic/schema.py
+++ b/pydantic/schema.py
@@ -794,6 +794,7 @@ def field_singleton_schema(  # noqa: C901 (ignore complexity)
     f_schema: Dict[str, Any] = {}
     if field.field_info is not None and field.field_info.const:
         f_schema['const'] = field.default
+
     if is_literal_type(field_type):
         values = all_literal_values(field_type)
 
@@ -810,8 +811,8 @@ def field_singleton_schema(  # noqa: C901 (ignore complexity)
         # All values have the same type
         field_type = values[0].__class__
         f_schema['enum'] = list(values)
-
-    if lenient_issubclass(field_type, Enum):
+        add_field_type_to_schema(field_type, f_schema)
+    elif lenient_issubclass(field_type, Enum):
         enum_name = model_name_map[field_type]
         f_schema, schema_overrides = get_field_info_schema(field)
         f_schema.update(get_schema_ref(enum_name, ref_prefix, ref_template, schema_overrides))


Test patch:
diff --git a/tests/test_schema.py b/tests/test_schema.py
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -1784,6 +1784,22 @@ class Model(BaseModel):
     }
 
 
+def test_literal_enum():
+    class MyEnum(str, Enum):
+        FOO = 'foo'
+        BAR = 'bar'
+
+    class Model(BaseModel):
+        kind: Literal[MyEnum.FOO]
+
+    assert Model.schema() == {
+        'title': 'Model',
+        'type': 'object',
+        'properties': {'kind': {'title': 'Kind', 'enum': ['foo'], 'type': 'string'}},
+        'required': ['kind'],
+    }
+
+
 def test_color_type():
     class Model(BaseModel):
         color: Color