Skip to content

Fix OpenAPI array and object parameter serialization - #7

Merged
andig merged 3 commits into
evcc-io:mainfrom
antgamdia:param-serialization
Jul 1, 2026
Merged

Fix OpenAPI array and object parameter serialization#7
andig merged 3 commits into
evcc-io:mainfrom
antgamdia:param-serialization

Conversation

@antgamdia

Copy link
Copy Markdown

Description

This PR fixes the incorrect serialization of array and object parameters in OpenAPI operations by taking into account the style and explode fields. I've tried to follow https://spec.openapis.org/oas/latest.html#parameter-object

Let's assume we have an operation accepting a query parameter (e.g., severity=["debug", "info", "warning", "critical"]), the MCP server was incorrectly serializing them using Go's default string formatting, resulting in malformed URLs:

Before (incorrect):
GET /foo?severity=%5Bdebug+info+warning+critical%5D, that is, GET/foo?severity=[debug+info+warning+critical], URL-decoded.

After (correct):
GET /foo?severity=debug&severity=info&severity=warning&severity=critical

Note: The default serialization method is style: form and explode: true. Source,

The code was using fmt.Sprintf("%v", val) to convert parameter values to strings, which works for primitives but produces Go's string representation for arrays ([item1 item2 item3]) and objects (map[key:value]). This doesn't match any valid OpenAPI serialization format.

The main changes of this PR are:

  • Refactored formatParameterValue() to accept schema instead of boolean flag
  • Added serializeParameter() - core serialization logic for all locations (path, query, header, cookie) and styles (simple, label, matrix, form, spaceDelimited, pipeDelimited, deepObject) whether it be with explode set as true or false.
  • Added style-specific wrappers: serializeQueryParameter(), serializePathParameter(), serializeHeaderParameter(), serializeCookieParameter()
  • Added TestSerializeParameter() with comprehensive test cases based on Swagger documentation examples

antgamdia added 2 commits May 25, 2026 17:48
Signed-off-by: Antonio Gamez Diaz <antonio.gamez@suse.com>
Signed-off-by: Antonio Gamez Diaz <antonio.gamez@suse.com>
@andig

andig commented Jul 1, 2026

Copy link
Copy Markdown
Member

Sorry, this PR has escaped my attention. Much appreciated, thank you!

Fold the redundant simple-style cases into the trailing default (both
comma-join), reuse buildParts() for the matrix object branches, and
replace the escapeFn func parameter with an escapePath bool. Behavior
unchanged; TestSerializeParameter passes as-is.
@andig
andig merged commit fd72c5a into evcc-io:main Jul 1, 2026
1 check passed
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