Skip to content

Commit

Permalink
Merge branch 'feature/multipart-form-data-support' into scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
kjagiello committed Apr 5, 2019
2 parents b408941 + 6155f85 commit 5694209
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
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 === "data") {
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,
});
});

0 comments on commit 5694209

Please sign in to comment.