Skip to content

Large integers inside style=deepObject struct mashaled as floats #28

Open
@mgabeler-lee-6rs

Description

@mgabeler-lee-6rs

Given a parameter like this:

        - name: foo
          in: query
          style: deepObject
          schema:
            type: object
            properties:
              min:
                type: integer
                format: int64
              max:
                type: integer
                format: int64

The generated client code:

  1. Marshals the struct to JSON
  2. Parses the JSON back as a map[string]any, resulting in the numbers being converted to float64 -- already not great her I think
  3. Constructs the query parameter values using fmt.Sprintf("=%v, ...) resulting in the integer parameter using floating point "e" notation if it's greater than ~ 100000
  4. The generated server then refuses to parse values like 1.23454678e9 as not valid string representations of integers

Item 2 happens in MarshalDeepObject:

err = json.Unmarshal(buf, &i2)

Item 3 happens just above in marshalDeepObject:

prefix + fmt.Sprintf("=%v", t),

I think that, if it has to go through the unfortunate JSON loop, it should use a json.Decoder with UseNumber() applied so that numbers are retained at full fidelity. This is what styleStruct does for other styles:

runtime/styleparam.go

Lines 235 to 236 in 35e8035

e := json.NewDecoder(bytes.NewReader(buf))
e.UseNumber()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions