Git Product home page Git Product logo

swagger-marshmallow-codegen's Introduction

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

#1

todo:

  • x-marshmallow-name

swagger-marshmallow-codegen's People

Contributors

cognifloyd avatar podhmo avatar wrouesnel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

swagger-marshmallow-codegen's Issues

nesting array type fields's options are ignored

definitions:
  xs:
    type: array
    items:
      type: string
      enum:
      - x
      - y
      - z
  member:
    type: object
    properties:
      name:
        type: string
      color:
        type: string
        enum:
          - r
          - g
          - b
      xs:
        type: array
        items:
          type: string
          enum:
          - x
          - y
          - z

currently

# -*- coding:utf-8 -*-
from marshmallow import (
    Schema,
    fields
)
from marshmallow.validate import OneOf


class Member(Schema):
    name = fields.String()
    color = fields.String(validate=[OneOf(choices=['r', 'g', 'b'], labels=[])])
    xs = fields.List(fields.String())

reboot

  • use github actions

  • fix broken tests

  • use marshmallow v3's function (e.g. additionalProperties)

    • UnknownField #62
    • NestedField using lambda #61
  • handling datetime field, correctly #52

  • support openAPI v3 #22

  • support python 3.9

  • update header

Integration with Connexion

I was looking for a way to generate schemas (for input and output) from the openapi file I use for connexion. Do you think that would be possible to have a nice integration? It would also be nice to be able to export to multiple files.

nested additionalProperties is not supported yet

e.g.

definitions:
  S:
    type: object
    properties:
      z:
        $ref: "#/definitions/D"
      z2: # ng
        additionalProperties:
          $ref: "#/definitions/D"
  P:
    type: integer
  P2:
    $ref: "#/definitions/P"
  D:
    additionalProperties:
      $ref: "#/definitions/P2"

What license is this?

I don't see a license in the swagger-marshmallow-codegen repo. Is it under the MIT license like marshmallow?

I would like to extract the Schema, Field and Validator classes into an independent library so that I can use them in generated code without depending on the full swagger-marshmallow-codegen.

(I'm planning to generate code with openapi-generator to generate a full client SDK based on uplink+marshmallow, supporting both swagger 2 and OpenAPI 3. And, yes, I will open source this work, submitting it upstream to openapi-generator).

additional properties support for array type is broken

I generated schema for Kubernetes from https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json

The generated schema has a TypeError for the following object.

    "io.k8s.api.authentication.v1beta1.UserInfo": {                                                                                                                                           
      "description": "UserInfo holds the information about the user needed to implement the user.Info interface.",                                                                            
      "properties": {                                                                                                                                                                         
        "extra": {                                                                                                                                                                            
          "additionalProperties": {                                                                                                                                                           
            "items": {                                                                                                                                                                        
              "type": "string"                                                                                                                                                                
            },                                                                                                                                                                                
            "type": "array"                                                                                                                                                                   
          },                                                                                                                                                                                  
          "description": "Any additional information provided by the authenticator.",                                                                                                         
          "type": "object"                                                                                                                                                                    
        },                                                                                                                                                                                    
        "groups": {                                                                                                                                                                           
          "description": "The names of groups this user is a part of.",                                                                                                                       
          "items": {                                                                                                                                                                          
            "type": "string"                                                                                                                                                                  
          },                                                                                                                                                                                  
          "type": "array"                                                                                                                                                                     
        },                                                                                                                                                                                    
        "uid": {                                                                                                                                                                              
          "description": "A unique value that identifies this user across time. If this user is deleted and another user by the same name is added, they will have different UIDs.",          
          "type": "string"                                                                                                                                                                    
        },                                                                                                                                                                                    
        "username": {                                                                                                                                                                         
          "description": "The name that uniquely identifies this user among all active users.",                                                                                               
          "type": "string"                                                                                                                                                                    
        }                                                                                                                                                                                     
      },                                                                                                                                                                                      
      "type": "object"                                                                                                                                                                        
    }, 
  File "/home/nsivanandam/w/scabbard/api/v1_0/edgemgr/edgemgr.py", line 7, in <module>
    from api.v1_0.kubernetes import Iok8sapicorev1NodeAddress
  File "/home/nsivanandam/w/scabbard/api/v1_0/kubernetes.py", line 1027, in <module>
    class Iok8sapiauthenticationv1UserInfoExtra(AdditionalPropertiesSchema):
  File "/home/nsivanandam/w/scabbard/api/v1_0/kubernetes.py", line 1030, in Iok8sapiauthenticationv1UserInfoExtra
    class Meta:
  File "/home/nsivanandam/w/scabbard/api/v1_0/kubernetes.py", line 1031, in Meta
    additional_field = fields.List(many=True)
TypeError: __init__() missing 1 required positional argument: 'cls_or_instance'

additionalProperties with ref is broken

definitions:
  S:
    type: object
    properties:
      x:
        additionalProperties:
          type: object
          properties:
            name:
              type: string
      y:
        additionalProperties:
          $ref: "#/definitions/O"
      z: # ng
        $ref: "#/definitions/D"
  O:
    type: object
    properties:
      name:
        type: string
  D:
    additionalProperties:
      type: object
      properties:
        name:
          type: string

snake_case attributes

Hello.

Just tried the code generation and it seems to work very well. I am building an ORMish client to a web API using marshmallow as the serialiser and I would not want to type that much.

But I am interfacing to a .Net API which uses CamelCase. I want my code to be as "pythonic" as possible so I am using snake_case attributes and then sets data_key to the actual field name. I couldn't find a add this functionality by just skimming the code.
If I would like to do this, where would be the point to add the function? There is no comments so it is a bit hard to get a grasp of the architecture.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.