OpenApi: optional query and header parameters are documented as nullable
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 57/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- api
Research direction
Start in OpenApi.ts at processParameters, the entry point named in the issue, and trace how optional query and header parameter schemas pass through the representation pipeline. Compare those parameter schemas with the body handling described in the issue, then inspect or add focused tests for OpenApi.fromApi query and header output. Done means optional parameters omit JSON null while preserving the documented string forms, with body behavior unchanged.
Written by the indexing model from the issue text.
Description
What version of Effect is running?
4.0.5. Also seen on 4.0.0. OpenApi.ts on main is the same as in 4.0.5.
What steps can reproduce the bug?
OpenApi.fromApi documents a query parameter declared with Schema.optional as anyOf: [T, null]. A query string has no way to send null, and the server's query decoder doesn't accept one.
import { Schema } from "effect"
import { HttpApi, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/http-api"
const Query = Schema.Struct({
limit: Schema.optional(Schema.FiniteFromString),
cursor: Schema.optional(Schema.NullOr(Schema.String))
})
const Api = HttpApi.make("api").add(
HttpApiGroup.make("items").add(
HttpApiEndpoint.get("list", "/items", { query: Query, success: Schema.String })
)
)
console.log(JSON.stringify(OpenApi.fromApi(Api).paths["/items"].get.parameters, null, 2))
// What the server does with the `null` the document allows
const decodeQuery = Schema.decodeUnknownExit(Schema.toCodecStringTree(Query))
console.log(decodeQuery({ limit: "null" })._tag) // "Failure"
What is the expected behavior?
limit documented as { "type": "string" } with required: false. required: false already says the parameter can be left out, so the undefined that Schema.optional adds has nothing left to describe.
cursor documented with only the string form of its NullOr, anyOf: [{ "type": "string", "enum": ["null"] }, { "type": "string" }], and no JSON null around it.
What do you see instead?
[
{
"name": "limit",
"in": "query",
"schema": { "anyOf": [{ "type": "string" }, { "type": "null" }] },
"required": false
},
{
"name": "cursor",
"in": "query",
"schema": {
"anyOf": [
{ "anyOf": [{ "type": "string", "enum": ["null"] }, { "type": "string" }] },
{ "type": "null" }
]
},
"required": false
}
]
cursor shows the mismatch most clearly. Its NullOr gets the query codec's representation, the string "null". The undefined from Schema.optional gets the JSON codec's, where Undefined encodes as null. One parameter ends up described by two codecs.
Additional information
- Headers have the same problem:
headers: Schema.Struct({ "x-trace": Schema.optional(Schema.String) })comes out asanyOf: [{ "type": "string" }, { "type": "null" }]. - Bodies are fine. A JSON body does decode
nulltoundefined, soT | nullis the right description there. - Effect's own
HttpApiClientis unaffected, since it dropsundefinedbefore building the URL. Clients generated from the document are the ones that break. We generate ours with orval, which typeslimitasstring | nulland sends?limit=nullwhen givennull. The service then rejects the request, or, for a plainSchema.String, reads"null"as a real value. - Switching to
Schema.optionalKeyfixes the document. It also makes{ limit: undefined }fail to encode, so every caller that builds a query from a possibly-undefined value has to leave the key out by hand, which is whatSchema.optionalexists to avoid. - A possible workaround until it's fixed: an
OpenApi.Transformthat drops thenullmember from every parameter whererequiredisfalse. - My guess at the cause, from reading the source:
processParametersqueues parameter schemas as"properties"ops, and those go through the same representation pipeline as bodies, so an optional property'sundefinedgets the JSON rule.
- Dominant language
- TypeScript
- Stars
- 16.7k
- Forks
- 808
- Avg merge
- 10h 12m
- Merged PRs (30d)
- 813
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Effect-TS/effect
-
BrowserWorkerRunner: port finalizer throws when the worker global has no close() (Bun)Possibly taken @santiago-ramos-02 claimed this 20 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Effect-TS/effect#8635 · 3 comments ·
Maintainers usually reply within 1 day
-
Support {self: this} for fnUntracedMay be free again @ArjunCodess claimed this 31 days ago, and no pull request is open. Openenhancement
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Effect-TS/effect#8101 · 1 comment ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 32/100
Effect-TS/effect#9610 · 2 comments ·
Maintainers usually reply within 1 day
-
Schema.isStringFinite (and Number string matching) takes quadratic time on long digit runsPossibly taken @magoz claimed this today. Open
Difficulty 3/5 1-2 days Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
Schema.isBase64 / isBase64Url throw RangeError on multi-megabyte stringsPossibly taken @magoz claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 20/100
Maintainers usually reply within 1 day
All issues in Effect-TS/effect
Similar issues
-
engine-rs P3 review-deferral wasm
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
maniator/verticopolis#1154 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
telerik/kendo-react#3861 ·
Maintainers usually reply within 1 day
-
[bug]: use-mobile.ts uses pixel logic, causing UI state de-sync with Tailwind CSS rem breakpointsOpenbug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
task
Difficulty 2/5 1-3 hours Newbie friendliness 72/100