question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple pattern properties are not handled correctly

See original GitHub issue

Describe the bug

JSON schema pattern properties can provide multiple patterns where any object property key must match one of the patterns. This is probably not handled correctly by models generated from such a schema.

To Reproduce

Example schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Foo",
  "type": "object",
  "properties": {
    "bar": {
      "type": "object",
      "patternProperties": {
        "^A_\\d$": {
          "type": "string"
        },
        "^B_\\d$": {
          "type": "string"
        }
      }
    }
  }
}

Used commandline:

$ datamodel-codegen --input tests/data/jsonschema/pattern_properties.json

Expected behavior The given command should generate a model that handles this example data:

{
  "bar": {
    "A_1": "a",
    "B_2": "b"
  }
}

However, it will fail with the following error message:

pydantic.error_wrappers.ValidationError: 2 validation errors for Foo
bar -> __key__
  string does not match regex "^A_\d$" (type=value_error.str.regex; pattern=^A_\d$)
bar -> __key__
  string does not match regex "^B_\d$" (type=value_error.str.regex; pattern=^B_\d$)

Version:

  • OS: Ubuntu 20.04
  • Python version: 3.8.10
  • datamodel-code-generator version: 0.11.15

Additional context Add any other context about the problem here.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
georgmscommented, Jan 11, 2022

So the generator comes up with something like

class Foo(BaseModel):
    bar: Optional[
        Union[
             Dict[constr(regex=r'^A_\d$'), str],
             Dict[constr(regex=r'^B_\d$'), str],
        ]
    ] = None

However, the correct way is

class Foo(BaseModel):
    bar: Optional[
        Dict[
            Union[constr(regex=r'^A_\d$'), constr(regex=r'^B_\d$')],
            str,
        ]
    ] = None

So Union inside Dict, not the other way around. I’ll try to come up with a fix for that.

0reactions
koxudaxicommented, Jan 11, 2022

@georgms Thank you for creating PR and the issue.

Nevermind. This only works for the case where all keys the same type str.

We should discuss the best way in the pydantic project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Correctly handling multiple views in a MVC javascript game
I think the ViewModel method is the way to go. I now have the following: UniverseDomainObject (and other miscellaneous Domain objects like ...
Read more >
Pattern matching overview - C# guide | Microsoft Learn
The two relational patterns are surrounded by parentheses, which you can use around any pattern for clarity. The final two switch arms handle...
Read more >
Everything you need to know about Regular Expressions
Most characters in a regex pattern do not have a special meaning, ... In many regex engines — such as Java, JavaScript, Python, ......
Read more >
Python's property(): Add Managed Attributes to Your Classes
In this example, you create Point with two non-public attributes ._x and ._y to hold the Cartesian coordinates of the point at hand....
Read more >
Pattern (Java Platform SE 7 ) - Oracle Help Center
Instances of this class are immutable and are safe for use by multiple concurrent threads. Instances of the Matcher class are not safe...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found