Skip to content

Commit

Permalink
add tests for getParameterSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
shockey committed Aug 28, 2019
1 parent 9106e39 commit 9a4ff63
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/helpers/get-parameter-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Im from "immutable"

const swagger2SchemaKeys = Im.Set(
const swagger2SchemaKeys = Im.Set.of(
"type",
"format",
"items",
Expand Down Expand Up @@ -37,13 +37,13 @@ const swagger2SchemaKeys = Im.Set(
* or OpenAPI 3.0 definition
* @return {Immutable.Map} The desired schema
*/
export default function getParameterSchema(parameter, { isOAS3 }) {
export default function getParameterSchema(parameter, { isOAS3 } = {}) {
// Return empty Map if `parameter` isn't a Map
if (!Im.Map.isMap(parameter)) return Im.Map()

if (!isOAS3) {
// Swagger 2.0
if (parameter.get("type") === "body") {
if (parameter.get("in") === "body") {
return parameter.get("schema", Im.Map())
} else {
return parameter.filter((v, k) => swagger2SchemaKeys.includes(k))
Expand All @@ -54,7 +54,7 @@ export default function getParameterSchema(parameter, { isOAS3 }) {

if (parameter.get("content")) {
const parameterContentMediaTypes = parameter
.get("content", Im.Map())
.get("content", Im.Map({}))
.keySeq()

return parameter.getIn(
Expand Down
142 changes: 142 additions & 0 deletions test/core/helpers/get-parameter-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* @prettier
*/

import expect from "expect"
import Im, { fromJS } from "immutable"
import getParameterSchema from "../../../src/helpers/get-parameter-schema"

describe("getParameterSchema", () => {
it("should return an empty Map when given no parameters", () => {
const result = getParameterSchema()

expect(result).toEqual(fromJS({}))
})

it("should return an empty Map when given an empty Map", () => {
const result = getParameterSchema(fromJS({}))

expect(result).toEqual(fromJS({}))
})

it("should return a schema for a Swagger 2.0 query parameter", () => {
const result = getParameterSchema(
fromJS({
name: "id",
in: "query",
description: "ID of the object to fetch",
required: false,
type: "array",
items: {
type: "string",
},
collectionFormat: "multi",
})
)

expect(result.toJS()).toEqual({
type: "array",
items: {
type: "string",
},
})
})

it("should return a schema for a Swagger 2.0 body parameter", () => {
const result = getParameterSchema(
fromJS({
name: "user",
in: "body",
description: "user to add to the system",
required: true,
schema: {
type: "array",
items: {
type: "string",
},
},
})
)

expect(result.toJS()).toEqual({
type: "array",
items: {
type: "string",
},
})
})

it("should return a schema for an OpenAPI 3.0 query parameter", () => {
const result = getParameterSchema(
fromJS({
name: "id",
in: "query",
description: "ID of the object to fetch",
required: false,
schema: {
type: "array",
items: {
type: "string",
},
},
style: "form",
explode: true,
}),
{
isOAS3: true,
}
)

expect(result.toJS()).toEqual({
type: "array",
items: {
type: "string",
},
})
})

it("should return a schema for an OpenAPI 3.0 query parameter with `content`", () => {
const result = getParameterSchema(
fromJS({
in: "query",
name: "coordinates",
content: {
"application/json": {
schema: {
type: "object",
required: ["lat", "long"],
properties: {
lat: {
type: "number",
},
long: {
type: "number",
},
},
},
"should-ignore/the-second-media-type": {
type: "string",
default: "this shouldn't be returned",
},
},
},
}),
{
isOAS3: true,
}
)

expect(result.toJS()).toEqual({
type: "object",
required: ["lat", "long"],
properties: {
lat: {
type: "number",
},
long: {
type: "number",
},
},
})
})
})

0 comments on commit 9a4ff63

Please sign in to comment.