sukovanej / effect-http

Declarative HTTP API library for effect-ts
https://sukovanej.github.io/effect-http
MIT License
247 stars 20 forks source link

using S.optional in request query leads to warning: Schema tag "UndefinedKeyword" is not supported for OpenAPI. #610

Closed adrian-gierakowski closed 1 month ago

adrian-gierakowski commented 3 months ago

minimal example:

import { NodeRuntime } from '@effect/platform-node'
import { Schema } from '@effect/schema'
import { pipe } from 'effect'
import { Api, ExampleServer, RouterBuilder } from 'effect-http'
import { NodeServer } from 'effect-http-node'

const Query = Schema.Struct({
  param: Schema.String,
  optionalParam: Schema.optional(Schema.String),
})

const api = Api.make().pipe(
  Api.addEndpoint(
    Api.get('hello', '/hello').pipe(
      Api.setRequestQuery(Query),
      Api.setRequestHeaders(Schema.Struct({ 'x-client-id': Schema.String })),
    ),
  ),
)

pipe(
  ExampleServer.make(api),
  RouterBuilder.build,
  NodeServer.listen({ port: 3000 }),
  NodeRuntime.runMain,
)

when ran, logs:

Schema tag "UndefinedKeyword" is not supported for OpenAPI.
timestamp=2024-06-26T10:12:33.967Z level=INFO fiber=#0 message="Listening on 0.0.0.0:3000"

removing optionalParam makes the first log line disappear

adrian-gierakowski commented 3 months ago

btw. Schema.optional seems to be ok when used in body

sukovanej commented 3 months ago

Try Schema.optional(..., { exact: true }), I believe this was already discussed in the past, the problem is that with Schema.optional you're also allowing the undefined value which is not representable in the JSON.

adrian-gierakowski commented 3 months ago

Thanks, that worked. Although I wonder why optional works for body though?

sukovanej commented 3 months ago

So in the end, the openapi logic is handling this case already (I honestly forgot about it) but it's not handled for query parameters / path parameters / headers. Fix prepared.

sukovanej commented 2 months ago

Hey, is it resolved for you?