OpenAPITools / OpenAPITools/openapi-generator

[BUG] scala-http4s generator fails to handle non-String header parameter types

Open
#22,022 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Custom headers create type ambiguity in API clients if the header is not of type String.

Specifically this part

    "/personvern/personverninnstillinger/hentinnbyggerspiforpart/v2": {
      "post": {
        "tags": [
          "PersonverninnstillingerRead"
        ],
        "parameters": [
          {
            "name": "X-hn-isdebug",
            "in": "header",
            "schema": {
              "type": "Boolean"
            }
          }
        ],

which generates this code

  override def personvernPersonverninnstillingerHentinnbyggerspiforpartV2Post(
      xHnIsdebug: Option[Boolean] = None,
      hentInnbyggersPiV2Request: Option[HentInnbyggersPiV2Request] = None
  )(using auth: _Authorization.ApiKey): F[HentInnbyggersPiV2Response] =
    val requestHeaders = Seq(
      Some("Content-Type" -> "application/json-patch+json"),
      xHnIsdebug.map(x => "X-hn-isdebug" -> x)
    ).flatten

    _executeRequest[HentInnbyggersPiV2Request, HentInnbyggersPiV2Response](
      method = "POST",
      path = s"/personvern/personverninnstillinger/hentinnbyggerspiforpart/v2",
      body = hentInnbyggersPiV2Request,
      formParameters = None,
      queryParameters = Nil,
      requestHeaders = requestHeaders,
      auth = Some(auth)
    ) {
...

The compile issue then becomes

[error] 169 |      requestHeaders = requestHeaders,
[error]     |                       ^^^^^^^^^^^^^^
[error]     |          Found:    (requestHeaders : Seq[(String, String | Boolean)])
[error]     |          Required: Seq[(String, String)]
[error]     |
[error]     | longer explanation available when compiling with `-explain`
openapi-generator version

❯ openapi-generator-cli --version
openapi-generator-cli 7.15.0
commit : 2c816f8
built : -999999999-01-01T00:00:00+18:00
source : https://github.com/openapitools/openapi-generator
docs : https://openapi-generator.tech/

OpenAPI declaration file content or url

https://eksternapi.hn2.test.nhn.no/personvern/swagger/external/swagger.json

Generation Details

openapi-generator-cli generate
-i https://eksternapi.hn2.test.nhn.no/personvern/swagger/external/swagger.json
-g scala-http4s
-o modules/nhn-apis/src/main/scala/personvernkomponenten/client
--package-name no.hnikt.nmkp.nhnapis.personvernkomponenten
--api-package no.hnikt.nmkp.nhnapis.personvernkomponenten.api
--model-package no.hnikt.nmkp.nhnapis.personvernkomponenten.model
--invoker-package no.hnikt.nmkp.nhnapis.personvernkomponenten.client

Steps to reproduce

Run the CLI command above, try to compile, observe compilation error

Related issues/PRs

Could not find any related issues/PRs

Suggest a fix

The offending line here is really xHnIsdebug.map(x => "X-hn-isdebug" -> x) and the offending template file is https://github.com/OpenAPITools/openapi-generator/blob/67b24330c97290929626bf847d3b32fb9e76f824/modules/openapi-generator/src/main/resources/scala-http4s/headerParamCreation.mustache#L3

Initially I would assume we could just toString x here and that would be fine, this assumes that we will always be passing String/Double/Boolean here and not some more complex type? I don't know enough about what is possible to define here in OpenAPI. My knee-jerk reaction is always that toString is "dangerous". Another option would just to import cats.syntax.all.* and use x.show and rely on the Show typeclass (probably more idiomatic for this generator in any case.

If this seems like an easy enough approach to fixing it I can open a PR

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/scala-http4s/headerParamCreation.mustache at the referenced line, then reproduce the issue using the provided OpenAPI declaration and generator CLI command. Verify the generated scala-http4s client handles the Boolean header without producing a Seq[(String, String | Boolean)] compilation error.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.