Skip to content

Instantly share code, notes, and snippets.

@johanste
Created December 4, 2020 22:53
Show Gist options
  • Save johanste/8330999be426a5728ba508d16cd07b4a to your computer and use it in GitHub Desktop.
Save johanste/8330999be426a5728ba508d16cd07b4a to your computer and use it in GitHub Desktop.
Swagger as TypedDict (WIP)
import typing
class InfoFragment(typing.TypedDict):
title: str
version: str
OptionalInAttribute = typing.TypedDict(
'OptionalInAttribute', {'in': str}
)
RefFragment = typing.TypedDict('RefFragment', {'$ref': str})
class RequiredParameterFragment(typing.TypedDict):
name: str
type: str
class ParameterFragment(RequiredParameterFragment, OptionalInAttribute):
...
class ResponseFragment(typing.TypedDict):
description: str
schema: typing.Union[RefFragment]
class _OptionalOperationFragment(typing.TypedDict, total=False):
parameters: typing.List[typing.Union[ParameterFragment, RefFragment]]
class _RequiredOperationFragment(typing.TypedDict):
responses: typing.Mapping[str, ResponseFragment]
class OperationFragment(_RequiredOperationFragment, _OptionalOperationFragment):
operationId: str
class _OptionalPathFragment(typing.TypedDict, total=False):
parameters: typing.List[typing.Union[ParameterFragment, RefFragment]]
delete: OperationFragment
patch: OperationFragment
post: OperationFragment
put: OperationFragment
class _RequredPathFragment(typing.TypedDict):
...
class PathFragment(_OptionalPathFragment, _RequredPathFragment, total=False):
get: OperationFragment
class OpenApiDocument(typing.TypedDict):
info: InfoFragment
paths: typing.Mapping[str, PathFragment]
data = {
"info": {
"title": "Hello",
"version": "1.0",
},
"paths": {
"/things": {
"parameters": [
{
"name": "filter",
"in": "query",
"type": "str"
},
{
"$ref": "#/parameters/apiversion"
}
],
"get": {
"operationId": "Something something...",
"responses": {
"200": {
"description": "Ok",
"schema": {
"$ref": "#/definitions/ThingPaged"
}
}
}
}
},
"/things/{thingId}": {
"get": {
"operationId": "Something else...",
"responses":{}
}
}
}
}
swagger: OpenApiDocument = {
"info": {
"title": "Hello",
"version": "1.0",
},
"paths": {
"/things": {
"parameters": [
{
"name": "filter",
"in": "query",
"type": "str"
},
{
"$ref": "#/parameters/apiversion"
}
],
"get": {
"operationId": "Something something...",
"responses": {
"200": {
"description": "Ok",
"schema": {
"$ref": "#/definitions/ThingPaged"
}
}
}
}
},
"/things/{thingId}": {
"get": {
"operationId": "Something else...",
"smurf": "<<--- This will give you uggly squigglies!",
"responses":{}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment