Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_string_references():
definitions = typesystem.SchemaDefinitions()
class ExampleA(typesystem.Schema, definitions=definitions):
field_on_a = typesystem.Integer()
example_b = typesystem.Reference("ExampleB")
class ExampleB(typesystem.Schema, definitions=definitions):
field_on_b = typesystem.Integer()
value = ExampleA.validate({"field_on_a": "123", "example_b": {"field_on_b": "456"}})
assert value == ExampleA(field_on_a=123, example_b=ExampleB(field_on_b=456))
class ExampleC(typesystem.Schema, definitions=definitions):
field_on_c = typesystem.Integer()
example_d = typesystem.Array(items=typesystem.Reference("ExampleD"))
class ExampleD(typesystem.Schema, definitions=definitions):
album = Album.validate(
{
"title": "Double Negative",
"release_date": "2018-09-14",
"artist": {"name": "Low"},
}
)
assert album == Album(
title="Double Negative",
release_date=datetime.date(2018, 9, 14),
artist=Artist(name="Low"),
)
# Identical class names in alternate definitions should not clash.
definitions = typesystem.SchemaDefinitions()
class Album(typesystem.Schema, definitions=definitions):
renamed_title = typesystem.String(max_length=100)
renamed_release_date = typesystem.Date()
renamed_artist = typesystem.Reference("Artist")
class Artist(typesystem.Schema, definitions=definitions):
renamed_name = typesystem.String(max_length=100)
album = Album.validate(
{
"renamed_title": "Double Negative",
"renamed_release_date": "2018-09-14",
"renamed_artist": {"renamed_name": "Low"},
}
)
import typesystem
from apistar.document import Document, Field, Link, Section
from apistar.schemas.jsonschema import JSON_SCHEMA
SCHEMA_REF = typesystem.Object(
properties={"$ref": typesystem.String(pattern="^#/components/schemas/")}
)
REQUESTBODY_REF = typesystem.Object(
properties={"$ref": typesystem.String(pattern="^#/components/requestBodies/")}
)
RESPONSE_REF = typesystem.Object(
properties={"$ref": typesystem.String(pattern="^#/components/responses/")}
)
definitions = typesystem.SchemaDefinitions()
OPEN_API = typesystem.Object(
title="OpenAPI",
properties={
"openapi": typesystem.String(),
"info": typesystem.Reference("Info", definitions=definitions),
"servers": typesystem.Array(
items=typesystem.Reference("Server", definitions=definitions)
),
"paths": typesystem.Reference("Paths", definitions=definitions),
"components": typesystem.Reference("Components", definitions=definitions),
"security": typesystem.Array(
items=typesystem.Reference("SecurityRequirement", definitions=definitions)
),
"tags": typesystem.Array(
items=typesystem.Reference("Tag", definitions=definitions)
def get_schema_definitions(self, data):
definitions = typesystem.SchemaDefinitions()
schemas = lookup(data, ["components", "schemas"], {})
for key, value in schemas.items():
ref = f"#/components/schemas/{key}"
definitions[ref] = typesystem.from_json_schema(
value, definitions=definitions
)
return definitions
import re
from urllib.parse import urljoin
import typesystem
from apistar.document import Document, Field, Link, Section
from apistar.schemas.jsonschema import JSON_SCHEMA
SCHEMA_REF = typesystem.Object(
properties={"$ref": typesystem.String(pattern="^#/definitiions/")}
)
RESPONSE_REF = typesystem.Object(
properties={"$ref": typesystem.String(pattern="^#/responses/")}
)
definitions = typesystem.SchemaDefinitions()
SWAGGER = typesystem.Object(
title="Swagger",
properties={
"swagger": typesystem.String(),
"info": typesystem.Reference("Info", definitions=definitions),
"paths": typesystem.Reference("Paths", definitions=definitions),
"host": typesystem.String(),
"basePath": typesystem.String(pattern="^/"),
"schemes": typesystem.Array(items=typesystem.Choice(choices=["http", "https", "ws", "wss"])),
"consumes": typesystem.Array(items=typesystem.String()),
"produces": typesystem.Array(items=typesystem.String()),
"definitions": typesystem.Object(additional_properties=typesystem.Any()),
"parameters": typesystem.Object(
additional_properties=typesystem.Reference(
"Parameters", definitions=definitions
import typesystem
definitions = typesystem.SchemaDefinitions()
JSON_SCHEMA = (
typesystem.Object(
properties={
"$ref": typesystem.String(),
"type": typesystem.String() | typesystem.Array(items=typesystem.String()),
"enum": typesystem.Array(unique_items=True, min_items=1),
"definitions": typesystem.Object(
additional_properties=typesystem.Reference(
"JSONSchema", definitions=definitions
)
),
# String
"minLength": typesystem.Integer(minimum=0),
"maxLength": typesystem.Integer(minimum=0),
"pattern": typesystem.String(format="regex"),
def get_schema_definitions(self, data):
definitions = typesystem.SchemaDefinitions()
schemas = lookup(data, ["components", "schemas"], {})
for key, value in schemas.items():
ref = f"#/components/schemas/{key}"
definitions[ref] = typesystem.from_json_schema(
value, definitions=definitions
)
return definitions