Skip to main content

"generating marshmallow's schema from swagger definition file"

Project description

swagger-marshmallow-codegen Python package PyPi version Code style: black

concepts

  • Code generation is better than meta programming
  • Don't touch me(generated code) if you can

todo: write down detail.

examples

$ swagger-marshmallow-codegen definition.yaml > definition.py

definition.yaml

# todo: gentle example
definitions:
  default:
    properties:
      string:
        type: string
        default: "default"
      integer:
        type: integer
        default: 10
      boolean:
        type: boolean
        default: true
      date:
        type: string
        format: date
        default: 2000-01-01
      datetime:
        type: string
        format: date-time
        default: 2000-01-01T01:01:01Z
      object:
        type: object
        properties:
          name:
            type: string
            default: foo
          age:
            type: integer
            default: 20
        default:
          name: foo
          age: 20
      array:
        type: array
        items:
          type: integer
        default:
          - 1
          - 2
          - 3

  length-validation:
    type: object
    properties:
      s0:
        type: string
      s1:
        type: string
        maxLength: 10
      s2:
        type: string
        minLength: 5
      s3:
        type: string
        maxLength: 10
        minLength: 5

  maximum-validation:
    type: object
    properties:
      n0:
        type: number
        maximum: 100
      n1:
        type: number
        maximum: 100
        exclusiveMaximum: true
      n2:
        type: number
        maximum: 100
        exclusiveMaximum: false
      m0:
        type: number
        minimum: 100
      m1:
        type: number
        minimum: 100
        exclusiveMinimum: true
      m2:
        type: number
        minimum: 100
        exclusiveMinimum: false

  regex-validation:
    type: object
    properties:
      team:
        type: string
        pattern: team[1-9][0-9]+
      team2:
        type: string
        pattern: team[1-9][0-9]+
        maxLength: 10

  array-validation:
    type: object
    properties:
      nums:
        type: array
        items:
          type: integer
        maxItems: 10
        minItems: 1
        uniqueItems: true

  color:
    type: string
    enum:
      - R
      - G
      - B
  yen:
    type: integer
    enum:
      - 1
      - 5
      - 10
      - 50
      - 100
      - 500
      - 1000
      - 5000
      - 10000
  huge-yen:
    typ

definition.py

# -*- coding:utf-8 -*-
# this is auto-generated by swagger-marshmallow-codegen
from marshmallow import (
    Schema,
    fields
)
import datetime
from collections import OrderedDict
from marshmallow.validate import (
    Length,
    OneOf,
    Regexp
)
from swagger_marshmallow_codegen.validate import (
    ItemsRange,
    MultipleOf,
    Range,
    Unique
)
import re


class Default(Schema):
    string = fields.String(missing=lambda: 'default')
    integer = fields.Integer(missing=lambda: 10)
    boolean = fields.Boolean(missing=lambda: True)
    date = fields.Date(missing=lambda: datetime.date(2000, 1, 1))
    datetime = fields.DateTime(missing=lambda: datetime.datetime(2000, 1, 1, 1, 1, 1))
    object = fields.Nested('DefaultObject', missing=lambda: OrderedDict([('name', 'foo'), ('age', 20)]))
    array = fields.List(fields.Integer(), missing=lambda: [1, 2, 3])


class DefaultObject(Schema):
    name = fields.String(missing=lambda: 'foo')
    age = fields.Integer(missing=lambda: 20)


class Length_validation(Schema):
    s0 = fields.String()
    s1 = fields.String(validate=[Length(min=None, max=10, equal=None)])
    s2 = fields.String(validate=[Length(min=5, max=None, equal=None)])
    s3 = fields.String(validate=[Length(min=5, max=10, equal=None)])


class Maximum_validation(Schema):
    n0 = fields.Number(validate=[Range(min=None, max=100, min_inclusive=True, max_inclusive=True)])
    n1 = fields.Number(validate=[Range(min=None, max=100, min_inclusive=True, max_inclusive=False)])
    n2 = fields.Number(validate=[Range(min=None, max=100, min_inclusive=True, max_inclusive=True)])
    m0 = fields.Number(validate=[Range(min=100, max=None, min_inclusive=True, max_inclusive=True)])
    m1 = fields.Number(validate=[Range(min=100, max=None, min_inclusive=False, max_inclusive=True)])
    m2 = fields.Number(validate=[Range(min=100, max=None, min_inclusive=True, max_inclusive=True)])


class Regex_validation(Schema):
    team = fields.String(validate=[Regexp(regex=re.compile('team[1-9][0-9]+'))])
    team2 = fields.String(validate=[Length(min=None, max=10, equal=None), Regexp(regex=re.compile('team[1-9][0-9]+'))])


class Array_validation(Schema):
    nums = fields.List(fields.Integer(), validate=[ItemsRange(min=1, max=10, min_inclusive=True, max_inclusive=True), Unique()])


class Enum_validation(Schema):
    name = fields.String(required=True)
    money = fields.Integer(validate=[OneOf(choices=[1, 5, 10, 50, 100, 500, 1000, 5000, 10000], labels=[])])
    deposit = fields.Integer(validate=[MultipleOf(n=10000)])
    color = fields.String(required=True, validate=[OneOf(choices=['R', 'G', 'B'], labels=[])])

customization:

todo: write down

https://github.com/podhmo/swagger-marshmallow-codegen/issues/1

todo:

  • x-marshmallow-name

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swagger-marshmallow-codegen-0.6.4.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

swagger_marshmallow_codegen-0.6.4-py3-none-any.whl (33.7 kB view details)

Uploaded Python 3

File details

Details for the file swagger-marshmallow-codegen-0.6.4.tar.gz.

File metadata

  • Download URL: swagger-marshmallow-codegen-0.6.4.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.5

File hashes

Hashes for swagger-marshmallow-codegen-0.6.4.tar.gz
Algorithm Hash digest
SHA256 de2d74f8b4f043bc530e52bb39092e3338867e6d46d1811e5309349395b7ab28
MD5 427021d591f607349e182ac0fa123ed3
BLAKE2b-256 d0778b4ebf5b1e3cce0cef94286ba8db818eace1e1da1a958c10656f97911118

See more details on using hashes here.

File details

Details for the file swagger_marshmallow_codegen-0.6.4-py3-none-any.whl.

File metadata

  • Download URL: swagger_marshmallow_codegen-0.6.4-py3-none-any.whl
  • Upload date:
  • Size: 33.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.5

File hashes

Hashes for swagger_marshmallow_codegen-0.6.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4cc4844cd8f14d24c5ea342356e6a0dc1880a2fda992304a8c0107fdc0e7d617
MD5 306350a8126cd67ee9b1d7454531a791
BLAKE2b-256 8ed56732d8d33302188e28caef660de5149fd08dd305578cc75a95426b3b2dcd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page