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

fix: Fix regex expression of hostname in pydantic v2 #1449

Merged
merged 3 commits into from
Aug 1, 2023

Conversation

xu-cheng
Copy link
Contributor

@xu-cheng xu-cheng commented Jul 25, 2023

The original regex causes errors when loading the generated model.

To Reproduce

Example schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Example",
  "type": "object",
  "properties": {
    "data": {
      "type": "string",
      "format": "hostname"
    }
  }
}

Used commandline:

$ datamodel-codegen --output-model-type pydantic_v2.BaseModel --input schema.json --output model.py
$ python3 -m model

Output:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/private/tmp/test/model.py", line 12, in <module>
    class Example(BaseModel):
  File "/private/tmp/test/env/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 174, in __new__
    complete_model_class(
  File "/private/tmp/test/env/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 449, in complete_model_class
    cls.__pydantic_validator__ = SchemaValidator(simplified_core_schema, core_config)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.SchemaError: Error building "model" validator:
  SchemaError: Error building "model-fields" validator:
  SchemaError: Field "data":
  SchemaError: Error building "default" validator:
  SchemaError: Error building "nullable" validator:
  SchemaError: Error building "str" validator:
  SchemaError: regex parse error:
    ^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9])\Z
                                                                                                                        ^^
error: unrecognized escape sequence

@codecov
Copy link

codecov bot commented Jul 27, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (e9b6edf) 100.00% compared to head (e5e1a32) 100.00%.
Report is 676 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##            master     #1449     +/-   ##
===========================================
  Coverage   100.00%   100.00%             
===========================================
  Files           11        32     +21     
  Lines         1020      3556   +2536     
  Branches       201       834    +633     
===========================================
+ Hits          1020      3556   +2536     
Flag Coverage Δ
unittests 99.63% <99.58%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
datamodel_code_generator/__init__.py 100.00% <100.00%> (ø)
datamodel_code_generator/__main__.py 100.00% <100.00%> (ø)
datamodel_code_generator/format.py 100.00% <100.00%> (ø)
datamodel_code_generator/http.py 100.00% <100.00%> (ø)
datamodel_code_generator/imports.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/__init__.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/base.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/dataclass.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/enum.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/improts.py 100.00% <100.00%> (ø)
... and 22 more

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@koxudaxi koxudaxi merged commit 1fe472e into koxudaxi:master Aug 1, 2023
73 checks passed
@xu-cheng xu-cheng deleted the pydantic-v2-hostname branch August 1, 2023 16:44
@koxudaxi
Copy link
Owner

koxudaxi commented Aug 5, 2023

@xu-cheng
Sorry for the delay in thanking you.
Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants