Skip to content

Commit b5da268

Browse files
authored
Merge pull request #3926 from swagger-api/bug/editor-1565-callback-expansion
Callback expansion issues
2 parents c28b128 + 26eb07d commit b5da268

File tree

7 files changed

+147
-14
lines changed

7 files changed

+147
-14
lines changed

src/core/components/content-type.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ export default class ContentType extends React.Component {
2727
}
2828
}
2929

30+
componentWillReceiveProps(nextProps) {
31+
if(!nextProps.contentTypes || !nextProps.contentTypes.size) {
32+
return
33+
}
34+
35+
if(!nextProps.contentTypes.includes(nextProps.value)) {
36+
nextProps.onChange(nextProps.contentTypes.first())
37+
}
38+
}
39+
3040
onChangeWrapper = e => this.props.onChange(e.target.value)
3141

3242
render() {

src/core/containers/OperationContainer.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,18 @@ export default class OperationContainer extends PureComponent {
5757
const operationId = op.getIn(["operation", "operationId"]) || op.getIn(["operation", "__originalOperationId"]) || opId(op.get("operation"), props.path, props.method) || op.get("id")
5858
const isShownKey = ["operations", props.tag, operationId]
5959
const isDeepLinkingEnabled = deepLinking && deepLinking !== "false"
60+
const allowTryItOut = typeof props.allowTryItOut === "undefined" ?
61+
props.specSelectors.allowTryItOutFor(props.path, props.method) : props.allowTryItOut
6062

6163
return {
6264
operationId,
6365
isDeepLinkingEnabled,
6466
showSummary,
6567
displayOperationId,
6668
displayRequestDuration,
69+
allowTryItOut,
6770
isShown: layoutSelectors.isShown(isShownKey, docExpansion === "full" ),
6871
jumpToKey: `paths.${props.path}.${props.method}`,
69-
allowTryItOut: props.specSelectors.allowTryItOutFor(props.path, props.method),
7072
response: props.specSelectors.responseFor(props.path, props.method),
7173
request: props.specSelectors.requestFor(props.path, props.method)
7274
}

src/core/plugins/oas3/components/callbacks.jsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import React from "react"
22
import PropTypes from "prop-types"
3+
import ImPropTypes from "react-immutable-proptypes"
4+
import { fromJS } from "immutable"
35

46
const Callbacks = (props) => {
57
let { callbacks, getComponent } = props
68
// const Markdown = getComponent("Markdown")
7-
const Operation = getComponent("operation", true)
9+
const OperationContainer = getComponent("OperationContainer", true)
810

911
if(!callbacks) {
1012
return <span>No callbacks</span>
@@ -16,24 +18,22 @@ const Callbacks = (props) => {
1618
{ callback.map((pathItem, pathItemName) => {
1719
return <div key={pathItemName}>
1820
{ pathItem.map((operation, method) => {
19-
return <Operation
20-
operation={operation}
21+
let op = fromJS({
22+
operation
23+
})
24+
return <OperationContainer
25+
{...props}
26+
op={op}
2127
key={method}
28+
tag={""}
2229
method={method}
23-
isShownKey={["callbacks", operation.get("id"), callbackName]}
2430
path={pathItemName}
2531
allowTryItOut={false}
26-
{...props}></Operation>
27-
// return <pre>{JSON.stringify(operation)}</pre>
32+
/>
2833
}) }
2934
</div>
3035
}) }
3136
</div>
32-
// return <div>
33-
// <h2>{name}</h2>
34-
// {callback.description && <Markdown source={callback.description}/>}
35-
// <pre>{JSON.stringify(callback)}</pre>
36-
// </div>
3737
})
3838
return <div>
3939
{callbackElements}
@@ -42,7 +42,7 @@ const Callbacks = (props) => {
4242

4343
Callbacks.propTypes = {
4444
getComponent: PropTypes.func.isRequired,
45-
callbacks: PropTypes.array.isRequired
45+
callbacks: ImPropTypes.iterable.isRequired
4646

4747
}
4848

src/core/plugins/oas3/components/request-body-editor.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ export default class RequestBodyEditor extends PureComponent {
4848
}
4949
}
5050

51+
componentDidUpdate(prevProps) {
52+
if(this.props.requestBody !== prevProps.requestBody) {
53+
// force recalc of value if the request body definition has changed
54+
this.setValueToSample(this.props.mediaType)
55+
}
56+
}
57+
5158
setValueToSample = (explicitMediaType) => {
5259
this.onChange(this.sample(explicitMediaType))
5360
}

src/core/plugins/oas3/components/request-body.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const RequestBody = ({
2222

2323
const mediaTypeValue = requestBodyContent.get(contentType)
2424

25+
if(!mediaTypeValue) {
26+
return null
27+
}
28+
2529
return <div>
2630
{ requestBodyDescription &&
2731
<Markdown source={requestBodyDescription} />

src/core/plugins/view/root-injects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,5 @@ export const getComponent = (getSystem, getStore, getComponents, componentName,
120120
return makeContainer(getSystem, component, getStore())
121121

122122
// container == truthy
123-
return makeContainer(getSystem, component)
123+
return makeContainer(getSystem, wrapRender(component))
124124
}

test/core/system/system.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,116 @@ describe("bound system", function(){
571571
// Then
572572
expect(renderedComponent.text()).toEqual("This came from mapStateToProps and this came from the system and this came from my own props")
573573
})
574+
575+
it("should catch errors thrown inside of React Component Class render methods", function() {
576+
// Given
577+
// eslint-disable-next-line react/require-render-return
578+
class BrokenComponent extends React.Component {
579+
render() {
580+
throw new Error("This component is broken")
581+
}
582+
}
583+
const system = new System({
584+
plugins: [
585+
ViewPlugin,
586+
{
587+
components: {
588+
BrokenComponent
589+
}
590+
}
591+
]
592+
})
593+
594+
// When
595+
var Component = system.getSystem().getComponent("BrokenComponent")
596+
const renderedComponent = render(<Component />)
597+
598+
// Then
599+
expect(renderedComponent.text()).toEqual("😱 Could not render BrokenComponent, see the console.")
600+
})
601+
602+
it("should catch errors thrown inside of pure component render methods", function() {
603+
// Given
604+
// eslint-disable-next-line react/require-render-return
605+
class BrokenComponent extends PureComponent {
606+
render() {
607+
throw new Error("This component is broken")
608+
}
609+
}
610+
611+
const system = new System({
612+
plugins: [
613+
ViewPlugin,
614+
{
615+
components: {
616+
BrokenComponent
617+
}
618+
}
619+
]
620+
})
621+
622+
// When
623+
var Component = system.getSystem().getComponent("BrokenComponent")
624+
const renderedComponent = render(<Component />)
625+
626+
// Then
627+
expect(renderedComponent.text()).toEqual("😱 Could not render BrokenComponent, see the console.")
628+
})
629+
630+
it("should catch errors thrown inside of stateless component functions", function() {
631+
// Given
632+
// eslint-disable-next-line react/require-render-return
633+
let BrokenComponent = function BrokenComponent() { throw new Error("This component is broken") }
634+
const system = new System({
635+
plugins: [
636+
ViewPlugin,
637+
{
638+
components: {
639+
BrokenComponent
640+
}
641+
}
642+
]
643+
})
644+
645+
// When
646+
var Component = system.getSystem().getComponent("BrokenComponent")
647+
const renderedComponent = render(<Component />)
648+
649+
// Then
650+
expect(renderedComponent.text().startsWith("😱 Could not render")).toEqual(true)
651+
})
652+
653+
it("should catch errors thrown inside of container components", function() {
654+
// Given
655+
// eslint-disable-next-line react/require-render-return
656+
class BrokenComponent extends React.Component {
657+
render() {
658+
throw new Error("This component is broken")
659+
}
660+
}
661+
662+
const system = new System({
663+
plugins: [
664+
ViewPlugin,
665+
{
666+
components: {
667+
BrokenComponent
668+
}
669+
}
670+
]
671+
})
672+
673+
// When
674+
var Component = system.getSystem().getComponent("BrokenComponent", true)
675+
const renderedComponent = render(
676+
<Provider store={system.getStore()}>
677+
<Component />
678+
</Provider>
679+
)
680+
681+
// Then
682+
expect(renderedComponent.text()).toEqual("😱 Could not render BrokenComponent, see the console.")
683+
})
574684
})
575685

576686
})

0 commit comments

Comments
 (0)