swagger-marshmallow-codegen icon indicating copy to clipboard operation
swagger-marshmallow-codegen copied to clipboard

Ability to use as a Python module

Open eLvErDe opened this issue 4 years ago • 2 comments
trafficstars

Hello,

It would be nice it this generator cool be called from Python code instead of command line, I mean something like this:

import swagger_marshmallow_codegen

SPEC = """
openapi: 3.0.0
components:
  schemas:
    ExampleArray:
      description: Example array requiring at least one element
      type: array
      items:
        type: string
      minItems: 1
    ExampleSchema:
      type: object
      properties:
        array:
          $ref: "#/components/schemas/ExampleArray"
      required:
        - array
      additionalProperties: false

"""

models_str = swagger_marshmallow_codegen.from_string(SPEC)

Also would you consider exposing version in module's root ? It is quite common to be able to do this:

from swagger_marshmallow_codegen import __version__

print(__version__)
# Shows "0.6.3")

Best regards, Adam.

eLvErDe avatar Jan 22 '21 08:01 eLvErDe

Hmm, I'll add to my task queue.

podhmo avatar Jan 26 '21 11:01 podhmo

This is work-around

from dictknife import loading
from swagger_marshmallow_codegen.codegen.config import ConfigDict
from swagger_marshmallow_codegen.driver import Driver


def from_string(source: str, *, config: Optional[ConfigDict] = None) -> Dict[str, str]:

    config: ConfigDict = config or {
        "emit_schema": True,
        "emit_input": False,
        "emit_output": False,
        "additional_properties_default": False,
        "separated_output": False,
    }
    data = loading.loads(source, format="yaml")
    driver = Driver(config)
    modules = {name: str(m) for name, m in driver.transform(data).files}
    return modules

podhmo avatar Jan 26 '21 12:01 podhmo