Skip to content

fix(rendering): components should rely on getSampleSchema utility #6828

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

Closed
wants to merge 2 commits into from
Closed
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
54 changes: 15 additions & 39 deletions src/core/components/parameter-row.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from "react"
import { Map, List } from "immutable"
import { Map } from "immutable"
import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import win from "core/window"
Expand Down Expand Up @@ -97,22 +97,16 @@ export default class ParameterRow extends Component {
let { specSelectors, pathMethod, rawParam, oas3Selectors } = this.props

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

const { schema } = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() })
const parameterMediaType = paramWithMeta
.get("content", Map())
.keySeq()
.first()

// getSampleSchema could return null
const generatedSampleValue = schema ? getSampleSchema(schema.toJS(), parameterMediaType, {

includeWriteOnly: true
}) : null

if (!paramWithMeta || paramWithMeta.get("value") !== undefined) {
return
}

if( paramWithMeta.get("in") !== "body" ) {
let initialValue

Expand All @@ -127,7 +121,7 @@ export default class ParameterRow extends Component {
: (schema && schema.getIn(["default"]))
} else if (specSelectors.isOAS3()) {
const currentExampleKey = oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey())
initialValue =
initialValue =
paramWithMeta.getIn(["examples", currentExampleKey, "value"]) !== undefined
? paramWithMeta.getIn(["examples", currentExampleKey, "value"])
: paramWithMeta.getIn(["content", parameterMediaType, "example"]) !== undefined
Expand All @@ -141,35 +135,17 @@ export default class ParameterRow extends Component {
: paramWithMeta.get("default") // ensures support for `parameterMacro`
}

//// Process the initial value

if(initialValue !== undefined && !List.isList(initialValue)) {
// Stringify if it isn't a List
initialValue = stringify(initialValue)
}
const generatedSampleValue = getSampleSchema(
schema,
parameterMediaType,
{
includeWriteOnly: true
},
initialValue
)

//// Dispatch the initial value

if(initialValue !== undefined) {
this.onChangeWrapper(initialValue)
} else if(
schema && schema.get("type") === "object"
&& generatedSampleValue
&& !paramWithMeta.get("examples")
) {
// Object parameters get special treatment.. if the user doesn't set any
// default or example values, we'll provide initial values generated from
// the schema.
// However, if `examples` exist for the parameter, we won't do anything,
// so that the appropriate `examples` logic can take over.
this.onChangeWrapper(
List.isList(generatedSampleValue) ? (
generatedSampleValue
) : (
stringify(generatedSampleValue)
)
)
}
this.onChangeWrapper(stringify(generatedSampleValue))
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/unit/components/parameter-row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe("<ParameterRow/>", () => {
param,
rawParam: param,
pathMethod: [],
getConfigs: () => ({})
getConfigs: () => ({}),
onChange: () => {}
})

it("Can render Swagger 2 parameter type with format", () => {
Expand Down