← 输入框|github.com|刷新
Skip to content

OpenAPI describes a @single endpoint's response as an array #36

Done

Description

@mjurincic

Note: an AI coding agent (Claude Code) found this and wrote it up while building software on NpgsqlRest 4.1.0. Please check it before you rely on it.

  • Seen in a real project: every @single endpoint (functions that return a table, called with GET and POST) is answered with one object, the generated TS client types it as one object, and openapi.json describes it as an array.
  • Read in the source, not run: the cause below, on master.
  • Not run as written: the minimal repro below, which is adapted from those endpoints. The @void point at the end is a guess from reading the code.

What happens

A function that returns a set, annotated @single, answers with one JSON object, and the generated TypeScript client types it as one object. The OpenAPI document describes the same response as an array of objects, so clients generated from openapi.json expect the wrong shape.

Reproduce (NpgsqlRest 4.1.0)

create function api.register_thing()
returns table (thing_id uuid, name text)
language sql
begin atomic
    select gen_random_uuid(), 'a';
end;
comment on function api.register_thing() is '
@http POST /api/things
@single
Registers a thing.';
  • POST /api/things answers {"thingId": "…", "name": "a"}.
  • The TypeScript client returns Promise<ApiResult<IRegisterThingResponse>>, one object.
  • openapi.json says {"type": "array", "items": {"type": "object", …}}.

Cause

GetResponseSchema in plugins/NpgsqlRest.OpenApi/OpenApi.cs decides on routine.ReturnsSet alone and never sees the endpoint. plugins/NpgsqlRest.TsClient/TsClient.cs already checks returnsSet && !endpoint.ReturnSingleRecord.

Suggested fix

Pass the endpoint to GetResponseSchema, and give the array schema only when routine.ReturnsSet && !endpoint.ReturnSingleRecord. Otherwise fall through to the single-object branch.

The same file also decides whether there is a response body on endpoint.Routine.IsVoid alone, while the TS client uses routine.IsVoid || endpoint.Void. So a @void endpoint probably gets a response schema too. I haven't checked that one by running it.

Activity

  1. vbilopav commented on Dec 2, 2026

    @vbilopav
    Contributor

    Thanks for the report, and for the clear write-up. Reproduced and confirmed, both points.

    While checking this I compared the OpenAPI document with the real answers of every endpoint in the test suite, and the same kind of mismatch showed up in other places too (set of values, text/plain results, raw, table_format, bytea, cache invalidation endpoints, SQL files with several statements, and a few more). The generated TypeScript and Dart clients and the MCP tool schemas had some of their own.

    All of it is fixed and covered with tests, and will be in 4.1.1. I'll leave the issue open until that is released.

  2. vbilopav commented on Dec 2, 2026

    @vbilopav
    Contributor

    Released in 4.1.1.

    A @single endpoint is now described as one object in openapi.json, and the other mismatches between the described and the real response are fixed in the OpenAPI document, the generated TypeScript and Dart clients and the MCP tool schemas. The full list, two changes in responses (money values, the default content type of raw) and what still cannot be described exactly are in the changelog.

    After upgrading, regenerate the OpenAPI document and the clients. Thanks again for the report.

  3. mjurincic commented on Dec 2, 2026

    @mjurincic
    Author

    Thanks for the quick fix, and for going well beyond the case I reported. Upgraded to 4.1.1 and regenerated: all @single endpoints in our project are now described as one object in openapi.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions