Skip to content

fix(templates): handle null parameters and nullable fields in test generation#6098

Merged
Fluf22 merged 6 commits into
fix/generatorsfrom
fix/templates
May 1, 2026
Merged

fix(templates): handle null parameters and nullable fields in test generation#6098
Fluf22 merged 6 commits into
fix/generatorsfrom
fix/templates

Conversation

@Fluf22
Copy link
Copy Markdown
Collaborator

@Fluf22 Fluf22 commented Mar 12, 2026

Jira: API-454

Summary

Extracted from #5860 — generic template fixes for null parameter handling across Go, Kotlin, Scala, and Swift.

Changes

  • Go: Use nameInPascalCase for optional With* methods; skip null optional params in tests; reformatted long calls
  • Kotlin: Handle null params in test request generation; handle nullable required fields (? = null)
  • Scala: Handle null params in test request generation
  • Swift: Emit nil for null values outside client tests

@Fluf22 Fluf22 requested a review from a team as a code owner March 12, 2026 13:15
@Fluf22 Fluf22 requested review from MarioAlexandruDan and morganleroi and removed request for a team March 12, 2026 13:15
@algolia-bot
Copy link
Copy Markdown
Collaborator

algolia-bot commented Mar 12, 2026

✔️ Code generated!

Name Link
🪓 Triggered by 40fc09b375100ab24caf0234d40ad0dc0e2a1115
🍃 Generated commit 090dfb664d466f4abbb7b8a149c6d8425e9a7678
🌲 Generated branch generated/fix/templates
📊 Benchmark results

Benchmarks performed on the method using a mock server, the results might not reflect the real-world performance.

Language Req/s
go 2967
swift 561
scala 23

@MarioAlexandruDan
Copy link
Copy Markdown
Contributor

MarioAlexandruDan commented Mar 24, 2026

Found this:

  • param_optional.mustache wraps null values in Some(...) instead of emitting None.
  • Why it this a problem: In Scala, Some(null) means "field is present with a null value" and serializes as "criticalThreshold": null in the JSON body. The correct behavior for an optional null field is None, which omits the field from the body entirely.
  • Location: templates/scala/tests/param_optional.mustache
  • Solution: Add a null check at the top — when isNull is true, short-circuit to None (optional) or null (required) instead of going through the Some(...) wrapping:
  {{#isNull}}{{^required}}None{{/required}}{{#required}}{{>                                                                                                                                                                                                                                                      
  tests/param_value}}{{/required}}{{/isNull}}{{^isNull}}{{^required}}{{^isHelper}}Some({{/isHelper}}{{#isHelper}}{{^hasDefaultValue}}Some({{/hasDefaultValue}}{{#hasDefaultValue}}{{^isRoot}}Some({{/isRoot}}{{/hasDefaultValue}}{{/isHelper}}{{/required}}{{>                                                   
  tests/param_value}}{{^required}}{{^isHelper}}){{/isHelper}}{{#isHelper}}{{^hasDefaultValue}}){{/hasDefaultValue}}{{#hasDefaultValue}}{{^isRoot}}){{/isRoot}}{{/hasDefaultValue}}{{/isHelper}}{{/required}}{{/isNull}}

@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented May 1, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

TIP This summary will be updated as you push new changes.

@Fluf22
Copy link
Copy Markdown
Collaborator Author

Fluf22 commented May 1, 2026

@MarioAlexandruDan the goal here is to be able to send a literal null value to the API, compared to just not sending the field, which is not the same behavior

@Fluf22 Fluf22 merged commit e7b66d0 into fix/generators May 1, 2026
19 of 30 checks passed
@Fluf22 Fluf22 deleted the fix/templates branch May 1, 2026 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants