Repository: pydantic/pydantic (Python)

Issue:
Add `default` in JSON Schema when `const=True`
### 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

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
             pydantic version: 1.9.0
            pydantic compiled: True
                 install path: ...
               python version: 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
                     platform: Windows-10-10.0.22000-SP0
     optional deps. installed: ['typing-extensions']
```

When running this snippet of code:

```py
from pydantic import BaseModel, Field

class MyModel(BaseModel):
    x: int = Field(default=0, const=True)

print(MyModel.schema_json(indent=2))
```

this is the result:

```json
{
  "title": "MyModel",
  "type": "object",
  "properties": {
    "x": {
      "title": "X",
      "const": 0,
      "type": "integer"
    }
  }
}
```

I am having issues with some JSON Schema validator because of what I think is a subtle difference here - yes, we're setting `x` to be `const=0`, but *we're not saying that the default is also zero*, which leaves the field as not filled if one creates a default object. I don't think this is correct behaviour. I suggest deleting https://github.com/samuelcolvin/pydantic/blob/8997cc5961139dd2695761a33c06a66adbf1430a/pydantic/schema.py#L215 , which should fix the problem.


Discussion:
PR welcome, but you'll need to see if it breaks other tests and go back to where we originally added that line and see if there's a reason we did it that way.



Patch:
diff --git a/changes/4031-aminalaee.md b/changes/4031-aminalaee.md
new file mode 100644
index 00000000000..f694600d50d
--- /dev/null
+++ b/changes/4031-aminalaee.md
@@ -0,0 +1,1 @@
+Add `default` value in JSON Schema when `const=True`
diff --git a/pydantic/schema.py b/pydantic/schema.py
index d37e82206c2..134da1a3292 100644
--- a/pydantic/schema.py
+++ b/pydantic/schema.py
@@ -210,12 +210,7 @@ def get_field_info_schema(field: ModelField, schema_overrides: bool = False) ->
         schema_['description'] = field.field_info.description
         schema_overrides = True
 
-    if (
-        not field.required
-        and not field.field_info.const
-        and field.default is not None
-        and not is_callable_type(field.outer_type_)
-    ):
+    if not field.required and field.default is not None and not is_callable_type(field.outer_type_):
         schema_['default'] = encode_default(field.default)
         schema_overrides = True
 


Test patch:
diff --git a/tests/test_main.py b/tests/test_main.py
index f4d3844c4aa..2ca3004fffb 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -585,12 +585,14 @@ class Model(BaseModel):
         'properties': {
             'a': {
                 'const': [SubModel(b=1), SubModel(b=2), SubModel(b=3)],
+                'default': [{'b': 1}, {'b': 2}, {'b': 3}],
                 'items': {'$ref': '#/definitions/SubModel'},
                 'title': 'A',
                 'type': 'array',
             },
             'b': {
                 'const': [{'b': 4}, {'b': 5}, {'b': 6}],
+                'default': [{'b': 4}, {'b': 5}, {'b': 6}],
                 'items': {'$ref': '#/definitions/SubModel'},
                 'title': 'B',
                 'type': 'array',
diff --git a/tests/test_schema.py b/tests/test_schema.py
index a483e3fad1f..617dfe09a42 100644
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -549,7 +549,7 @@ class Model(BaseModel):
     assert Model.schema() == {
         'title': 'Model',
         'type': 'object',
-        'properties': {'a': {'title': 'A', 'type': 'string', 'const': 'some string'}},
+        'properties': {'a': {'title': 'A', 'type': 'string', 'const': 'some string', 'default': 'some string'}},
     }