Skip to content

Instantly share code, notes, and snippets.

@adamcharnock
Last active March 2, 2018 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcharnock/b1a5d63e718a4359f2de77fadedf8895 to your computer and use it in GitHub Desktop.
Save adamcharnock/b1a5d63e718a4359f2de77fadedf8895 to your computer and use it in GitHub Desktop.
Extracting a json scheme from Python 3 typing hinting (examples)
// Source: https://github.com/adamcharnock/lightbus/blob/master/experiments/types_to_jsonschema.py
// $ python experiments/types_to_jsonschema.py
// Example:
// def check_password(username: str, password: str) -> bool: pass
// Parameter schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "check_password() parameters",
"type": "object",
"additionalProperties": false,
"properties": {
"username": {
"type": "string"
},
"password": {
"type": "string"
}
},
"required": [
"username",
"password"
]
}
// Response schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "check_password() parameters",
"type": "boolean"
}
// Example:
// def get_user(username: str) -> User: pass
// Parameter schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "get_user() parameters",
"type": "object",
"additionalProperties": false,
"properties": {
"username": {
"type": "string"
}
},
"required": [
"username"
]
}
// Response schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "get_user() parameters",
"type": "object",
"title": "User",
"properties": {
"username": {
"type": "string"
},
"password": {
"type": "string"
},
"is_admin": {
"type": "boolean",
"default": false
}
},
"required": [
"username",
"password"
],
"additionalProperties": false
}
// Example:
// def get_user(username: str) -> User: pass
// Parameter schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "get_user() parameters",
"type": "object",
"additionalProperties": false,
"properties": {
"username": {
"type": "string"
}
},
"required": [
"username"
]
}
// Response schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "get_user() parameters",
"type": "object",
"title": "User",
"properties": {
"username": {
"type": "string"
},
"password": {
"type": "string"
},
"is_admin": {
"type": "boolean",
"default": false
}
},
"required": [
"username",
"password"
],
"additionalProperties": false
}
// Example:
// def tuple_return_type(username: str) -> Tuple[str, int, bool]: pass
// Parameter schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "tuple_return_type() parameters",
"type": "object",
"additionalProperties": false,
"properties": {
"username": {
"type": "string"
}
},
"required": [
"username"
]
}
// Response schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "tuple_return_type() parameters",
"type": "array",
"maxItems": 3,
"minItems": 3,
"items": [
{
"type": "string"
},
{
"type": "number"
},
{
"type": "boolean"
}
]
}
// Example:
// def union_parameter(weird_value: Union[str, int]) -> list: pass
// Parameter schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "union_parameter() parameters",
"type": "object",
"additionalProperties": false,
"properties": {
"weird_value": {
"oneOf": [
{
"type": "string"
},
{
"type": "number"
}
]
}
},
"required": [
"weird_value"
]
}
// Response schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "union_parameter() parameters",
"type": "array"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment