Skip to content

feat(ktor-server): skip internal ContentNegotiation install when already configured#2174

Merged
samuelAndalon merged 2 commits into
ExpediaGroup:masterfrom
gaurav0107:fix/2025-add-option-to-skip-internal-contentnegot
May 18, 2026
Merged

feat(ktor-server): skip internal ContentNegotiation install when already configured#2174
samuelAndalon merged 2 commits into
ExpediaGroup:masterfrom
gaurav0107:fix/2025-add-option-to-skip-internal-contentnegot

Conversation

@gaurav0107
Copy link
Copy Markdown
Contributor

📝 Description

Route.graphQLGetRoute() and Route.graphQLPostRoute() unconditionally install ContentNegotiation on the route scope. When the host application has already installed ContentNegotiation at the application level — a common pattern when the app serves other JSON endpoints or uses StatusPages with JSON error responses — Ktor throws DuplicatePluginException:

Installing RouteScopedPlugin to application and route is not supported.
Consider moving application level install to routing root.

This PR makes the internal install conditional on application.pluginOrNull(ContentNegotiation):

  • If ContentNegotiation is already installed at the application level, the route-scoped install is skipped and the existing global configuration is reused.
  • If no global install is present, behaviour is unchanged: the route-scoped install proceeds exactly as today, honouring streamingResponse and jacksonConfiguration.

When the global install wins, streamingResponse and jacksonConfiguration are not applied, since the application-level configuration takes precedence; this trade-off is documented in the KDoc for both routes.

No API change; fully backward compatible.

Added two regression tests in GraphQLPluginTest covering the globally-installed case for both the GET and POST routes.

🔗 Related Issues

Fixes #2025

…ady configured

When the host application has already installed ContentNegotiation at the
application level — a common pattern when the app serves other JSON endpoints
or uses StatusPages with JSON error responses — the existing unconditional
route-scoped install in graphQLGetRoute and graphQLPostRoute caused Ktor to
throw DuplicatePluginException.

The install is now guarded by application.pluginOrNull(ContentNegotiation):
if the plugin is present at the application level, the route-scoped install
is skipped and the existing configuration is reused. When no global install
exists, behaviour is unchanged.

Added regression tests for both route helpers.

Fixes ExpediaGroup#2025
@gaurav0107 gaurav0107 marked this pull request as ready for review May 14, 2026 19:53
@samuelAndalon samuelAndalon self-requested a review May 18, 2026 15:12
@samuelAndalon samuelAndalon merged commit f9e0ea4 into ExpediaGroup:master May 18, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Add Option to Skip Internal ContentNegotiation Installation in graphQLPostRoute()

2 participants