Skip to content

[Fix][scala-sttp][circe] Circe codecs do not preserve original JSON field names for non-camelCase properties#23465

Merged
wing328 merged 8 commits intoOpenAPITools:masterfrom
nikhilsu:scala-sttp/fix-kebab-case-fields
Apr 9, 2026
Merged

[Fix][scala-sttp][circe] Circe codecs do not preserve original JSON field names for non-camelCase properties#23465
wing328 merged 8 commits intoOpenAPITools:masterfrom
nikhilsu:scala-sttp/fix-kebab-case-fields

Conversation

@nikhilsu
Copy link
Copy Markdown
Contributor

@nikhilsu nikhilsu commented Apr 7, 2026

Summary

This PR fixes: #23464

The scala-sttp generator with jsonLibrary=circe relies on circe's AutoDerivation to derive Encoder/Decoder instances. This derives codecs from Scala field names (camelCase), not the original JSON property names from the OpenAPI spec - breaking the wire contract for kebab-case, snake_case, and PascalCase properties.

This PR replaces AutoDerivation with explicit per-model Encoder/Decoder instances that use baseName (the original JSON property name), following the same pattern already used by the scala-http4s generator.

What changed

  • Replace AutoDerivation with explicit per-model Encoder/Decoder using baseName for JSON keys (following scala-http4s pattern)
  • Add missing circe codecs for File, Any, and URI in AdditionalTypeSerializers
  • Map type: object to io.circe.Json instead of Any for circe (fix was also needed in processOpts() since user config isn't available in the constructor)

Example

Given a spec with first-name, phone_number, ZipCode, lastName, address properties:

Before (broken): circe serializes using Scala field names

{"firstName": "John", "phoneNumber": "555-1234", "zipCode": "90210"}

After (fixed): circe serializes using original JSON names

{"first-name": "John", "phone_number": "555-1234", "ZipCode": "90210"}

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc @chameleon82 (Scala sttp), @flsh86 (Scala sttp4), @JennyLeahy (Scala http4s - this PR follows the http4s pattern)

How to validate

# Build and generate
./mvnw clean install -DskipTests -Dmaven.javadoc.skip=true
./bin/generate-samples.sh ./bin/configs/scala-sttp-circe.yaml

# Run tests
./mvnw test -pl modules/openapi-generator -Dtest=SttpCodegenTest

# Verify field names in generated sample
grep -n "downField\|baseName\|first-name\|phone_number\|ZipCode" \
  samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/model/PropertyNameMapping.scala

@nikhilsu nikhilsu marked this pull request as ready for review April 8, 2026 00:27
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 18 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/core/AdditionalTypeSerializers.scala">

<violation number="1" location="samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/core/AdditionalTypeSerializers.scala:28">
P2: `FileDecoder` registers every temp file with `deleteOnExit`, causing runtime accumulation of temp files and JVM exit-cleanup bookkeeping in long-lived processes.</violation>

<violation number="2" location="samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/core/AdditionalTypeSerializers.scala:44">
P2: AnyEncoder converts all non-Json `Any` values into JSON strings, corrupting numeric/boolean/object JSON types.</violation>
</file>

<file name="modules/openapi-generator/src/main/resources/scala-sttp/model.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/scala-sttp/model.mustache:44">
P1: Decoder template can generate invalid Scala for models with no properties due to an empty `for`-comprehension.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/yaml/user.yml">

<violation number="1">
P2: `userStatus` is typed as `integer` but its enum values are strings, creating an inconsistent schema contract.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@nikhilsu
Copy link
Copy Markdown
Contributor Author

nikhilsu commented Apr 8, 2026

@wing328 could you also review this. Thank you!

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 8, 2026

@nikhilsu
Copy link
Copy Markdown
Contributor Author

nikhilsu commented Apr 8, 2026

please follow step 3 to fix https://github.com/OpenAPITools/openapi-generator/actions/runs/24111536296/job/70440385683?pr=23465

Sorry missed regenerating samples after I incorporated a couple of cubic AI suggestions. It's updated now.

@nikhilsu nikhilsu force-pushed the scala-sttp/fix-kebab-case-fields branch from e19b737 to 09c7821 Compare April 8, 2026 19:09
@wing328 wing328 merged commit a79d60b into OpenAPITools:master Apr 9, 2026
23 checks passed
@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 9, 2026

thanks for the fix, which has been merged.

@nikhilsu
Copy link
Copy Markdown
Contributor Author

nikhilsu commented Apr 9, 2026

thanks for the fix, which has been merged.

Thank you. How often do you cut a release version?

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 9, 2026

monthly

https://github.com/OpenAPITools/openapi-generator/milestones

@nikhilsu
Copy link
Copy Markdown
Contributor Author

nikhilsu commented Apr 9, 2026

Do you think you can include this commit in 7.22.0?

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 9, 2026

yes as it's already merged

you can use the snapshot version (e.g. docker cli) for the time being

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][scala-sttp][circe] Circe codecs do not preserve original JSON field names for non-camelCase properties

2 participants