Skip to content

fix(apiform): preserve float32 precision in comma arrays - #78

Open
sylvesterkaczmarek wants to merge 2 commits into
openai:mainfrom
sylvesterkaczmarek:fix/apiform-float32-comma
Open

fix(apiform): preserve float32 precision in comma arrays#78
sylvesterkaczmarek wants to merge 2 commits into
openai:mainfrom
sylvesterkaczmarek:fix/apiform-float32-comma

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown

Summary

Keep multipart comma-array float32 serialization consistent with the existing scalar float32 path.

Fixes #77.

Problem

internal/apiform already formats scalar float32 values with 32-bit precision, but FormatComma arrays combine float32 and float64 behind a 64-bit formatting call.

That means the same source value can be represented differently depending only on whether it appears by itself or inside a comma-delimited form field.

For example, current main serializes:

float32(0.1)

as:

0.1

but:

[]float32{0.1, 1.5}

with FormatComma as:

0.10000000149011612,1.5

Root cause

The primitive form path correctly distinguishes widths:

case reflect.Float32:
    strconv.FormatFloat(val.Float(), 'f', -1, 32)
case reflect.Float64:
    strconv.FormatFloat(val.Float(), 'f', -1, 64)

The comma-array path instead uses one branch for both kinds and always passes bitSize=64. Reflection returns floating-point values as float64, so the 64-bit formatting request preserves digits introduced by widening the original float32.

Fix

Split the comma-array branch by source kind:

  • reflect.Float32 -> FormatFloat(..., 32)
  • reflect.Float64 -> FormatFloat(..., 64)

No multipart boundaries, field names, array-format selection, integer formatting, boolean formatting, or float64 output changes.

Regression coverage

The existing table-driven form encoder test now includes:

  • a scalar float32(0.1) control showing the existing expected representation;
  • a FormatComma []float32{0.1, 1.5} regression requiring 0.1,1.5.

The comma-array case fails on current upstream main, while the scalar control demonstrates the consistency requirement directly.

Validation

The branch is based directly on upstream main at d082a010f7c6cacf407d8a1581446a7857f9f1bb and is not behind it.

Production diff: 3 additions and 1 deletion in internal/apiform/encoder.go, plus focused table-driven coverage in internal/apiform/form_test.go.

Full repository validation is left to the repository's GitHub Actions checks.

Risk

Low. Only the textual formatting of float32 values inside FormatComma arrays changes, and it becomes identical to the width-aware behavior already used for scalar float32 fields. float64 and all non-floating form values are unchanged.

@sylvesterkaczmarek
sylvesterkaczmarek requested a review from a team as a code owner August 18, 2026 10:50
@markstuart-oai

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 6, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-06T01:31:08.429378Z b686484 Manual request
🔒 Security Review Completed 2026-09-06T01:32:00.557393Z b686484 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: b686484b73

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Security review completed. No security issues were found in this pull request.

Reviewed commit: b686484b73

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

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.

Comma-form float32 arrays are serialized with widened precision

2 participants