@@ -1339,29 +1339,58 @@ The `--field-extra-keys` flag configures the code generation behavior.
13391339
13401340??? example "Output"
13411341
1342- ```python
1343- # generated by datamodel-codegen:
1344- # filename: extras.json
1345- # timestamp: 2019-07-26T00:00:00+00:00
1346-
1347- from __future__ import annotations
1348-
1349- from typing import Optional
1350-
1351- from pydantic import BaseModel, Field
1352-
1353-
1354- class Extras(BaseModel):
1355- name: Optional[str] = Field(
1356- None,
1357- description='normal key',
1358- example='example',
1359- invalid_key_1='abc',
1360- key2=456,
1361- repr=True,
1362- )
1363- age: Optional[int] = Field(None, example=12, examples=[13, 20])
1364- ```
1342+ === "Pydantic v1"
1343+
1344+ ```python
1345+ # generated by datamodel-codegen:
1346+ # filename: extras.json
1347+ # timestamp: 2019-07-26T00:00:00+00:00
1348+
1349+ from __future__ import annotations
1350+
1351+ from typing import Optional
1352+
1353+ from pydantic import BaseModel, Field
1354+
1355+
1356+ class Extras(BaseModel):
1357+ name: Optional[str] = Field(
1358+ None,
1359+ description='normal key',
1360+ example='example',
1361+ invalid_key_1='abc',
1362+ key2=456,
1363+ repr=True,
1364+ )
1365+ age: Optional[int] = Field(None, example=12, examples=[13, 20])
1366+ ```
1367+
1368+ === "Pydantic v2"
1369+
1370+ ```python
1371+ # generated by datamodel-codegen:
1372+ # filename: extras.json
1373+ # timestamp: 2019-07-26T00:00:00+00:00
1374+
1375+ from __future__ import annotations
1376+
1377+ from typing import Optional
1378+
1379+ from pydantic import BaseModel, Field
1380+
1381+
1382+ class Extras(BaseModel):
1383+ name: Optional[str] = Field(
1384+ None,
1385+ description='normal key',
1386+ examples=['example'],
1387+ json_schema_extra={'key2': 456, 'invalid-key-1': 'abc'},
1388+ repr=True,
1389+ )
1390+ age: Optional[int] = Field(
1391+ None, examples=[13, 20], json_schema_extra={'example': 12}
1392+ )
1393+ ```
13651394
13661395---
13671396
@@ -1422,37 +1451,77 @@ in Field(). This is useful for custom schema extensions and vendor-specific meta
14221451
14231452??? example "Output"
14241453
1425- ```python
1426- # generated by datamodel-codegen:
1427- # filename: extras.json
1428- # timestamp: 2019-07-26T00:00:00+00:00
1429-
1430- from __future__ import annotations
1431-
1432- from typing import Optional
1433-
1434- from pydantic import BaseModel, Field
1435-
1436-
1437- class Extras(BaseModel):
1438- name: Optional[str] = Field(
1439- None,
1440- description='normal key',
1441- example='example',
1442- field_comment='comment',
1443- field_exclude=123,
1444- field_invalid_key_2='efg',
1445- invalid_key_1='abc',
1446- key1=123,
1447- key2=456,
1448- readOnly=True,
1449- register_='hij',
1450- repr=True,
1451- schema_='klm',
1452- x_abc=True,
1453- )
1454- age: Optional[int] = Field(None, example=12, examples=[13, 20], writeOnly=True)
1455- ```
1454+ === "Pydantic v1"
1455+
1456+ ```python
1457+ # generated by datamodel-codegen:
1458+ # filename: extras.json
1459+ # timestamp: 2019-07-26T00:00:00+00:00
1460+
1461+ from __future__ import annotations
1462+
1463+ from typing import Optional
1464+
1465+ from pydantic import BaseModel, Field
1466+
1467+
1468+ class Extras(BaseModel):
1469+ name: Optional[str] = Field(
1470+ None,
1471+ description='normal key',
1472+ example='example',
1473+ field_comment='comment',
1474+ field_exclude=123,
1475+ field_invalid_key_2='efg',
1476+ invalid_key_1='abc',
1477+ key1=123,
1478+ key2=456,
1479+ readOnly=True,
1480+ register_='hij',
1481+ repr=True,
1482+ schema_='klm',
1483+ x_abc=True,
1484+ )
1485+ age: Optional[int] = Field(None, example=12, examples=[13, 20], writeOnly=True)
1486+ ```
1487+
1488+ === "Pydantic v2"
1489+
1490+ ```python
1491+ # generated by datamodel-codegen:
1492+ # filename: extras.json
1493+ # timestamp: 2019-07-26T00:00:00+00:00
1494+
1495+ from __future__ import annotations
1496+
1497+ from typing import Optional
1498+
1499+ from pydantic import BaseModel, Field
1500+
1501+
1502+ class Extras(BaseModel):
1503+ name: Optional[str] = Field(
1504+ None,
1505+ description='normal key',
1506+ examples=['example'],
1507+ json_schema_extra={
1508+ 'key1': 123,
1509+ 'key2': 456,
1510+ '$exclude': 123,
1511+ 'invalid-key-1': 'abc',
1512+ '-invalid+key_2': 'efg',
1513+ '$comment': 'comment',
1514+ 'register': 'hij',
1515+ 'schema': 'klm',
1516+ 'x-abc': True,
1517+ 'readOnly': True,
1518+ },
1519+ repr=True,
1520+ )
1521+ age: Optional[int] = Field(
1522+ None, examples=[13, 20], json_schema_extra={'example': 12, 'writeOnly': True}
1523+ )
1524+ ```
14561525
14571526---
14581527
0 commit comments