datamodel-code-generator
datamodel-code-generator copied to clipboard
Validation error on anyOf property
Describe the bug Model generation seems to fail on anyOf, I encountered this issue for the SARIF spec:
- https://raw.githubusercontent.com/oasis-tcs/sarif-spec/123e95847b13fbdd4cbe2120fa5e33355d4a042b/Schemata/sarif-schema-2.1.0.json
To Reproduce
Used commandline:
wget https://raw.githubusercontent.com/oasis-tcs/sarif-spec/123e95847b13fbdd4cbe2120fa5e33355d4a042b/Schemata/sarif-schema-2.1.0.json
$ datamodel-codegen sarif-schema-2.1.0.json --output model.py
...
File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 2 validation errors for JsonSchemaObject
properties -> anyOf
value is not a valid dict (type=type_error.dict)
properties -> anyOf
value could not be parsed to a boolean (type=type_error.bool)
Expected behavior No errors 😅
PR welcome.
@JoranHonig @gaborbernat
For me this looks like an issue with the schema provided and not a bug. The anyOf under region has wrong indent and is part of properties, which is correct in main branch of SARIF spec
--- sarif-schema-2.1.0.issue.json 2025-05-30 08:54:39.366734510 +0200
+++ sarif-schema-2.1.0.main.json 2025-05-30 08:44:56.320744399 +0200
@@ -1,7 +1,7 @@
{
- "$schema": "https://json-schema.org/draft/2020-12/schema",
+ "$schema": "http://json-schema.org/draft-04/schema#",
"title": "Static Analysis Results Format (SARIF) Version 2.1.0 JSON Schema",
- "$id": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
+ "id": "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json",
"description": "Static Analysis Results Format (SARIF) Version 2.1.0 JSON Schema: a standard format for the output of static analysis tools.",
"additionalProperties": false,
"type": "object",
@@ -1778,14 +1778,14 @@
"properties": {
"description": "Key/value pairs that provide additional information about the region.",
"$ref": "#/definitions/propertyBag"
- },
+ }
+ },
- "anyOf": [
- { "required": [ "startLine" ] },
- { "required": [ "charOffset" ] },
- { "required": [ "byteOffset" ] }
- ]
- }
+ "anyOf": [
+ { "required": [ "startLine" ] },
+ { "required": [ "charOffset" ] },
+ { "required": [ "byteOffset" ] }
+ ]
},
"replacement": {
datamodel-codegen works fine with the current version of SARIF spec.
@JoranHonig could you confirm it was a SARIF spec issue rather than a datamodel-codegen issue? Thanks!