feature: support for Parameter.content (#5571)

* add `getParameterSchema` OAS helper

* use `Parameter.content.[firstKey].schema` as schema value when present

* `newValue` -> `initialValue`

* make `paramWithMeta` a const

* add trailing comma to `swagger2SchemaKeys`

* refactor `helpers` to a folder

* deprecate `src/core/utils.js` in favor of `src/core/helpers/`

* support `Parameter.content.[mediaType].schema` in validateParam

* reject `null` as an OAS3 object value

* expose Fetch errors in the browser console

* generate ParameterRow default values based on `content` values

* add tests for `getParameterSchema`

* remove debugger statement

* remove debugger statement

* don't apply `generatedSampleValue`s to parameters with `examples`

* remove extra semi

* disable JSON check in parameter runtime validation

* stringify JsonSchema_object textarea values

* add Cypress tests

* swagger-client@3.9.4
This commit is contained in:
kyle
2019-08-31 16:37:43 -07:00
committed by GitHub
parent 24c6473990
commit c9c3b2338e
11 changed files with 400 additions and 52 deletions

View File

@@ -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",
},
},
})
})
})

View File

@@ -412,15 +412,15 @@ describe("utils", function() {
})
assertValidateOas3Param(param, value, [])
// invalid object-as-string
param = {
required: true,
schema: {
type: "object"
}
}
value = "{{}"
assertValidateOas3Param(param, value, ["Parameter string value must be valid JSON"])
// // invalid object-as-string
// param = {
// required: true,
// schema: {
// type: "object"
// }
// }
// value = "{{}"
// assertValidateOas3Param(param, value, ["Parameter string value must be valid JSON"])
// missing when required
param = {
@@ -456,14 +456,14 @@ describe("utils", function() {
})
assertValidateOas3Param(param, value, [])
// invalid object-as-string
param = {
schema: {
type: "object"
}
}
value = "{{}"
assertValidateOas3Param(param, value, ["Parameter string value must be valid JSON"])
// // invalid object-as-string
// param = {
// schema: {
// type: "object"
// }
// }
// value = "{{}"
// assertValidateOas3Param(param, value, ["Parameter string value must be valid JSON"])
// missing when not required
param = {

View File

@@ -0,0 +1,41 @@
openapi: "3.0.0"
paths:
/:
get:
parameters:
- name: users
in: query
description: List of users to query for
content:
application/json:
example:
- userId: 1
currency: USD
- userId: 2
currency: CAD
schema:
$ref: "#/components/schemas/UserArray"
responses:
200:
description: OK!
components:
schemas:
UserArray:
type: array
items:
$ref: "#/components/schemas/User"
User:
type: object
required:
- userId
- currency
properties:
userId:
type: integer
format: int32
currency:
type: string

View File

@@ -0,0 +1,37 @@
/**
* @prettier
*/
describe("UI #4442: Parameter.content display and execution", function() {
it("should display textareas as static documentation according to the `example`", () => {
cy.visit("/?url=/documents/bugs/4442.yaml")
.get(`#operations-default-get_`)
.click()
.get(".btn.try-out__btn")
.click()
.get(
`div.json-schema-array > div:nth-child(1) > div > textarea`
)
.should("have.value", `{\n "userId": 1,\n "currency": "USD"\n}`)
.get(
`div.json-schema-array > div:nth-child(2) > div > textarea`
)
.should("have.value", `{\n "userId": 2,\n "currency": "CAD"\n}`)
})
it("should serialize JSON into a query correctly", () => {
cy.visit("/?url=/documents/bugs/4442.yaml")
.get(`#operations-default-get_`)
.click()
.get(".btn.try-out__btn")
.click()
.get(".btn.execute")
.click()
.get(".request-url pre")
.should(
"have.text",
`http://localhost:3230/?users=${encodeURIComponent(
`[{"userId":1,"currency":"USD"},{"userId":2,"currency":"CAD"}]`
)}`
)
})
})