Skip to content

fix(execute): keep primitive OAS3 form values as they were passed - #4310

Open
HawkForge wants to merge 1 commit into
swagger-api:mainfrom
HawkForge:fix/oas3-form-primitive-values
Open

HawkForge wants to merge 1 commit into
swagger-api:mainfrom
HawkForge:fix/oas3-form-primitive-values

Conversation

@HawkForge

Copy link
Copy Markdown

Description

buildRequest for OAS3 runs every application/x-www-form-urlencoded and multipart/form-data value through JSON.parse (added in #3482), so object fields that are passed as JSON text get decoded. The side effect is that string values which happen to be valid JSON primitives are converted to numbers/booleans first and then serialized back from that, which changes what is sent:

  • '1.0' is sent as 1, '5.0' as 5
  • '1234567890123456789' is sent as 1234567890123456800 (precision loss past Number.MAX_SAFE_INTEGER)

This change keeps the parsed value only when it is an object or an array. For primitives the original value is used, which serializes to the same text it was passed as. Object/array fields passed as JSON text behave as before.

Motivation and Context

Refs #4136, see the reproduction by @DenyVeyten in the comments there (thanks also to @warappa for the original report). The same root cause is reported on the Swagger UI side in swagger-api/swagger-ui#10385 by @tsxalchemist and @ezi-ocb (string field with 5.0, long numeric IDs).

The original report in #4136 (an object-typed field sent as JSON text being exploded by style: form) is not changed by this PR.

How Has This Been Tested?

Added tests in test/oas3/execute/build-request.js that build urlencoded and multipart requests with id: '1234567890123456789', version: '1.0' and an object field passed as JSON text. Both fail on main (id=1234567890123456800&version=1&...) and pass with the change.

npx cross-env BABEL_ENV=commonjs jest --runInBand --config ./config/jest/jest.unit.config.js test/oas3 test/execute test/http test/swagger2 test/bugs: 24 suites, 358 passed. ESLint and Prettier are clean on the changed files.

Screenshots (if appropriate):

Types of changes

  • No code changes (changes to documentation, CI, metadata, etc)
  • Dependency changes (any modification to dependencies in package.json)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Replaces #4292, which got closed when my fork was accidentally detached.

Form values of application/x-www-form-urlencoded and multipart/form-data
request bodies were run through JSON.parse, so string values that look
like JSON numbers were changed before serialization ('1.0' was sent as 1,
'1234567890123456789' lost precision). Only use the parsed value when it
is an object or an array.

Refs swagger-api#4136
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.

1 participant