Skip to content

Commit 39d3452

Browse files
dedece35shockey
authored andcommitted
Improve enum values for Enum Type in Swagger ReadOnly documentation (#4191)
* Adding enum values for Enum Type in Swagger ReadOnly documentation * Adding enum values for Enum Type in Swagger ReadOnly documentation (optimisation) and also adding default/example value * Add new display enums, defaults, and examples when not in TIO mode (another way to have enums values in swagger.json) * Fix npm test result * review corrections * fix: don't render parameter description if field is empty * use cross-version schema variable to access properties * pass className through Markdown component usage * add per-field classNames to Markdown for easier styling + testing * remove parameter Example field (out-of-scope for this PR) * get default value from schema instead of top-level parameter * tests: add e2e cases for swagger2 and oas3 * remove `swagger-petstore-enum.json` the purpose of this file lives on in the e2e test specs folder * add missing proptypes validation * use `classnames` to more effectively union class names
1 parent d903532 commit 39d3452

File tree

6 files changed

+138
-37
lines changed

6 files changed

+138
-37
lines changed

src/core/components/parameter-row.jsx

+28-30
Original file line numberDiff line numberDiff line change
@@ -107,40 +107,43 @@ export default class ParameterRow extends Component {
107107

108108
let paramWithMeta = specSelectors.parameterWithMeta(pathMethod, param.get("name"), param.get("in"))
109109

110-
let schema = param.get("schema")
111-
let type = isOAS3 && isOAS3() ? param.getIn(["schema", "type"]) : param.get("type")
110+
let schema = isOAS3 && isOAS3() ? param.get("schema") : param
111+
let type = schema.get("type")
112112
let isFormData = inType === "formData"
113113
let isFormDataSupported = "FormData" in win
114114
let required = param.get("required")
115-
let itemType = param.getIn(isOAS3 && isOAS3() ? ["schema", "items", "type"] : ["items", "type"])
115+
let itemType = schema.getIn(["items", "type"])
116116
let value = paramWithMeta ? paramWithMeta.get("value") : ""
117117
let extensions = getExtensions(param)
118118

119119
let paramItems // undefined
120-
let paramItemsEnum // undefined
121-
let isDisplayParamItemsEnum = false
120+
let paramEnum // undefined
121+
let paramDefaultValue // undefined
122+
let paramExample // undefined
123+
let isDisplayParamEnum = false
124+
122125
if ( param !== undefined ) {
123-
paramItems = param.get("items")
126+
paramItems = schema.get("items")
124127
}
125-
if ( paramItems !== undefined ) {
126-
paramItemsEnum = param.get("items").get("enum")
128+
129+
if (paramItems !== undefined) {
130+
paramEnum = paramItems.get("enum")
131+
paramDefaultValue = paramItems.get("default")
132+
} else {
133+
paramEnum = schema.get("enum")
127134
}
128-
if ( paramItemsEnum !== undefined ) {
129-
if (paramItemsEnum.size > 0) {
130-
isDisplayParamItemsEnum = true
131-
}
135+
136+
if ( paramEnum !== undefined && paramEnum.size > 0) {
137+
isDisplayParamEnum = true
132138
}
133139

134140
// Default and Example Value for readonly doc
135-
let paramDefaultValue // undefined
136-
let paramExample // undefined
137141
if ( param !== undefined ) {
138-
paramDefaultValue = param.get("default")
142+
paramDefaultValue = schema.get("default")
139143
paramExample = param.get("example")
140-
}
141-
142-
if (isDisplayParamItemsEnum) { // if we have an array, default value is in "items"
143-
paramDefaultValue = paramItems.get("default")
144+
if (paramExample === undefined) {
145+
paramExample = param.get("x-example")
146+
}
144147
}
145148

146149
return (
@@ -159,23 +162,18 @@ export default class ParameterRow extends Component {
159162
</td>
160163

161164
<td className="col parameters-col_description">
162-
<Markdown source={ param.get("description") }/>
165+
{ param.get("description") ? <Markdown source={ param.get("description") }/> : null }
163166

164-
{ (bodyParam || !isExecute) && isDisplayParamItemsEnum ?
165-
<Markdown source={
166-
"<i>Available values</i>: " + paramItemsEnum.map(function(item) {
167+
{ (bodyParam || !isExecute) && isDisplayParamEnum ?
168+
<Markdown className="parameter__enum" source={
169+
"<i>Available values</i> : " + paramEnum.map(function(item) {
167170
return item
168171
}).toArray().join(", ")}/>
169172
: null
170173
}
171174

172175
{ (bodyParam || !isExecute) && paramDefaultValue !== undefined ?
173-
<Markdown source={"<i>Default value</i>: " + paramDefaultValue}/>
174-
: null
175-
}
176-
177-
{ (bodyParam || !isExecute) && paramExample !== undefined ?
178-
<Markdown source={"<i>Example</i>: " + paramExample}/>
176+
<Markdown className="parameter__default" source={"<i>Default value</i> : " + paramDefaultValue}/>
179177
: null
180178
}
181179

@@ -189,7 +187,7 @@ export default class ParameterRow extends Component {
189187
description={param.get("description") ? `${param.get("name")} - ${param.get("description")}` : `${param.get("name")}`}
190188
onChange={ this.onChangeWrapper }
191189
errors={ paramWithMeta.get("errors") }
192-
schema={ isOAS3 && isOAS3() ? param.get("schema") : param }/>
190+
schema={ schema }/>
193191
}
194192

195193

src/core/components/providers/markdown.jsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import React from "react"
22
import PropTypes from "prop-types"
33
import Remarkable from "remarkable"
44
import sanitize from "sanitize-html"
5+
import cx from "classnames"
56

67
// eslint-disable-next-line no-useless-escape
78
const isPlainText = (str) => /^[A-Z\s0-9!?\.]+$/gi.test(str)
89

9-
function Markdown({ source }) {
10+
function Markdown({ source, className = "" }) {
1011
if(isPlainText(source)) {
1112
// If the source text is not Markdown,
1213
// let's save some time and just render it.
@@ -28,12 +29,13 @@ function Markdown({ source }) {
2829
}
2930

3031
return (
31-
<div className="markdown" dangerouslySetInnerHTML={{ __html: sanitized }}></div>
32+
<div className={cx(className, "markdown")} dangerouslySetInnerHTML={{ __html: sanitized }}></div>
3233
)
3334
}
3435

3536
Markdown.propTypes = {
36-
source: PropTypes.string.isRequired
37+
source: PropTypes.string.isRequired,
38+
className: PropTypes.string.isRequired
3739
}
3840

3941
export default Markdown

src/core/plugins/oas3/wrap-components/markdown.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from "react"
22
import PropTypes from "prop-types"
33
import ReactMarkdown from "react-markdown"
4+
import cx from "classnames"
45
import { Parser, HtmlRenderer } from "commonmark"
56
import { OAS3ComponentWrapFactory } from "../helpers"
67
import { sanitizer } from "core/components/providers/markdown"
78

8-
export const Markdown = ({ source }) => {
9+
export const Markdown = ({ source, className = "" }) => {
910
if ( source ) {
1011
const parser = new Parser()
1112
const writer = new HtmlRenderer()
@@ -19,14 +20,15 @@ export const Markdown = ({ source }) => {
1920
return (
2021
<ReactMarkdown
2122
source={sanitized}
22-
className={"renderedMarkdown"}
23+
className={cx(className, "renderedMarkdown")}
2324
/>
2425
)
2526
}
2627
return null
2728
}
2829
Markdown.propTypes = {
29-
source: PropTypes.string
30+
source: PropTypes.string,
31+
className: PropTypes.string,
3032
}
3133

32-
export default OAS3ComponentWrapFactory(Markdown)
34+
export default OAS3ComponentWrapFactory(Markdown)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
describe("parameter enum rendering", function () {
2+
describe("swagger 2.0", () => {
3+
beforeEach(function (client, done) {
4+
client
5+
.url("localhost:3230")
6+
.waitForElementVisible(".download-url-input", 10000)
7+
.pause(1000)
8+
.clearValue(".download-url-input")
9+
.setValue(".download-url-input", "http://localhost:3230/test-specs/features/parameter-enum-rendering.swagger.yaml")
10+
.click("button.download-url-button")
11+
.pause(1000)
12+
13+
done()
14+
})
15+
afterEach(function (client, done) {
16+
done()
17+
})
18+
it("reveals a string parameter's enums and defaults when viewing that parameter", function (client) {
19+
client.waitForElementVisible(".opblock-tag-section", 10000)
20+
.assert.containsText(".opblock-summary-path span", "/report")
21+
.click(".opblock")
22+
.waitForElementVisible(".opblock.is-open", 5000)
23+
.pause(500)
24+
.assert.containsText("div.parameter__enum", "today, yesterday, lastweek")
25+
.assert.containsText("div.parameter__default", "today")
26+
27+
client.end()
28+
})
29+
})
30+
describe("openapi 3.0", () => {
31+
beforeEach(function (client, done) {
32+
client
33+
.url("localhost:3230")
34+
.waitForElementVisible(".download-url-input", 10000)
35+
.pause(1000)
36+
.clearValue(".download-url-input")
37+
.setValue(".download-url-input", "http://localhost:3230/test-specs/features/parameter-enum-rendering.openapi.yaml")
38+
.click("button.download-url-button")
39+
.pause(1000)
40+
41+
done()
42+
})
43+
afterEach(function (client, done) {
44+
done()
45+
})
46+
it("reveals a string parameter's enums and defaults when viewing that parameter", function (client) {
47+
client.waitForElementVisible(".opblock-tag-section", 10000)
48+
.assert.containsText(".opblock-summary-path span", "/report")
49+
.click(".opblock")
50+
.waitForElementVisible(".opblock.is-open", 5000)
51+
.pause(500)
52+
.assert.containsText("div.parameter__enum", "today, yesterday, lastweek")
53+
.assert.containsText("div.parameter__default", "today")
54+
55+
client.end()
56+
})
57+
})
58+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
openapi: 3.0.0
2+
info:
3+
title: test
4+
version: 0.0.0
5+
paths:
6+
/report:
7+
get:
8+
parameters:
9+
- in: query
10+
name: rel_date
11+
required: true
12+
schema:
13+
type: string
14+
default: today
15+
enum:
16+
- today
17+
- yesterday
18+
- lastweek
19+
responses:
20+
'200':
21+
description: OK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
swagger: '2.0'
2+
info:
3+
title: test
4+
version: 0.0.0
5+
paths:
6+
/report:
7+
get:
8+
parameters:
9+
- in: query
10+
name: rel_date
11+
required: true
12+
type: string
13+
default: today
14+
enum:
15+
- today
16+
- yesterday
17+
- lastweek
18+
responses:
19+
200:
20+
description: OK

0 commit comments

Comments
 (0)