Swagspotta - Installation - Synopsis - Templates

Templates

I use Jinja2 as templating engine.

Structure

Each language has 3 basic templates:

Utils

The utils template contains macros to generate the serialization, deserialization and class code. These macros take a list of fields parsed from a swagger definition.

Class

The class template generates the plainTo*, serialize* and class using macros from the utils.

Models

All classes are concattenated into a big file generated from the models template.

Model Generation

swagspotta includes the default skeleton templates. When called with the -t/--class-templates options, we first look for templates for each class in that directory and copy the default template if not found. The local template is then used to generate the final class + model output.

Eg. after first calling

curl -f https://petstore.swagger.io/v2/swagger.json | spotta - typescript \
  -c Pet \
  -c Tag \
  --class-templates src/app/models/auto \
  -o src/app/models.ts

the src/app/models/auto directory will then contain these files:

ls src/app/models/auto
Pet.ts.j2  Tag.ts.j2  models.ts.j2

swagspotta then directly uses these templates for subsequent calls. This means you can edit the templates and add custom code (e.g. methods in the class, additional de/serialization, …, additional models, …) as long as the basic scaffold remains untouched.