Skip to content

Improve enum values for Enum Type in Swagger ReadOnly documentation #4191

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

Merged
merged 32 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d58ecc9
Adding enum values for Enum Type in Swagger ReadOnly documentation
dedece35 Nov 29, 2017
3a6b5b1
Merge branch 'master' into master
dedece35 Dec 12, 2017
8ee3c6d
Merge remote-tracking branch 'upstream/master'
dedece35 Dec 13, 2017
da75e3c
Adding enum values for Enum Type in Swagger ReadOnly documentation (o…
Dec 13, 2017
cdf57e4
Merge branch 'master' into master
dedece35 Jan 4, 2018
fff710d
Merge branch 'master' into master
shockey Jan 6, 2018
3ac1a3c
Conflict from original github repo
dedece35 Feb 4, 2018
c17b02c
Add new display enums, defaults, and examples when not in TIO mode (a…
dedece35 Feb 4, 2018
6aba5d1
Fix npm test result
dedece35 Feb 4, 2018
a2d863a
Merge branch 'master' into master
dedece35 Feb 13, 2018
34761b0
Merge branch 'master' into master
dedece35 Feb 16, 2018
7f6c814
Merge branch 'master' into master
dedece35 Feb 20, 2018
6bcda39
Merge branch 'master' into master
dedece35 Mar 5, 2018
d7bc198
Merge branch 'master' into master
dedece35 Mar 12, 2018
dc63157
Merge branch 'master' into master
dedece35 Mar 16, 2018
ebb765b
Merge branch 'master' into master
dedece35 Mar 18, 2018
7e07e1f
Merge branch 'master' into master
dedece35 Mar 23, 2018
fa09a29
review corrections
dedece35 Mar 23, 2018
3d93e5a
Merge branch 'master' into master
dedece35 Mar 24, 2018
2120836
Merge branch 'master' into master
dedece35 Mar 25, 2018
50fdc65
Merge branch 'master' into master
dedece35 Mar 28, 2018
3f45283
Merge branch 'master' into master
shockey Apr 6, 2018
bac5e7d
fix: don't render parameter description if field is empty
shockey Apr 6, 2018
b36b641
use cross-version schema variable to access properties
shockey Apr 6, 2018
9d02ee8
pass className through Markdown component usage
shockey Apr 6, 2018
a43d109
add per-field classNames to Markdown for easier styling + testing
shockey Apr 6, 2018
240db57
remove parameter Example field (out-of-scope for this PR)
shockey Apr 6, 2018
e5dc422
get default value from schema instead of top-level parameter
shockey Apr 6, 2018
2de8448
tests: add e2e cases for swagger2 and oas3
shockey Apr 6, 2018
1ac4be3
remove `swagger-petstore-enum.json`
shockey Apr 6, 2018
8f1356e
add missing proptypes validation
shockey Apr 6, 2018
16932d2
use `classnames` to more effectively union class names
shockey Apr 6, 2018
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
58 changes: 28 additions & 30 deletions src/core/components/parameter-row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,40 +107,43 @@ export default class ParameterRow extends Component {

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

let schema = param.get("schema")
let type = isOAS3 && isOAS3() ? param.getIn(["schema", "type"]) : param.get("type")
let schema = isOAS3 && isOAS3() ? param.get("schema") : param
let type = schema.get("type")
let isFormData = inType === "formData"
let isFormDataSupported = "FormData" in win
let required = param.get("required")
let itemType = param.getIn(isOAS3 && isOAS3() ? ["schema", "items", "type"] : ["items", "type"])
let itemType = schema.getIn(["items", "type"])
let value = paramWithMeta ? paramWithMeta.get("value") : ""
let extensions = getExtensions(param)

let paramItems // undefined
let paramItemsEnum // undefined
let isDisplayParamItemsEnum = false
let paramEnum // undefined
let paramDefaultValue // undefined
let paramExample // undefined
let isDisplayParamEnum = false

if ( param !== undefined ) {
paramItems = param.get("items")
paramItems = schema.get("items")
}
if ( paramItems !== undefined ) {
paramItemsEnum = param.get("items").get("enum")

if (paramItems !== undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These statements should be inverted: the if statement should have a positive condition (paramItems === undefined) and the else statement should be used for the negative case.

Copy link
Contributor Author

@dedece35 dedece35 Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this condition, I have no positive case : I only want to do things in negative case. I can't translate this condition in positive way.

paramEnum = paramItems.get("enum")
paramDefaultValue = paramItems.get("default")
} else {
paramEnum = schema.get("enum")
}
if ( paramItemsEnum !== undefined ) {
if (paramItemsEnum.size > 0) {
isDisplayParamItemsEnum = true
}

if ( paramEnum !== undefined && paramEnum.size > 0) {
isDisplayParamEnum = true
}

// Default and Example Value for readonly doc
let paramDefaultValue // undefined
let paramExample // undefined
if ( param !== undefined ) {
paramDefaultValue = param.get("default")
paramDefaultValue = schema.get("default")
paramExample = param.get("example")
}

if (isDisplayParamItemsEnum) { // if we have an array, default value is in "items"
paramDefaultValue = paramItems.get("default")
if (paramExample === undefined) {
paramExample = param.get("x-example")
}
}

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

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

{ (bodyParam || !isExecute) && isDisplayParamItemsEnum ?
<Markdown source={
"<i>Available values</i>: " + paramItemsEnum.map(function(item) {
{ (bodyParam || !isExecute) && isDisplayParamEnum ?
<Markdown className="parameter__enum" source={
"<i>Available values</i> : " + paramEnum.map(function(item) {
return item
}).toArray().join(", ")}/>
: null
}

{ (bodyParam || !isExecute) && paramDefaultValue !== undefined ?
<Markdown source={"<i>Default value</i>: " + paramDefaultValue}/>
: null
}

{ (bodyParam || !isExecute) && paramExample !== undefined ?
<Markdown source={"<i>Example</i>: " + paramExample}/>
<Markdown className="parameter__default" source={"<i>Default value</i> : " + paramDefaultValue}/>
: null
}

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


Expand Down
8 changes: 5 additions & 3 deletions src/core/components/providers/markdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import React from "react"
import PropTypes from "prop-types"
import Remarkable from "remarkable"
import sanitize from "sanitize-html"
import cx from "classnames"

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

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

return (
<div className="markdown" dangerouslySetInnerHTML={{ __html: sanitized }}></div>
<div className={cx(className, "markdown")} dangerouslySetInnerHTML={{ __html: sanitized }}></div>
)
}

Markdown.propTypes = {
source: PropTypes.string.isRequired
source: PropTypes.string.isRequired,
className: PropTypes.string.isRequired
}

export default Markdown
Expand Down
10 changes: 6 additions & 4 deletions src/core/plugins/oas3/wrap-components/markdown.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from "react"
import PropTypes from "prop-types"
import ReactMarkdown from "react-markdown"
import cx from "classnames"
import { Parser, HtmlRenderer } from "commonmark"
import { OAS3ComponentWrapFactory } from "../helpers"
import { sanitizer } from "core/components/providers/markdown"

export const Markdown = ({ source }) => {
export const Markdown = ({ source, className = "" }) => {
if ( source ) {
const parser = new Parser()
const writer = new HtmlRenderer()
Expand All @@ -19,14 +20,15 @@ export const Markdown = ({ source }) => {
return (
<ReactMarkdown
source={sanitized}
className={"renderedMarkdown"}
className={cx(className, "renderedMarkdown")}
/>
)
}
return null
}
Markdown.propTypes = {
source: PropTypes.string
source: PropTypes.string,
className: PropTypes.string,
}

export default OAS3ComponentWrapFactory(Markdown)
export default OAS3ComponentWrapFactory(Markdown)
58 changes: 58 additions & 0 deletions test/e2e/scenarios/features/parameter-enum-rendering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
describe("parameter enum rendering", function () {
describe("swagger 2.0", () => {
beforeEach(function (client, done) {
client
.url("localhost:3230")
.waitForElementVisible(".download-url-input", 10000)
.pause(1000)
.clearValue(".download-url-input")
.setValue(".download-url-input", "http://localhost:3230/test-specs/features/parameter-enum-rendering.swagger.yaml")
.click("button.download-url-button")
.pause(1000)

done()
})
afterEach(function (client, done) {
done()
})
it("reveals a string parameter's enums and defaults when viewing that parameter", function (client) {
client.waitForElementVisible(".opblock-tag-section", 10000)
.assert.containsText(".opblock-summary-path span", "/report")
.click(".opblock")
.waitForElementVisible(".opblock.is-open", 5000)
.pause(500)
.assert.containsText("div.parameter__enum", "today, yesterday, lastweek")
.assert.containsText("div.parameter__default", "today")

client.end()
})
})
describe("openapi 3.0", () => {
beforeEach(function (client, done) {
client
.url("localhost:3230")
.waitForElementVisible(".download-url-input", 10000)
.pause(1000)
.clearValue(".download-url-input")
.setValue(".download-url-input", "http://localhost:3230/test-specs/features/parameter-enum-rendering.openapi.yaml")
.click("button.download-url-button")
.pause(1000)

done()
})
afterEach(function (client, done) {
done()
})
it("reveals a string parameter's enums and defaults when viewing that parameter", function (client) {
client.waitForElementVisible(".opblock-tag-section", 10000)
.assert.containsText(".opblock-summary-path span", "/report")
.click(".opblock")
.waitForElementVisible(".opblock.is-open", 5000)
.pause(500)
.assert.containsText("div.parameter__enum", "today, yesterday, lastweek")
.assert.containsText("div.parameter__default", "today")

client.end()
})
})
})
21 changes: 21 additions & 0 deletions test/e2e/specs/features/parameter-enum-rendering.openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.0.0
info:
title: test
version: 0.0.0
paths:
/report:
get:
parameters:
- in: query
name: rel_date
required: true
schema:
type: string
default: today
enum:
- today
- yesterday
- lastweek
responses:
'200':
description: OK
20 changes: 20 additions & 0 deletions test/e2e/specs/features/parameter-enum-rendering.swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
swagger: '2.0'
info:
title: test
version: 0.0.0
paths:
/report:
get:
parameters:
- in: query
name: rel_date
required: true
type: string
default: today
enum:
- today
- yesterday
- lastweek
responses:
200:
description: OK