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

Deprecated flag is not configured correctly on query parameter objects

tsokalski opened this issue · comments

When using the metadata parameter on a Marshmallow field, the deprecated field is not properly configured on parameter objects with a location of "query" in the generated spec (the description field is set as expected, but not deprecated):

from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from marshmallow import Schema, fields


expected_result = """paths:
  /endpoint:
    get:
      parameters:
      - in: query
        name: bar
        description: A field called bar
        deprecated: true
        schema:
          type: string
        required: true
info:
  title: My API Spec
  version: 1.0.0
openapi: 3.1.0
"""

actual_result = """paths:
  /endpoint:
    get:
      parameters:
      - in: query
        name: bar
        description: A field called bar
        schema:
          type: string
          deprecated: true
        required: true
info:
  title: My API Spec
  version: 1.0.0
openapi: 3.1.0
"""


class TestSchema(Schema):
    bar = fields.Str(
        required=True,
        metadata={"description": "A field called bar", "deprecated": True},
    )


spec = APISpec(
    title="My API Spec",
    version="1.0.0",
    openapi_version="3.1.0",
    plugins=[MarshmallowPlugin()],
)
spec.path(
    "/endpoint",
    operations={"get": {"parameters": [{"in": "query", "schema": TestSchema}]}},
)

assert spec.to_yaml() == actual_result  # passes when it should fail
assert spec.to_yaml() == expected_result  # fails when it should pass

The deprecated field works correctly for request and response bodies, but not for query parameters. It seems to me that setting deprecated = True in the metadata on a field instance should mark it as deprecated on the parameter object itself and not the nested schema object, since the schema itself is not what is deprecated.

It looks like the OpenAPIConverter._field2parameter method might need updating to promote the deprecated flag from the nested schema object into the parent object, similar to how description is handled.