Skip to content

[Runtime] Respect Accept header quality values when detecting JSON query requests #736

Description

@cssbruno

Priority

Medium — HTTP content negotiation correctness for generated query routes.

Context

runtime/app/backend.go routes contract query requests when either X-GOWDK-Query is truthy or the request Accept header appears to accept JSON.

The helper currently detects JSON with:

for _, part := range strings.Split(header, ",") {
    mediaType := strings.ToLower(strings.TrimSpace(strings.Split(part, ";")[0]))
    if mediaType == "application/json" || strings.HasSuffix(mediaType, "+json") {
        return true
    }
}

Problem

The parser ignores q quality values and other structured Accept-header semantics.

Examples:

Accept: application/json;q=0, text/html

This explicitly says JSON is unacceptable, but the current helper still returns true because it only checks the media type before ;.

Consequences:

  1. A request can be routed as a query request even when JSON has q=0.
  2. Browser/tool content negotiation can behave unexpectedly.
  3. Future response negotiation is harder because the current helper is not a real Accept parser.
  4. Tests may encode an overly permissive contract.

Proposed direction

Use a small structured Accept parser for generated-runtime routing decisions.

The helper should at minimum:

  • split media ranges safely;
  • parse parameters;
  • treat missing q as 1;
  • treat invalid q values consistently;
  • ignore JSON media ranges with q=0;
  • support application/json and application/*+json.

Acceptance criteria

  • Accept: application/json still marks a query request.
  • Accept: application/problem+json or another +json media type still marks a query request.
  • Accept: application/json;q=0, text/html does not mark a query request unless X-GOWDK-Query is set.
  • Invalid Accept headers have deterministic behavior documented in tests.
  • Tests cover multiple header values, whitespace, uppercase media types, q-values, and wildcard behavior if supported.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions