[scala-sttp4] Fix enum operation parameters: type import and wire-value toString#24346
Open
vivekmahajan wants to merge 1 commit into
Open
[scala-sttp4] Fix enum operation parameters: type import and wire-value toString#24346vivekmahajan wants to merge 1 commit into
vivekmahajan wants to merge 1 commit into
Conversation
vivekmahajan
force-pushed
the
fix/scala-sttp4-enum-params
branch
from
July 19, 2026 08:53
1e4426e to
e316829
Compare
…ue toString
Enums used as operation parameters in the scala-sttp4 generator are broken
in two ways:
1. Generated code does not compile: the API imports enum types
Enumeration-style ('import pkg.MyEnum._'), inherited from scala-sttp
where 'type MyEnum = Value' lives inside the companion. scala-sttp4
enums are sealed traits + case objects, so the wildcard does not bring
the type into scope. Import the type itself instead; same-package model
imports need no import at all, so the enum special-case (and the now
unused enumRefs/isEnumClass/getEnumRefs helpers) is removed.
2. Path/query interpolation serializes the Scala identifier instead of the
wire value: uri"...${param}..." uses toString, so a request for
'sold-out' goes out as '/pets/SoldOut'. Override toString on each enum
case object with its wire value, matching scala-sttp's Enumeration
behavior where Value("sold-out").toString was the wire value. JSON
codecs are unaffected (circe and json4s both map values explicitly).
Covered by Sttp4CodegenTest#verifyEnumParameterImportAndWireValues; sttp4
petstore samples regenerated (jsoniter has its own codegen/templates and
is unaffected).
Made-With: pi
vivekmahajan
force-pushed
the
fix/scala-sttp4-enum-params
branch
from
July 19, 2026 09:42
e316829 to
dcd86c8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #24347.
Enums used as operation parameters in the
scala-sttp4generator are broken in two ways:1. Generated code does not compile. The API imports enum types Enumeration-style (
import pkg.MyEnum._), inherited from the sttp3 generator wheretype MyEnum = Valuelives inside the companion. scala-sttp4 enums are sealed traits + case objects, so the wildcard import does not bring the type into scope:Fixed by importing the type itself. Same-package model imports need no import at all for sealed traits, so that special case is removed along with the now-unused
enumRefs/isEnumClass/getEnumRefshelpers.2. Path/query interpolation serializes the Scala identifier, not the wire value.
uri"$baseUrl/pets/${status}"usestoString, so a request forsold-outgoes out as/pets/SoldOut. Fixed by overridingtoStringon each enum case object with its wire value, matching the sttp3 generator's behavior (Enumeration.Value("sold-out").toStringis the wire value there):JSON codecs are unaffected — both circe and json4s map enum values explicitly (visible in the regenerated samples).
Tests:
Sttp4CodegenTest#verifyEnumParameterImportAndWireValueswith a new spec fixture (sttp4-enum-param.yaml): a string enum (including a hyphenated value) used as a path parameter and as a model property. Asserts the plain type import in the API, no wildcard import, no same-package import in models, and wire-valuetoStringon case objects.Also verified end-to-end against a real FastAPI-generated spec (enum path param + discriminated
oneOfbodies): the generated client compiles and produces correct URLs, correct request bodies, and round-trips responses.scala-sttp4-jsoniteris unaffected (separate codegen class and templates); its samples are unchanged by regeneration.PR checklist
./bin/generate-samples.sh bin/configs/scala-sttp4*.yaml— sample changes are committed. No generator options changed, so no doc export changes.master.Summary by cubic
Fixes enum parameters in
scala-sttp4so generated clients compile and send wire values in URLs. APIs now import the enum type directly, and enum case objects stringify to their wire values.Bug Fixes
MyEnum._wildcard; no same-package model imports).toStringwith the wire value (path/query params serialize correctly, including nested...Enums).Refactors
scala-sttp4andscala-sttp4-circesamples.scala-sttp4-jsoniterunchanged.Written for commit dcd86c8. Summary will update on new commits.