Skip to content

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/language/typescript/2.0/serializers/operation-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ export const serializeQueryParameterObject = (
case 'number':
case 'boolean': {
const f = serializedFragment(
`value => encodeURIComponent('${parameter.name}') + '=' + encodeURIComponent(value)`,
[],
`value => some(encodeURIComponent('${parameter.name}') + '=' + encodeURIComponent(value))`,
Copy link
Contributor

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 all switch branches, would it make more sense to keep this function simpler and operate on simple types and wrap its result in some on the call site?

[serializedDependency('some', 'fp-ts/lib/Option')],
[],
);
return right(getSerializedOptionCallFragment(!required, f, encoded));
Expand All @@ -432,16 +432,16 @@ export const serializeQueryParameterObject = (
case 'pipes': {
const s = getCollectionSeparator(collectionFormat);
const f = serializedFragment(
`value => encodeURIComponent('${parameter.name}') + '=' + encodeURIComponent(value.join('${s}'))`,
[],
`value => some(encodeURIComponent('${parameter.name}') + '=' + encodeURIComponent(value.join('${s}')))`,
[serializedDependency('some', 'fp-ts/lib/Option')],
[],
);
return right(getSerializedOptionCallFragment(!required, f, encoded));
}
case 'multi': {
const f = serializedFragment(
`value => value.map(item => encodeURIComponent('${parameter.name}') + '=' + encodeURIComponent(item)).join('&')`,
[],
`value => some(value.map(item => encodeURIComponent('${parameter.name}') + '=' + encodeURIComponent(item)).join('&'))`,
[serializedDependency('some', 'fp-ts/lib/Option')],
[],
);
return right(getSerializedOptionCallFragment(!required, f, encoded));
Expand Down
8 changes: 4 additions & 4 deletions src/language/typescript/3.0/bundled/openapi-3-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ const content = `
export const serializePrimitiveParameter = (style: string, name: string, value: unknown): Either<Error, string> => {
switch (style) {
case 'matrix': {
return right(\`;\${name}=\${value}\`);
return right(\`;\${name}=\${encodeURIComponent(String(value))}\`);
}
case 'label': {
return right(\`.\${value}\`);
return right(\`.\${encodeURIComponent(String(value))}\`);
}
case 'form': {
return right(\`\${name}=\${value}\`);
return right(\`\${name}=\${encodeURIComponent(String(value))}\`);
}
case 'simple': {
return right(\`\${value}\`);
return right(\`\${encodeURIComponent(String(value))}\`);
}
}
return left(new Error(\`Unsupported style "\${style}" for parameter "\${name}"\`));
Expand Down
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, ' '),
);
});
});
});
8 changes: 4 additions & 4 deletions src/language/typescript/3.0/serializers/operation-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getParameterObjectSchema,
isRequired,
serializeParameterObject,
serializeParameterToTemplate,
serializeQueryParameterToTemplate,
} from './parameter-object';
import { pipe } from 'fp-ts/lib/pipeable';
import { getSerializedKindDependency, serializedDependency } from '../../common/data/serialized-dependency';
Expand Down Expand Up @@ -70,7 +70,7 @@ const eqParameterByNameAndIn: Eq<ParameterObject> = getStructEq({
});
const contains = array.elem(eqParameterByNameAndIn);

const getParameters = combineReader(
export const getParameters = combineReader(
ask<ResolveRefContext>(),
e => (from: Ref, operation: OperationObject, pathItem: PathItemObject): Either<Error, Parameters> => {
const processedParameters: ParameterObject[] = [];
Expand Down Expand Up @@ -142,7 +142,7 @@ const getParameters = combineReader(
return resolvedSchema;
}

const queryStringFragment = serializeParameterToTemplate(
const queryStringFragment = serializeQueryParameterToTemplate(
from,
resolved.right,
resolvedSchema.right,
Expand Down Expand Up @@ -209,7 +209,7 @@ const getParameters = combineReader(
option.map(f =>
combineFragmentsK(f, c =>
serializedFragment(
`encodeURIComponent(compact([${c}]).join('&'))`,
`compact([${c}]).join('&')`,
[serializedDependency('compact', 'fp-ts/lib/Array')],
[],
),
Expand Down
23 changes: 16 additions & 7 deletions src/language/typescript/3.0/serializers/parameter-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const getParameterExplode = (parameter: ParameterObject): boolean =>
option.getOrElse(() => getParameterObjectStyle(parameter) === 'form'),
);

export const serializeParameterToTemplate = (
export const serializeQueryParameterToTemplate = (
from: Ref,
parameter: ParameterObject,
parameterSchema: SchemaObject,
Expand Down Expand Up @@ -121,26 +121,35 @@ const getFn = (
if (PrimitiveSchemaObjectCodec.is(schema)) {
return right(
serializedFragment(
`value => serializePrimitiveParameter('${style}', '${parameter.name}', value)`,
[serializedDependency('serializePrimitiveParameter', pathToUtils)],
`value => fromEither(serializePrimitiveParameter('${style}', '${parameter.name}', value))`,
[
serializedDependency('fromEither', 'fp-ts/lib/Option'),
serializedDependency('serializePrimitiveParameter', pathToUtils),
],
[],
),
);
}
if (ArraySchemaObjectCodec.is(schema)) {
return right(
serializedFragment(
`value => serializeArrayParameter('${style}', '${parameter.name}', value, ${explode})`,
[serializedDependency('serializeArrayParameter', pathToUtils)],
`value => fromEither(serializeArrayParameter('${style}', '${parameter.name}', value, ${explode}))`,
[
serializedDependency('fromEither', 'fp-ts/lib/Option'),
serializedDependency('serializeArrayParameter', pathToUtils),
],
[],
),
);
}
if (ObjectSchemaObjectCodec.is(schema)) {
return right(
serializedFragment(
`value => serializeObjectParameter('${style}', '${parameter.name}', value, ${explode})`,
[serializedDependency('serializeObjectParameter', pathToUtils)],
`value => fromEither(serializeObjectParameter('${style}', '${parameter.name}', value, ${explode}))`,
[
serializedDependency('fromEither', 'fp-ts/lib/Option'),
serializedDependency('serializeObjectParameter', pathToUtils),
],
[],
),
);
Expand Down
14 changes: 12 additions & 2 deletions src/language/typescript/common/data/serialized-fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: is it really necessary to change direct function invocation to pipe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a matter of readability; direct invocation turns out less readable even after applying prettier. Shall I switch it back to the direct call?
image

Copy link
Contributor

Choose a reason for hiding this comment

The 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(', ', [], []);