guardrail-dev / guardrail-dev/guardrail

http4s generator does not handle non-string headers

Open
#155 0 comments 0 reactions 0 assignees View on GitHub
bug good first issue help wanted scala-http4s
Dominant language
Scala
Stars
541
Forks
138
PR merge metrics
No merged PRs in 30d

Description

Related to #150

Currently, server headers are handled as:
```scala
req.headers.get("x-header".ci).map(_.value).map(Json.fromString(_).as[Boolean])
```
... resulting in attempting to parse the JSON literal `"false"` as a `Boolean`, which can never be true. This is related to a similar issue in the akka-http generator, where `X-Header: ""` will be silently converted to an empty string, as circe `Decoder`s are used similar to `parser.parse(str).orElse(Json.fromString(str)).flatMap(_.as[Boolean])` to attempt to decode all possible `x-scala-type` overrides.

I believe the most effective way to disambiguate this usecase is to alter [`headersToHttp4s`](https://github.com/twilio/guardrail/blob/a5d974d/modules/codegen/src/main/scala/com/twilio/guardrail/generators/Http4sServerGenerator.scala#L166-L193) (and likely [`formToHttp4s`](https://github.com/twilio/guardrail/blob/a5d974d/modules/codegen/src/main/scala/com/twilio/guardrail/generators/Http4sServerGenerator.scala#L208-L271), and any other place `Json.fromString` is used) by adding an additional parameter to each case in `directivesFromParams`, `baseType: Type.Name`, to explicitly handle _`String`_ decoders vs other JSON literal values.

Test swagger spec:
```yaml
swagger: '2.0'
paths:
/test:
post:
operationId: createFoo
parameters:
- in: header
name: x-header
type: boolean
required: true
- in: header
name: x-optional-header
type: boolean
required: false
responses:
200:
description: stub
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.