[Backport release-2.3] xrd: Opportunistically infer integer types when generating from an XR#112
Merged
Merged
Conversation
When generating an XRD from an example XR, we have to infer the type of each field from the example input. We do this by unmarshalling into a `map[string]any` and then observing the actual type of each value. Since the JSON specification has only a single numerical type, which is floating point, `json.Unmarshal` treats all numbers as floats. This means a field like `replicas: 3` in an example XR produces a `number` field in the OpenAPI spec when an `integer` field would be more appropriate. Detect integers by comparing the truncation of the value to the original value. This is an imperfect heuristic since not all integers are representable as floats, but it will work for common cases (small integers) and produce less surprising behavior for users. Signed-off-by: Adam Wolfe Gordon <awg@upbound.io> (cherry picked from commit 24976cd)
If an array in an example XR contains a mix of integers and floats, use "number" as the array's element type rather than returning an error. Signed-off-by: Adam Wolfe Gordon <awg@upbound.io> (cherry picked from commit 25a5e5a)
4 tasks
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.
Description
Backport of #108 to
release-2.3.