marshmallow-code / apispec

A pluggable API specification generator. Currently supports the OpenAPI Specification (f.k.a. the Swagger specification)..

Home Page:https://apispec.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

spec schemas not generated from marshmallow_dataclass schemas and bottle

hutchisr opened this issue · comments

Spec generation doesn't seem to work for schemas created with marshmallow_dataclass:

@dataclass
class NetworkDevice:
    name: str
    hardware_address: str
    address: str
    netmask: str
    gateway: str

NetworkDeviceSchema = class_schema(NetworkDevice)

Then in my application (bottle, if that's relevant)

spec = APISpec(
    title="Admin API",
    version="2022",
    openapi_version="3.0.2",
    plugins=[BottlePlugin(), MarshmallowPlugin()],
)

app = Bottle(__name__)
# ...
@app.get("/network_config")
def get_network_config():
    """Get network device config
    ---
    get:
      responses:
        200:
          description: Get network device config
          content:
            application/json:
              schema: NetworkDeviceSchema
    """
    # ...
for route in app.routes:
    spec.path(app=app, view=route.callback)

No schema is generated in the resulting spec (eg from spec.to_yaml()). Other routes that use manually defined schemas work fine.

Manually registering it with

spec.components.schema("NetworkDevice", schema=NetworkDeviceSchema)

and referencing it in the comment as NetworkDevice does work, but from other issues I got the impression you should be able to have things registered with docstrings directly.

If you change schema: NetworkDeviceSchema to schema: NetworkDevice the schema will be registered properly.

Registering schemas via docstring yaml relies on the entries in marshmallow's schema registry. In this case the schema is registered as NetworkDevice instead of NetworkDeviceSchema.

Gotcha, thanks