Skip to content

Conversation

nomi3
Copy link

@nomi3 nomi3 commented Jun 17, 2025

OpenAPI 3.x fully supports request bodies whose root schema is an array, but apispec + marshmallow currently cannot generate such schemas automatically. Projects must hand-write YAML/dict snippets for every endpoint that needs it.

This PR introduces an opt-in flag—list_as_array—to MarshmallowPlugin that lets the plugin emit type: array schemas automatically when:

  • a plain fields.List(...) is supplied as the schema, or
  • a Schema (class or instance) contains exactly one fields.List field with data_key=None.

The default behaviour remains unchanged (object-root), so the change is non-breaking.

@@ -108,6 +110,30 @@ def resolve_nested_schema(self, schema):

:param schema: schema to add to the spec
"""
if isinstance(schema, marshmallow.fields.List) and getattr(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use getattr? Isn't list_as_array always defined?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! I've removed the getattr calls and now use self.list_as_array directly.

@lafrech
Copy link
Member

lafrech commented Sep 3, 2025

Does it have to be optional?

I mean is there any reason to pass an array if this flag is not set?

If so, wouldn't that use case be broken if this flag is set to accommodate for a given resource?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants