Skip to content
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

Object query params (maps/dictionaries) support #53

Merged
merged 1 commit into from
Dec 8, 2022
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
7 changes: 3 additions & 4 deletions src/components/TryOut/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,16 @@ const FormItemTypesSwitch = ({ item, onChange, discriminator, ancestors, locatio
}
}
case FormItemKind.additionalProps: {
const dictionaryName = ancestors.pop();
return (
<div style={containerStyle}>
<JsonViewer
data={{}}
editable
hideButtons
setParsedJSON={jsonValue =>
setParsedJSON={jsonValue => {
onChange &&
onChange(dictionaryName, jsonValue, undefined, ancestors, item.in || location)
}
onChange(ancestors.pop(), jsonValue, undefined, ancestors, item.in || location);
}}
/>
</div>
);
Expand Down
40 changes: 30 additions & 10 deletions src/utils/tryout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,37 @@ const appendQueryParamsToPath = (path: string, queryParams: Record<string, strin
/**
*
* @returns equivalent params, with dictionary params converted to string params
* e.g. reqParam = {id: 3, severity:5} becomes reqParam=id%3D%3D3%26%26severity%3D%3D5 or reqParam=id==3&&severity==5
* by spreading them on root (or we could say collapse nested map/dictionary params)
* e.g.
* {
* someFilter: "location%3D%3DSingapore",
* reqParam: {
* filter: "age%3D%3D7",
* scope: true
* }
* }
* becomes
* {
* someFilter: "location%3D%3DSingapore",
* filter: "age%3D%3D7",
* scope: true
* }
*/
const formatQueryParams = params => {
const spreadNestedParams = params => {
if (isEmpty(params)) return params;
return Object.fromEntries(
Object.entries(params).map(([key, value]) => {
const isObjectParam = typeof value === 'object' && value !== null;
if (!isObjectParam) return [key, value];
return [key, entriesToQueryString(Object.entries(value as object))];
}),
);
const allParams = {};
Object.entries(params).forEach(([key, value]) => {
if (value === null) return;
const isObjectParam = typeof value === 'object';
if (isObjectParam) {
Object.entries(value as object)?.forEach(([nestedParamKey, nestedParamValue]) => {
allParams[nestedParamKey] = nestedParamValue;
});
} else {
allParams[key] = value;
}
});
return allParams;
};

export const appendParamsToPath = (
Expand All @@ -59,7 +79,7 @@ export const appendParamsToPath = (
queryParams: Record<string, string>,
): string => {
path = appendPathParamsToPath(path, pathParams);
path = appendQueryParamsToPath(path, formatQueryParams(queryParams));
path = appendQueryParamsToPath(path, spreadNestedParams(queryParams));
return path;
};

Expand Down