Skip to content

bug: a 400 for a renamed query property names the decoded key in source.parameter, not the wire parameter #305

Closed

Description

@thomasfosterau

Found while fixing the review findings on #300 (Refs #282). This predates #300; the follow-up PR for it does not change it.

What's wrong

When a query schema renames a property on the wire, a 400 for a rejected value names the decoded key in source.parameter, not the wire parameter the client sent. JSON:API's source.parameter is "a string indicating which URI query parameter caused the error", so a client that reads it is pointed at a parameter that does not exist.

Middleware.schemaErrorDocument's sourceOf (src/Middleware.ts) builds source.parameter from the issue's path. The issue path is in decoded keys, because Schema.encodeKeys renames the struct in a transformation the error path does not undo.

Reproduction

On trunk bb1a07f, served with Middleware.SchemaErrorsLive:

const Person = Resource.make("people", { attributes: { name: Schema.String } })
const Post = Resource.make("posts", {
  attributes: { title: Schema.String },
  relationships: { author: Relationship.one(() => Person) },
})
// HttpApiEndpoint.get("posts", "/posts", {
//   query: Schema.Struct({ paths: Schema.optionalKey(Query.Include(Post)) }).pipe(
//     Schema.encodeKeys({ paths: "include" }),
//   ),
//   success: Schema.Unknown,
// }).middleware(Middleware.SchemaErrors)

GET /posts?include=author  → 200 {"paths":["author"]}
GET /posts?include=nope    → 400 {"errors":[{"status":"400","code":"bad_request","title":"Bad Request",
                              "detail":"Expected \"author\"","source":{"parameter":"paths"},
                              "meta":{"includablePaths":["author"]}}]}

Expected vs actual

  • Expected: "source": { "parameter": "include" }, the wire name the request used.
  • Actual: "source": { "parameter": "paths" }, the decoded property name.

The same applies to any query family renamed this way. Query.bracketKeys / Query.bracketPageKeys rename keys too (offset → page[offset]), so they are worth checking in the same pass.

Done when

  • A rejected value in a renamed query property names the wire parameter in source.parameter.
  • A test pins it for Schema.encodeKeys, and for Query.bracketPageKeys if that is affected too.

Generated by Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions