Skip to content

Commit

Permalink
Merge pull request #38 from ccaroon/support-oneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
ghandic authored Oct 4, 2022
2 parents d10de7d + 0d7a9f3 commit 01c9b6f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/jsf/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pydantic import conlist
from smart_open import open as s_open

from .schema_types import AllTypes, Array, JSFEnum, JSFTuple, Object, PrimativeTypes, Primitives, AnyOf
from .schema_types import AllTypes, Array, JSFEnum, JSFTuple, Object, PrimativeTypes, Primitives, AnyOf, OneOf

logger = logging.getLogger()
faker = Faker()
Expand Down Expand Up @@ -117,7 +117,12 @@ def __parse_definition(self, name: str, path: str, schema: Dict[str, Any]) -> Al
schemas = []
for d in schema["anyOf"]:
schemas.append(self.__parse_definition(name, path, d))
return AnyOf(schemas=schemas)
return AnyOf(name=name, path=path, schemas=schemas)
elif "oneOf" in schema:
schemas = []
for d in schema["oneOf"]:
schemas.append(self.__parse_definition(name, path, d))
return OneOf(name=name, path=path, schemas=schemas)
else:
raise ValueError(f"Cannot parse schema {repr(schema)}") # pragma: no cover

Expand Down
3 changes: 2 additions & 1 deletion src/jsf/schema_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .object import Object
from .string import String
from .anyof import AnyOf
from .oneof import OneOf

Primitives = {
"number": Number,
Expand All @@ -19,5 +20,5 @@
"null": Null,
}

AllTypes = Union[JSFEnum, Object, Array, JSFTuple, String, Boolean, Null, Number, Integer, AnyOf]
AllTypes = Union[JSFEnum, Object, Array, JSFTuple, String, Boolean, Null, Number, Integer, AnyOf, OneOf]
PrimativeTypes = Union[String, Boolean, Null, Number, Integer]
20 changes: 20 additions & 0 deletions src/jsf/schema_types/oneof.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import random
from typing import Any, Dict, List, Optional

from .base import BaseSchema, ProviderNotSetException


class OneOf(BaseSchema):
schemas: List[BaseSchema] = None

def from_dict(d):
return OneOf(**d)

def generate(self, context: Dict[str, Any]) -> Optional[List[Any]]:
try:
return super().generate(context)
except ProviderNotSetException:
return random.choice(self.schemas).generate(context)

def model(self, context: Dict[str, Any]):
pass
11 changes: 11 additions & 0 deletions src/tests/data/oneof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"oneOf": [
{
"type": "string",
"maxLength": 5
},
{
"type": "boolean"
}
]
}
8 changes: 8 additions & 0 deletions src/tests/test_default_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def test_fake_anyof(TestData):
for d in fake_data:
assert isinstance(d, str) or isinstance(d, float)

def test_fake_oneof(TestData):
with open(TestData / f"oneof.json", "r") as file:
schema = json.load(file)
p = JSF(schema)

fake_data = [p.generate() for _ in range(10)]
for d in fake_data:
assert isinstance(d, bool) or isinstance(d, str)

def test_fake_boolean(TestData):
with open(TestData / f"boolean.json", "r") as file:
Expand Down

0 comments on commit 01c9b6f

Please sign in to comment.