fix: wrong named param check for js client (#8820)

* fix: wrong named param check for js client

* rearrange type imports

* add changeset

* fix: workaround for undefined endpoint_info

---------

Co-authored-by: Hannah <hannahblair@users.noreply.github.com>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
JacobLinCool 2024-07-23 02:57:22 +08:00 committed by GitHub
parent 2f630abf53
commit 5050b36221
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 15 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/client": patch
"gradio": patch
---
fix:fix: wrong named param check for js client

View File

@ -1,11 +1,17 @@
import type { Status } from "../types";
import {
HOST_URL,
INVALID_URL_MSG,
QUEUE_FULL_MSG,
SPACE_METADATA_ERROR_MSG
} from "../constants";
import type { ApiData, ApiInfo, Config, JsApiData } from "../types";
import type {
ApiData,
ApiInfo,
Config,
JsApiData,
EndpointInfo,
Status
} from "../types";
import { determine_protocol } from "./init_helpers";
export const RE_SPACE_NAME = /^[a-zA-Z0-9_\-\.]+\/[a-zA-Z0-9_\-\.]+$/;
@ -384,11 +390,11 @@ export function handle_message(
export const map_data_to_params = (
data: unknown[] | Record<string, unknown> = [],
api_info: ApiInfo<JsApiData | ApiData>
endpoint_info: EndpointInfo<JsApiData | ApiData>
): unknown[] => {
const parameters = Object.values(api_info.named_endpoints).flatMap(
(values) => values.parameters
);
// Workaround for the case where the endpoint_info is undefined
// See https://github.com/gradio-app/gradio/pull/8820#issuecomment-2237381761
const parameters = endpoint_info ? endpoint_info.parameters : [];
if (Array.isArray(data)) {
if (data.length > parameters.length) {

View File

@ -573,31 +573,33 @@ describe("map_data_params", () => {
}
];
let endpoint_info = test_data.named_endpoints["/predict"];
it("should return an array of data when data is an array", () => {
const data = [1, 2];
const result = map_data_to_params(data, transformed_api_info);
const result = map_data_to_params(data, endpoint_info);
expect(result).toEqual(data);
});
it("should return an empty array when data is an empty array", () => {
const data = [];
const result = map_data_to_params(data, transformed_api_info);
const result = map_data_to_params(data, endpoint_info);
expect(result).toEqual(data);
});
it("should return an empty array when data is not defined", () => {
const data = undefined;
const result = map_data_to_params(data, transformed_api_info);
const result = map_data_to_params(data, endpoint_info);
expect(result).toEqual([]);
});
it("should return the data when too many arguments are provided for the endpoint", () => {
const data = [1, 2, 3, 4];
const result = map_data_to_params(data, transformed_api_info);
const result = map_data_to_params(data, endpoint_info);
expect(result).toEqual(data);
});
@ -608,7 +610,7 @@ describe("map_data_params", () => {
param3: 3
};
const result = map_data_to_params(data, transformed_api_info);
const result = map_data_to_params(data, endpoint_info);
expect(result).toEqual([1, 2, 3]);
});
@ -618,7 +620,7 @@ describe("map_data_params", () => {
param2: 2
};
const result = map_data_to_params(data, transformed_api_info);
const result = map_data_to_params(data, endpoint_info);
expect(result).toEqual([1, 2, 3]);
});
@ -630,7 +632,7 @@ describe("map_data_params", () => {
param4: 4
};
expect(() => map_data_to_params(data, transformed_api_info)).toThrowError(
expect(() => map_data_to_params(data, endpoint_info)).toThrowError(
"Parameter `param4` is not a valid keyword argument. Please refer to the API for usage."
);
});
@ -638,7 +640,7 @@ describe("map_data_params", () => {
it("should throw an error when no value is provided for a required parameter", () => {
const data = {};
expect(() => map_data_to_params(data, transformed_api_info)).toThrowError(
expect(() => map_data_to_params(data, endpoint_info)).toThrowError(
"No value provided for required parameter: param1"
);
});

View File

@ -61,7 +61,7 @@ export function submit(
config
);
let resolved_data = map_data_to_params(data, api_info);
let resolved_data = map_data_to_params(data, endpoint_info);
let websocket: WebSocket;
let stream: EventSource | null;