Usage Examples
# typescript model generation in Hinkskalle
curl https://kuebel.testha.se/swagger |
spotta - typescript \
-c Collection \
-c Container \
\ # ...
-c Manifest \
-t backend/share/typescript-templates \
-o frontend/src/store/models.ts
# python model generation in hinkskalle-api
curl -f https://kuebel.testha.se/swagger |
spotta -v DEBUG - python \
-c Collection \
-c Container \
\ # ...
-c Manifest \
-c TagData \
-c Token \
-t hinkskalle_api/auto \
-o hinkskalle_api/auto/models.py
Output Examples
Input is the Pet definition from the classic Petstore example.
Python
swagspotta generates 2 functions for each model:
plainTo[Model](json)
to instantiate an object and fill it with values from deserialized jsonserialize[Model](obj)
to generate a plain dict of the object for JSON unmarshalling.
The model class is a simple dataclass with all attributes optional (could be improved). swagspotta also adds type annotations.
Nested definitions are de/serialized recursively. Fields with the readOnly
attribute set are not serialized.
curl -f https://petstore.swagger.io/v2/swagger.json | spotta - python -c Pet
def plainToPet(json: dict) -> Pet:
obj = Pet()
obj.id = json.get('id')
obj.category = plainToCategory(json['category']) if json.get('category') is not None else None
obj.name = json.get('name')
obj.color = json.get('color')
obj.photoUrls = json['photoUrls'] if json.get('photoUrls') is not None and isinstance(json['photoUrls'], list) else []
obj.tags = [
plainToTag(o) for o in (json['tags'] if json.get('tags') is not None and isinstance(json['tags'], list) else [])
]
obj.status = json.get('status')
return obj
def serializePet(obj: Pet) -> dict:
json = {}
json['id'] = obj.id
json['category'] = serializeCategory(obj.category) if obj.category is not None else None
json['name'] = obj.name
json['photoUrls'] = obj.photoUrls
json['tags'] = [ serializeTag(o) for o in obj.tags ] if obj.tags is not None else []
json['status'] = obj.status
return json
@dataclass
class Pet:
id: typing.Optional[int] = None
category: typing.Optional[Category] = None
name: typing.Optional[str] = None
color: typing.Optional[str] = None
photoUrls: list[str] = field(default_factory=list)
tags: list[Tag] = field(default_factory=list)
status: typing.Optional[str] = None
Typescript
Similar to python swagspotta generates 2 functions for each definition:
plainTo[model](json)
to instantiate an object and fill it with values from a json objectserialize[model](obj, unroll=false)
to turn an object into a plain object (hash, dict)
The model class is a simple class with all attributes optional (not null assertions, could be improved).
Nested definitions are deserialized recursively. serialize has an optional parameter unroll
that can be set to true
to enable recursive serialization, too. Fields with readOnly
set are not serialized.
using two lodash functions, must be installed
curl -f https://petstore.swagger.io/v2/swagger.json | spotta - typescript -c Pet
import { isNil as _isNil, map as _map } from 'lodash';
class Pet {
public id!: number
public category!: Category
public name!: string
public photoUrls!: string[]
public tags!: Tag[]
public status!: string
}
export function plainToPet(json: any): Pet {
const obj = new Pet();
obj.id = json['id'];
if (!_isNil(json['category'])) obj.category = plainToCategory(json['category']);
obj.name = json['name'];
obj.photoUrls = json['photoUrls'];
if (!_isNil(json['tags'])) obj.tags = _map(json['tags'], plainToTag);
obj.status = json['status'];
return obj;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function serializePet(obj: Pet, unroll=false): any {
const json: any = {};
json['id'] = obj.id;
if (unroll) json['category'] = _isNil(obj.category) ? null : serializeCategory(obj.category);
json['name'] = obj.name;
json['photoUrls'] = obj.photoUrls;
if (unroll) json['tags'] = _isNil(obj.tags) ? [] : _map(obj.tags, f => serializeTag(f));
json['status'] = obj.status;
return json;
}
export { Pet };