Repository: pydantic/pydantic

Issue:
Possible regression of __modify_schema__ on master
# Bug

I think #1422 causes an unintentional (?) change in the behaviour of `__modify_schema__` - it's now applying to the list schema as well as the item schema.


Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.5.1
            pydantic compiled: False
                 install path: /home/johnc/Projects/pydantic/pydantic
               python version: 3.7.7 (default, Apr 18 2020, 02:59:53)  [GCC 9.3.0]
                     platform: Linux-5.4.0-29-generic-x86_64-with-Ubuntu-20.04-focal
     optional deps. installed: []
```
Actually at git version 5195e55c102e537be6c00579df8f464ecd90b5ff - 

```
pip freeze | grep pydantic
-e git+https://github.com/samuelcolvin/pydantic.git@5195e55c102e537be6c00579df8f464ecd90b5ff#egg=pydantic
```

The output of the following code is different between master and v1.5.1

```py
from typing import List

import pydantic


class MyField(str):
  @classmethod
  def __modify_schema__(cls, field_schema):
    field_schema["foo"] = "buzz"


class Foo(pydantic.BaseModel):
  a_field: MyField

  some_fields: List[MyField]

print(Foo.schema_json(indent=2))
```

With 1.5.1 this outputs:

```
{
  "title": "Foo",
  "type": "object",
  "properties": {
    "a_field": {
      "title": "A Field",
      "type": "string",
      "foo": "buzz"
    },
    "some_fields": {
      "title": "Some Fields",
      "type": "array",
      "items": {
        "type": "string",
        "foo": "buzz"
      }
    }
  },
  "required": [
    "a_field",
    "some_fields"
  ]
}
```

With current master this outputs:

```
{
  "title": "Foo",
  "type": "object",
  "properties": {
    "a_field": {
      "title": "A Field",
      "foo": "buzz",
      "type": "string"
    },
    "some_fields": {
      "title": "Some Fields",
      "foo": "buzz",
      "type": "array",
      "items": {
        "type": "string",
        "foo": "buzz"
      }
    }
  },
  "required": [
    "a_field",
    "some_fields"
  ]
}
```

Diff:

```diff
 {
   "title": "Foo",
   "type": "object",
   "properties": {
     "a_field": {
       "title": "A Field",
-      "type": "string",
-      "foo": "buzz"
+      "foo": "buzz",
+      "type": "string"
     },
     "some_fields": {
       "title": "Some Fields",
+      "foo": "buzz",
       "type": "array",
       "items": {
         "type": "string",
         "foo": "buzz"
       }
     }
   },
   "required": [
     "a_field",
     "some_fields"
   ]
 }
 ```

Note the extra "foo" in `some_fields` - I think this is a regression?



Discussion:

Resolving patch:
diff --git a/pydantic/schema.py b/pydantic/schema.py
--- a/pydantic/schema.py
+++ b/pydantic/schema.py
@@ -239,7 +239,7 @@ def get_field_schema_validations(field: ModelField) -> Dict[str, Any]:
         f_schema['const'] = field.default
     if field.field_info.extra:
         f_schema.update(field.field_info.extra)
-    modify_schema = getattr(field.type_, '__modify_schema__', None)
+    modify_schema = getattr(field.outer_type_, '__modify_schema__', None)
     if modify_schema:
         modify_schema(f_schema)
     return f_schema


Test patch:
diff --git a/tests/test_schema.py b/tests/test_schema.py
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -1822,6 +1822,7 @@ def __modify_schema__(cls, schema):
     class Model(BaseModel):
         path1: Path
         path2: MyPath
+        path3: List[MyPath]
 
     assert Model.schema() == {
         'title': 'Model',
@@ -1829,8 +1830,9 @@ class Model(BaseModel):
         'properties': {
             'path1': {'title': 'Path1', 'type': 'string', 'format': 'path'},
             'path2': {'title': 'Path2', 'type': 'string', 'format': 'path', 'foobar': 123},
+            'path3': {'title': 'Path3', 'type': 'array', 'items': {'type': 'string', 'format': 'path', 'foobar': 123}},
         },
-        'required': ['path1', 'path2'],
+        'required': ['path1', 'path2', 'path3'],
     }