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

Multipart form data support #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ Swagger.makeApisTagOperation = client => {
}),
{}
);
} else if (parameter.in === "formData") {
parameters.data = parameters.data || {};
parameters.data[parameter.name] = parameter;
} else if (parameter.in === "query") {
parameters[parameter.name] = parameter;
}
Expand Down
19 changes: 18 additions & 1 deletion src/forms/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ function normalizePath(path) {
return path.replace("]", "").replace("[", ".");
}

// TODO: more verbose error messages
export function fieldFromSchema(schema, field) {
if (Array.isArray(schema)) {
return fieldFromFlatSchema(schema, field);
}
return fieldFromNestedSchema(schema, field);
}

// TODO: more verbose error messages
// Nested schema is typically used for content-type application/json
export function fieldFromNestedSchema(schema, field) {
const normalizedSchema = { type: "object", properties: schema };
const normalizedPath = normalizePath(field);
return normalizedPath.split(".").reduce((acc, key) => {
Expand All @@ -27,6 +35,15 @@ export function fieldFromSchema(schema, field) {
}, normalizedSchema);
}

// Flat schema is typically used for content-type multipart/form-data
export function fieldFromFlatSchema(schema, field) {
const value = schema.find(({ name }) => name === field);
if (typeof value === "undefined") {
throw new Error(`Encountered a non-existent field "${field}".`);
}
return value;
}

export function fieldFromErrorResponse(response, field) {
const normalizedPath = normalizePath(field);
return normalizedPath
Expand Down
32 changes: 32 additions & 0 deletions tests/fixtures/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,35 @@ export const nestedSchema = {
$$ref: "#/definitions/Artist",
},
};

// Used when an endpoint expects multipart/form-data
export const flatSchema = [
{
name: "order",
in: "formData",
required: true,
type: "integer",
},
{
name: "title",
in: "formData",
required: true,
type: "string",
maxLength: 64,
minLength: 1,
},
{
name: "content",
in: "formData",
required: false,
type: "string",
"x-nullable": true,
},
{
name: "attachment",
in: "formData",
required: false,
type: "file",
"x-nullable": true,
},
];
12 changes: 11 additions & 1 deletion tests/forms/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fieldFromErrorResponse, fieldFromSchema } from "../../src/forms/utils";
import { errorResponse } from "../fixtures/response";
import { nestedSchema } from "../fixtures/schema";
import { flatSchema, nestedSchema } from "../fixtures/schema";

test("Get a top level field from schema", () => {
expect(fieldFromSchema(nestedSchema, "id")).toEqual({
Expand Down Expand Up @@ -65,3 +65,13 @@ test("Get an array field from error response", () => {
"Ensure this field has at least 12 characters.",
]);
});

test("Get a field from a multipart/form-data schema", () => {
expect(fieldFromSchema(flatSchema, "attachment")).toEqual({
name: "attachment",
in: "formData",
required: false,
type: "file",
"x-nullable": true,
});
});