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

Example #4730

Merged
merged 11 commits into from
Jul 14, 2018
43 changes: 42 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"chromedriver": "^2.38.3",
"copy-webpack-plugin": "^4.0.1",
"css-loader": "^0.28.11",
"dedent": "^0.7.0",
"deepmerge": "^2.1.0",
"enzyme": "^2.7.1",
"eslint": "^4.1.1",
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/param-body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default class ParamBody extends PureComponent {
let { value, isEditBox } = this.state

return (
<div className="body-param">
<div className="body-param" data-param-name={param.get("name")} data-param-in={param.get("in")}>
{
isEditBox && isExecute
? <TextArea className={ "body-param__text" + ( errors.count() ? " invalid" : "")} value={value} onChange={ this.handleOnChange }/>
Expand Down
44 changes: 24 additions & 20 deletions src/core/components/parameter-row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ export default class ParameterRow extends Component {
let { specSelectors, pathMethod, rawParam } = props
let { isOAS3 } = specSelectors

let example = rawParam.get("example")

let parameterWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam)
// fallback, if the meta lookup fails
parameterWithMeta = parameterWithMeta.isEmpty() ? rawParam : parameterWithMeta

let enumValue

if(isOAS3()) {
let schema = rawParam.get("schema") || Map()
let schema = parameterWithMeta.get("schema") || Map()
enumValue = schema.get("enum")
} else {
enumValue = parameterWithMeta ? parameterWithMeta.get("enum") : undefined
Expand All @@ -50,15 +48,15 @@ export default class ParameterRow extends Component {

if ( paramValue !== undefined ) {
value = paramValue
} else if ( example !== undefined ) {
value = example
} else if ( rawParam.get("required") && enumValue && enumValue.size ) {
value = enumValue.first()
}

if ( value !== undefined ) {
if ( value !== undefined && value !== paramValue ) {
this.onChangeWrapper(value)
}

this.setDefaultValue()
}

onChangeWrapper = (value, isXml = false) => {
Expand All @@ -69,22 +67,28 @@ export default class ParameterRow extends Component {
setDefaultValue = () => {
let { specSelectors, pathMethod, rawParam } = this.props

if (rawParam.get("value") !== undefined) {
return
}
let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam)

let schema = specSelectors.isOAS3() ? rawParam.get("schema", Map({})) : rawParam

let defaultValue = schema.get("default")
let xExampleValue = rawParam.get("x-example") // Swagger 2 only
let parameter = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam)
let value = parameter ? parameter.get("value") : ""
if (paramWithMeta.get("value") !== undefined) {
return
}

if( rawParam.get("in") !== "body" ) {
if ( xExampleValue !== undefined && value === undefined && specSelectors.isSwagger2() ) {
this.onChangeWrapper(xExampleValue)
} else if ( defaultValue !== undefined && value === undefined ) {
this.onChangeWrapper(defaultValue)
if( paramWithMeta.get("in") !== "body" ) {
let newValue

if (specSelectors.isSwagger2()) {
newValue = paramWithMeta.get("x-example")
|| paramWithMeta.getIn(["default"])
|| paramWithMeta.getIn(["schema", "example"])
|| paramWithMeta.getIn(["schema", "default"])
} else if (specSelectors.isOAS3()) {
newValue = paramWithMeta.get("example")
|| paramWithMeta.getIn(["schema", "example"])
|| paramWithMeta.getIn(["schema", "default"])
}
if(newValue !== undefined) {
this.onChangeWrapper(newValue)
}
}
}
Expand Down Expand Up @@ -161,7 +165,7 @@ export default class ParameterRow extends Component {
}

return (
<tr className="parameters">
<tr data-param-name={param.get("name")} data-param-in={param.get("in")}>
<td className="col parameters-col_name">
<div className={required ? "parameter__name required" : "parameter__name"}>
{ param.get("name") }
Expand Down
39 changes: 20 additions & 19 deletions src/core/components/response.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@ import React from "react"
import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import cx from "classnames"
import { fromJS, Seq, Iterable, List } from "immutable"
import { getSampleSchema, fromJSOrdered } from "core/utils"
import { fromJS, Seq, Iterable, List, Map } from "immutable"
import { getSampleSchema, fromJSOrdered, stringify } from "core/utils"

const getExampleComponent = ( sampleResponse, examples, HighlightCode ) => {
if ( examples && examples.size ) {
return examples.entrySeq().map( ([ key, example ]) => {
let exampleValue = example
if ( example.toJS ) {
try {
exampleValue = JSON.stringify(example.toJS(), null, 2)
}
catch(e) {
exampleValue = String(example)
}
}
let exampleValue = stringify(example)

return (<div key={ key }>
<h5>{ key }</h5>
Expand Down Expand Up @@ -97,20 +89,29 @@ export default class Response extends React.Component {
const ContentType = getComponent("contentType")

var sampleResponse
var sampleSchema
var schema, specPathWithPossibleSchema

const activeContentType = this.state.responseContentType || contentType

if(isOAS3()) {
const schemaPath = List(["content", this.state.responseContentType, "schema"])
const oas3SchemaForContentType = response.getIn(schemaPath)
sampleResponse = oas3SchemaForContentType ? getSampleSchema(oas3SchemaForContentType.toJS(), this.state.responseContentType, {
includeReadOnly: true
}) : null
const mediaType = response.getIn(["content", activeContentType], Map({}))
const oas3SchemaForContentType = mediaType.get("schema", Map({}))

if(mediaType.get("example") !== undefined) {
sampleSchema = stringify(mediaType.get("example"))
} else {
sampleSchema = getSampleSchema(oas3SchemaForContentType.toJS(), this.state.responseContentType, {
includeReadOnly: true
})
}
sampleResponse = oas3SchemaForContentType ? sampleSchema : null
schema = oas3SchemaForContentType ? inferSchema(oas3SchemaForContentType.toJS()) : null
specPathWithPossibleSchema = oas3SchemaForContentType ? schemaPath : specPath
specPathWithPossibleSchema = oas3SchemaForContentType ? List(["content", this.state.responseContentType, "schema"]) : specPath
} else {
schema = inferSchema(response.toJS()) // TODO: don't convert back and forth. Lets just stick with immutable for inferSchema
specPathWithPossibleSchema = response.has("schema") ? specPath.push("schema") : specPath
sampleResponse = schema ? getSampleSchema(schema, contentType, {
sampleResponse = schema ? getSampleSchema(schema, activeContentType, {
includeReadOnly: true,
includeWriteOnly: true // writeOnly has no filtering effect in swagger 2.0
}) : null
Expand All @@ -126,7 +127,7 @@ export default class Response extends React.Component {
let example = getExampleComponent( sampleResponse, examples, HighlightCode )

return (
<tr className={ "response " + ( className || "") }>
<tr className={ "response " + ( className || "") } data-code={code}>
<td className="col response-col_status">
{ code }
</td>
Expand Down
8 changes: 5 additions & 3 deletions src/core/plugins/oas3/components/request-body-editor.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { PureComponent } from "react"
import PropTypes from "prop-types"
import { fromJS } from "immutable"
import { getSampleSchema } from "core/utils"
import { getSampleSchema, stringify } from "core/utils"

const NOOP = Function.prototype

Expand Down Expand Up @@ -67,9 +67,11 @@ export default class RequestBodyEditor extends PureComponent {

sample = (explicitMediaType) => {
let { requestBody, mediaType } = this.props
let schema = requestBody.getIn(["content", explicitMediaType || mediaType, "schema"]).toJS()
let mediaTypeValue = requestBody.getIn(["content", explicitMediaType || mediaType])
let schema = mediaTypeValue.get("schema").toJS()
let mediaTypeExample = mediaTypeValue.get("example") !== undefined ? stringify(mediaTypeValue.get("example")) : null

return getSampleSchema(schema, explicitMediaType || mediaType, {
return mediaTypeExample || getSampleSchema(schema, explicitMediaType || mediaType, {
includeWriteOnly: true
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/plugins/samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export const sampleFromSchema = (schema, config={}) => {
let props = objectify(properties)
let obj = {}
for (var name in props) {
if ( props[name].readOnly && !includeReadOnly ) {
if ( props[name] && props[name].readOnly && !includeReadOnly ) {
continue
}
if ( props[name].writeOnly && !includeWriteOnly ) {
if ( props[name] && props[name].writeOnly && !includeWriteOnly ) {
continue
}
obj[name] = sampleFromSchema(props[name], config)
Expand Down
21 changes: 21 additions & 0 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,24 @@ export function deeplyStripKey(input, keyToStrip, predicate = () => true) {

return obj
}

export function stringify(thing) {
if (typeof thing === "string") {
return thing
}

if (thing.toJS) {
thing = thing.toJS()
}

if (typeof thing === "object" && thing !== null) {
try {
return JSON.stringify(thing, null, 2)
}
catch (e) {
return String(thing)
}
}

return thing.toString()
}
6 changes: 4 additions & 2 deletions test/bugs/4557-default-parameter-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ describe("bug #4557: default parameter values", function(){
specSelectors: {
security(){},
parameterWithMetaByIdentity(){ return paramValue },
isOAS3(){ return false }
isOAS3(){ return false },
isSwagger2(){ return true }
},
fn: {},
operation: {get: ()=>{}},
Expand Down Expand Up @@ -52,7 +53,8 @@ describe("bug #4557: default parameter values", function(){
specSelectors: {
security(){},
parameterWithMetaByIdentity(){ return paramValue },
isOAS3(){ return true }
isOAS3(){ return true },
isSwagger2() { return false }
},
fn: {},
operation: {get: ()=>{}},
Expand Down
4 changes: 0 additions & 4 deletions test/e2e/nightwatch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
"custom_assertions_path" : "",
"page_objects_path" : "test/e2e/pages",
"globals_path" : "",
"test_workers" : {
"enabled" : true,
"workers" : "auto"
},

"test_runner" : {
"type" : "mocha",
Expand Down
Loading