Fix encoding of 'Any' type in jsonschema (#1438)

This commit is contained in:
Ross Wolf
2021-08-19 10:15:21 -06:00
committed by GitHub
parent 2d517432e3
commit 11c443ba26
8 changed files with 185 additions and 28 deletions
+8 -1
View File
@@ -10,7 +10,7 @@ import marshmallow_dataclass
import marshmallow_dataclass.union_field
import marshmallow_jsonschema
import marshmallow_union
from marshmallow import Schema
from marshmallow import Schema, fields
from .utils import cached
@@ -117,6 +117,13 @@ class PatchedJSONSchema(marshmallow_jsonschema.JSONSchema):
# Patch marshmallow-jsonschema to support marshmallow-dataclass[union]
def _get_schema_for_field(self, obj, field):
"""Patch marshmallow_jsonschema.base.JSONSchema to support marshmallow-dataclass[union]."""
if isinstance(field, fields.Raw) and field.allow_none and not field.validate:
# raw fields shouldn't be type string but type any. bug in marshmallow_dataclass:__init__.py:
# if typ is Any:
# metadata.setdefault("allow_none", True)
# return marshmallow.fields.Raw(**metadata)
return {"type": ["string", "number", "object", "array", "boolean", "null"]}
if isinstance(field, marshmallow_dataclass.union_field.Union):
# convert to marshmallow_union.Union
field = marshmallow_union.Union([subfield for _, subfield in field.union_fields],
+2 -2
View File
@@ -42,7 +42,7 @@ class RuleMeta(MarshmallowDataclassMixin):
related_endpoint_rules: Optional[List[str]]
# Extended information as an arbitrary dictionary
extended: Optional[dict]
extended: Optional[Dict[str, Any]]
def get_validation_stack_versions(self) -> Dict[str, dict]:
"""Get a dict of beats and ecs versions per stack release."""
@@ -157,7 +157,7 @@ class BaseRuleData(MarshmallowDataclassMixin):
interval: Optional[definitions.Interval]
max_signals: Optional[definitions.MaxSignals]
meta: Optional[dict]
meta: Optional[Dict[str, Any]]
name: str
note: Optional[definitions.Markdown]
# can we remove this comment?
+28 -4
View File
@@ -4,7 +4,13 @@
"properties": {
"actions": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -25,7 +31,13 @@
},
"exceptions_list": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -38,7 +50,13 @@
"filters": {
"items": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
@@ -63,7 +81,13 @@
},
"meta": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
+28 -4
View File
@@ -4,7 +4,13 @@
"properties": {
"actions": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -25,7 +31,13 @@
},
"exceptions_list": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -38,7 +50,13 @@
"filters": {
"items": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
@@ -75,7 +93,13 @@
},
"meta": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
@@ -4,7 +4,13 @@
"properties": {
"actions": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -29,7 +35,13 @@
},
"exceptions_list": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -42,7 +54,13 @@
"filters": {
"items": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
@@ -80,7 +98,13 @@
},
"meta": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
+28 -4
View File
@@ -4,7 +4,13 @@
"properties": {
"actions": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -25,7 +31,13 @@
},
"exceptions_list": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -38,7 +50,13 @@
"filters": {
"items": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
@@ -77,7 +95,13 @@
},
"meta": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
@@ -4,7 +4,13 @@
"properties": {
"actions": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -31,7 +37,13 @@
},
"exceptions_list": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -44,7 +56,13 @@
"filters": {
"items": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
@@ -89,7 +107,13 @@
},
"meta": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
@@ -285,7 +309,13 @@
"threat_filters": {
"items": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
+28 -4
View File
@@ -4,7 +4,13 @@
"properties": {
"actions": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -25,7 +31,13 @@
},
"exceptions_list": {
"items": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "array"
},
@@ -38,7 +50,13 @@
"filters": {
"items": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},
@@ -77,7 +95,13 @@
},
"meta": {
"additionalProperties": {
"type": "string"
"type": [
"string",
"number",
"object",
"array",
"boolean"
]
},
"type": "object"
},