Skip to content

Commit 9155eb9

Browse files
authored
fix: always display locally-available title property as a model's name (#4542)
* fix: always display locally-available `title` property as a model's name * fix failing unit test * add e2e test case
1 parent 66e9bd7 commit 9155eb9

File tree

9 files changed

+64
-13
lines changed

9 files changed

+64
-13
lines changed

src/core/components/array-model.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ export default class ArrayModel extends Component {
1111
getConfigs: PropTypes.func.isRequired,
1212
specSelectors: PropTypes.object.isRequired,
1313
name: PropTypes.string,
14+
displayName: PropTypes.string,
1415
required: PropTypes.bool,
1516
expandDepth: PropTypes.number,
1617
specPath: ImPropTypes.list.isRequired,
1718
depth: PropTypes.number
1819
}
1920

2021
render(){
21-
let { getComponent, getConfigs, schema, depth, expandDepth, name, specPath } = this.props
22+
let { getComponent, getConfigs, schema, depth, expandDepth, name, displayName, specPath } = this.props
2223
let description = schema.get("description")
2324
let items = schema.get("items")
24-
let title = schema.get("title") || name
25+
let title = schema.get("title") || displayName || name
2526
let properties = schema.filter( ( v, key) => ["type", "items", "description", "$$ref"].indexOf(key) === -1 )
2627

2728
const Markdown = getComponent("Markdown")

src/core/components/model-wrapper.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default class ModelWrapper extends Component {
99
static propTypes = {
1010
schema: PropTypes.object.isRequired,
1111
name: PropTypes.string,
12+
displayName: PropTypes.string,
1213
getComponent: PropTypes.func.isRequired,
1314
getConfigs: PropTypes.func.isRequired,
1415
specSelectors: PropTypes.object.isRequired,

src/core/components/model.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default class Model extends ImmutablePureComponent {
1010
getConfigs: PropTypes.func.isRequired,
1111
specSelectors: PropTypes.object.isRequired,
1212
name: PropTypes.string,
13+
displayName: PropTypes.string,
1314
isRef: PropTypes.bool,
1415
required: PropTypes.bool,
1516
expandDepth: PropTypes.number,
@@ -33,7 +34,7 @@ export default class Model extends ImmutablePureComponent {
3334
}
3435

3536
render () {
36-
let { getComponent, getConfigs, specSelectors, schema, required, name, isRef, specPath } = this.props
37+
let { getComponent, getConfigs, specSelectors, schema, required, name, isRef, specPath, displayName } = this.props
3738
const ObjectModel = getComponent("ObjectModel")
3839
const ArrayModel = getComponent("ArrayModel")
3940
const PrimitiveModel = getComponent("PrimitiveModel")
@@ -51,7 +52,7 @@ export default class Model extends ImmutablePureComponent {
5152

5253
if(!schema) {
5354
return <span className="model model-title">
54-
<span className="model-title__text">{ name }</span>
55+
<span className="model-title__text">{ displayName || name }</span>
5556
<img src={require("core/../img/rolling-load.svg")} height={"20px"} width={"20px"} style={{
5657
marginLeft: "1em",
5758
position: "relative",

src/core/components/models.jsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ export default class Models extends Component {
5151
</h4>
5252
<Collapse isOpened={showModels}>
5353
{
54-
definitions.entrySeq().map( ( [ name ])=>{
54+
definitions.entrySeq().map(([name])=>{
5555

56-
const schema = specSelectors.specResolvedSubtree([...specPathBase, name])
56+
const fullPath = [...specPathBase, name]
57+
const schema = specSelectors.specResolvedSubtree(fullPath)|| Im.Map()
58+
const rawSchema = specSelectors.specJson().getIn(fullPath, Im.Map())
59+
const displayName = schema.get("title") || rawSchema.get("title") || name
5760

5861
if(layoutSelectors.isShown(["models", name], false) && schema === undefined) {
5962
// Firing an action in a container render is not great,
@@ -63,7 +66,8 @@ export default class Models extends Component {
6366

6467
const content = <ModelWrapper name={ name }
6568
expandDepth={ defaultModelsExpandDepth }
66-
schema={ schema || Im.Map() }
69+
schema={ schema || Im.Map() }
70+
displayName={displayName}
6771
specPath={Im.List([...specPathBase, name])}
6872
getComponent={ getComponent }
6973
specSelectors={ specSelectors }
@@ -72,7 +76,9 @@ export default class Models extends Component {
7276
layoutActions = {layoutActions}/>
7377

7478
const title = <span className="model-box">
75-
<span className="model model-title">{name}</span>
79+
<span className="model model-title">
80+
{displayName}
81+
</span>
7682
</span>
7783

7884
return <div id={ `model-${name}` } className="model-container" key={ `models-section-${name}` }>
@@ -81,6 +87,7 @@ export default class Models extends Component {
8187
collapsedContent={this.getCollapsedContent(name)}
8288
onToggle={this.handleToggle}
8389
title={title}
90+
displayName={displayName}
8491
modelName={name}
8592
hideSelfOnExpand={true}
8693
expanded={defaultModelsExpandDepth > 1}

src/core/components/object-model.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ export default class ObjectModel extends Component {
1515
onToggle: PropTypes.func,
1616
specSelectors: PropTypes.object.isRequired,
1717
name: PropTypes.string,
18+
displayName: PropTypes.string,
1819
isRef: PropTypes.bool,
1920
expandDepth: PropTypes.number,
2021
depth: PropTypes.number,
2122
specPath: ImPropTypes.list.isRequired
2223
}
2324

2425
render(){
25-
let { schema, name, isRef, getComponent, getConfigs, depth, onToggle, expanded, specPath, ...otherProps } = this.props
26+
let { schema, name, displayName, isRef, getComponent, getConfigs, depth, onToggle, expanded, specPath, ...otherProps } = this.props
2627
let { specSelectors,expandDepth } = otherProps
2728
const { isOAS3 } = specSelectors
2829

@@ -35,7 +36,7 @@ export default class ObjectModel extends Component {
3536
let description = schema.get("description")
3637
let properties = schema.get("properties")
3738
let additionalProperties = schema.get("additionalProperties")
38-
let title = schema.get("title") || name
39+
let title = schema.get("title") || displayName || name
3940
let requiredProperties = schema.get("required")
4041

4142
const JumpToPath = getComponent("JumpToPath", true)

src/core/components/primitive-model.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ export default class Primitive extends Component {
1010
getComponent: PropTypes.func.isRequired,
1111
getConfigs: PropTypes.func.isRequired,
1212
name: PropTypes.string,
13+
displayName: PropTypes.string,
1314
depth: PropTypes.number
1415
}
1516

1617
render(){
17-
let { schema, getComponent, getConfigs, name, depth } = this.props
18+
let { schema, getComponent, getConfigs, name, displayName, depth } = this.props
1819

1920
const { showExtensions } = getConfigs()
2021

@@ -27,7 +28,7 @@ export default class Primitive extends Component {
2728
let format = schema.get("format")
2829
let xml = schema.get("xml")
2930
let enumArray = schema.get("enum")
30-
let title = schema.get("title") || name
31+
let title = schema.get("title") || displayName || name
3132
let description = schema.get("description")
3233
let extensions = getExtensions(schema)
3334
let properties = schema

test/components/models.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import React from "react"
33
import expect, { createSpy } from "expect"
44
import { shallow } from "enzyme"
5-
import { fromJS } from "immutable"
5+
import { fromJS, Map } from "immutable"
66
import Models from "components/models"
77
import ModelCollpase from "components/model-collapse"
88
import ModelComponent from "components/model-wrapper"
@@ -19,6 +19,7 @@ describe("<Models/>", function(){
1919
},
2020
specSelectors: {
2121
isOAS3: () => false,
22+
specJson: () => Map(),
2223
definitions: function() {
2324
return fromJS({
2425
def1: {},

test/e2e/scenarios/bugs/4536.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
describe("bug #4536: model name consistency", function () {
2+
let mainPage
3+
beforeEach(function (client, done) {
4+
mainPage = client
5+
.url("localhost:3230")
6+
.page.main()
7+
8+
client.waitForElementVisible(".download-url-input", 5000)
9+
.pause(2000)
10+
.clearValue(".download-url-input")
11+
.setValue(".download-url-input", "http://localhost:3230/test-specs/bugs/4536.yaml")
12+
.click("button.download-url-button")
13+
.pause(1000)
14+
15+
done()
16+
})
17+
afterEach(function (client, done) {
18+
done()
19+
})
20+
it("consistently displays a model's name regardless of expansion state", function (client) {
21+
client.waitForElementVisible("span.model.model-title", 10000)
22+
.assert.containsText("span.model.model-title", "TitleName")
23+
.click("span.model.model-title")
24+
.pause(500)
25+
.assert.containsText("span.model-title__text", "TitleName")
26+
27+
client.end()
28+
})
29+
})

test/e2e/specs/bugs/4536.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
swagger: '2.0'
2+
info:
3+
version: 0.0.0
4+
title: test
5+
paths: {}
6+
7+
definitions:
8+
ModelName:
9+
title: TitleName

0 commit comments

Comments
 (0)