-
Notifications
You must be signed in to change notification settings - Fork 17
fix: query string is incorrectly serialized for primitive parameters (#103) #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { getParameters as createGetParameters } from '../operation-object'; | ||
import { constant } from 'fp-ts/lib/function'; | ||
import { left } from 'fp-ts/lib/Either'; | ||
import { fromString } from '../../../../../utils/ref'; | ||
import { PathItemObjectCodec } from '../../../../../schema/3.0/path-item-object'; | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { either, option } from 'fp-ts'; | ||
import { OperationObjectCodec } from '../../../../../schema/3.0/operation-object'; | ||
import { sequenceTEither } from '@devexperts/utils/dist/adt/either.utils'; | ||
|
||
describe('OperationObject', () => { | ||
describe('getParameters', () => { | ||
const getParameters = createGetParameters({ | ||
resolveRef: constant(left(new Error('Refs not supported'))), | ||
}); | ||
|
||
it('should correctly handle primitive query parameters', () => { | ||
const operation = pipe( | ||
OperationObjectCodec.decode({ | ||
responses: {}, | ||
parameters: [ | ||
{ | ||
in: 'query', | ||
name: 'offset', | ||
required: false, | ||
schema: { | ||
type: 'number', | ||
}, | ||
}, | ||
{ | ||
in: 'query', | ||
name: 'limit', | ||
required: true, | ||
schema: { | ||
type: 'number', | ||
}, | ||
}, | ||
], | ||
}), | ||
either.mapLeft(constant(new Error())), | ||
); | ||
|
||
const pathItem = pipe(PathItemObjectCodec.decode({}), either.mapLeft(constant(new Error()))); | ||
|
||
const result = pipe( | ||
sequenceTEither(fromString('#/test'), operation, pathItem), | ||
either.chain(([ref, operation, pathItem]) => getParameters(ref, operation, pathItem)), | ||
); | ||
|
||
const generated = pipe( | ||
result, | ||
option.fromEither, | ||
option.chain(result => result.serializedQueryString), | ||
option.fold(constant(''), fragment => fragment.value.replace(/\s+/g, ' ')), | ||
); | ||
|
||
expect(generated).toEqual( | ||
`compact([pipe( | ||
optionFromNullable(number).encode(parameters.query.offset), | ||
option.fromNullable, | ||
option.chain(value => fromEither(serializePrimitiveParameter('form', 'offset', value))), | ||
),pipe( | ||
number.encode(parameters.query.limit), | ||
value => fromEither(serializePrimitiveParameter('form', 'limit', value)), | ||
)]).join('&')` | ||
.trim() | ||
.replace(/\s+/g, ' '), | ||
); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,9 @@ export function combineFragmentsK( | |
return serializedFragment(fragment.value, dependencies.concat(fragment.dependencies), refs.concat(fragment.refs)); | ||
} | ||
|
||
/** | ||
* @param f returns an Option | ||
*/ | ||
export const getSerializedOptionCallFragment = ( | ||
nullable: boolean, | ||
f: SerializedFragment, | ||
|
@@ -136,12 +139,19 @@ export const getSerializedOptionCallFragment = ( | |
`pipe( | ||
${a}, | ||
option.fromNullable, | ||
option.map(${fn}), | ||
option.chain(${fn}), | ||
)`, | ||
[serializedDependency('option', 'fp-ts'), serializedDependency('pipe', 'fp-ts/lib/pipeable')], | ||
[], | ||
) | ||
: serializedFragment(`some((${fn})(${a}))`, [serializedDependency('some', 'fp-ts/lib/Option')], []), | ||
: serializedFragment( | ||
`pipe( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: is it really necessary to change direct function invocation to pipe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's skip |
||
${a}, | ||
${fn}, | ||
)`, | ||
[serializedDependency('pipe', 'fp-ts/lib/pipeable')], | ||
[], | ||
), | ||
); | ||
|
||
export const commaFragment = serializedFragment(', ', [], []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that you add
some
in allswitch
branches, would it make more sense to keep this function simpler and operate on simple types and wrap its result insome
on the call site?