Skip to content

Commit

Permalink
Set unparsable data to a string in JSON output (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
verhovsky committed Jan 4, 2024
1 parent e52e342 commit 112ae7a
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/curl/opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ export interface OperationConfig {
// TODO: remove any.
// This is difficult because we have curl's arguments but also a couple
// curlconverter-specific arguments that are handled by the same code.
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
[key: string]: any;
}
// type Satisfies<T, U extends T> = void;
// type AssertSubsetKeys = Satisfies<
Expand Down
59 changes: 27 additions & 32 deletions src/generators/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type JSONOutput = {
headers?: { [key: string]: string | null };
queries?: { [key: string]: string | string[] };
// `| any` because of JSON
data?: { [key: string]: string } | any; // eslint-disable-line @typescript-eslint/no-explicit-any
data?: { [key: string]: string } | string | any;
// raw_data?: string[],
files?: { [key: string]: string };
// raw_files: string[],
Expand All @@ -64,48 +64,42 @@ type JSONOutput = {
connect_timeout?: number;
};

function getDataString(request: Request): {
data?: { [key: string]: string | string[] };
} {
function getDataString(
request: Request
): { [key: string]: string | string[] } | string {
if (!request.data) {
return {};
}

const contentType = request.headers.getContentType();
if (contentType === "application/json") {
try {
const json = JSON.parse(request.data.toString());
return { data: json };
return JSON.parse(request.data.toString());
} catch (e) {}
}

const [parsedQuery, parsedQueryDict] = parseQueryString(request.data);
if (!parsedQuery || !parsedQuery.length) {
// TODO: this is not a good API
return {
data: {
[request.data.toString()]: "",
},
};
}
if (parsedQueryDict) {
const data = Object.fromEntries(
parsedQueryDict.map((param) => [
param[0].toString(),
Array.isArray(param[1])
? param[1].map((v) => v.toString())
: param[1].toString(),
])
);
return { data };
} else {
return {
if (contentType === "application/x-www-form-urlencoded") {
const [parsedQuery, parsedQueryDict] = parseQueryString(request.data);
if (parsedQueryDict) {
return Object.fromEntries(
parsedQueryDict.map((param) => [
param[0].toString(),
Array.isArray(param[1])
? param[1].map((v) => v.toString())
: param[1].toString(),
])
);
}
if (parsedQuery) {
// .fromEntries() means we lose data when there are repeated keys
data: Object.fromEntries(
return Object.fromEntries(
parsedQuery.map((param) => [param[0].toString(), param[1].toString()])
),
};
);
}
}

// TODO: this leads to ambiguity with JSON strings
return request.data.toString();
}

function getFilesString(
Expand Down Expand Up @@ -190,17 +184,18 @@ export function _toJsonString(
);
}

// TODO: not Object.assign, doesn't work with type system
if (request.data) {
Object.assign(requestJson, getDataString(request));
requestJson.data = getDataString(request);
} else if (request.multipartUploads) {
// TODO: not Object.assign, doesn't work with type system
Object.assign(requestJson, getFilesString(request));
}

if (request.compressed) {
requestJson.compressed = true;
}
if (request.insecure) {
// TODO: rename to verify? insecure=false doesn't make sense
requestJson.insecure = false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/generators/julia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function formatData(request: Request, imports: Set<string>): [string, string] {
try {
const jsonData = jsonParseLossless(
request.dataArray[0].toString()
) as any; // eslint-disable-line @typescript-eslint/no-explicit-any
) as any;
const result = jsonAsJulia(jsonData);
imports.add("JSON");
return [result, "JSON.json(body)"];
Expand Down
5 changes: 0 additions & 5 deletions src/generators/lua.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import { reprStr as pyreprStr } from "./python/python.js";

const supportedArgs = new Set([
...COMMON_SUPPORTED_ARGS,
// "insecure",
// "no-insecure",
// "compressed",
// "no-compressed",
// "max-time",
// "form",
// "form-string",
]);
Expand Down
4 changes: 1 addition & 3 deletions test/fixtures/json/j_patch_array.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions test/fixtures/json/multiple_d_post.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions test/fixtures/json/post_with_data_binary.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions test/fixtures/json/post_with_data_raw.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions test/fixtures/json/post_xpost.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 112ae7a

Please sign in to comment.