Skip to content

Commit 1ce9ce0

Browse files
committed
refactor: use samples plugin to generate schemas examples (#8728)
Refs #8577
1 parent 027f53c commit 1ce9ce0

File tree

8 files changed

+22
-98
lines changed

8 files changed

+22
-98
lines changed

Diff for: src/core/components/param-body.jsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { PureComponent } from "react"
22
import PropTypes from "prop-types"
33
import { fromJS, List } from "immutable"
4-
import { getSampleSchema } from "core/utils"
54
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"
65

76
const NOOP = Function.prototype
@@ -67,10 +66,10 @@ export default class ParamBody extends PureComponent {
6766
}
6867

6968
sample = (xml) => {
70-
let { param, fn:{inferSchema} } = this.props
71-
let schema = inferSchema(param.toJS())
69+
let { param, fn} = this.props
70+
let schema = fn.inferSchema(param.toJS())
7271

73-
return getSampleSchema(schema, xml, {
72+
return fn.getSampleSchema(schema, xml, {
7473
includeWriteOnly: true
7574
})
7675
}

Diff for: src/core/components/parameter-row.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Map, List } from "immutable"
33
import PropTypes from "prop-types"
44
import ImPropTypes from "react-immutable-proptypes"
55
import win from "core/window"
6-
import { getSampleSchema, getExtensions, getCommonExtensions, numberToString, stringify, isEmptyValue } from "core/utils"
6+
import { getExtensions, getCommonExtensions, numberToString, stringify, isEmptyValue } from "core/utils"
77
import getParameterSchema from "../../helpers/get-parameter-schema.js"
88

99
export default class ParameterRow extends Component {
@@ -94,7 +94,7 @@ export default class ParameterRow extends Component {
9494
}
9595

9696
setDefaultValue = () => {
97-
let { specSelectors, pathMethod, rawParam, oas3Selectors } = this.props
97+
let { specSelectors, pathMethod, rawParam, oas3Selectors, fn } = this.props
9898

9999
const paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
100100
const { schema } = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() })
@@ -104,7 +104,7 @@ export default class ParameterRow extends Component {
104104
.first()
105105

106106
// getSampleSchema could return null
107-
const generatedSampleValue = schema ? getSampleSchema(schema.toJS(), parameterMediaType, {
107+
const generatedSampleValue = schema ? fn.getSampleSchema(schema.toJS(), parameterMediaType, {
108108

109109
includeWriteOnly: true
110110
}) : null

Diff for: src/core/components/response.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from "prop-types"
33
import ImPropTypes from "react-immutable-proptypes"
44
import cx from "classnames"
55
import { fromJS, Seq, Iterable, List, Map } from "immutable"
6-
import { getExtensions, getSampleSchema, fromJSOrdered, stringify } from "core/utils"
6+
import { getExtensions, fromJSOrdered, stringify } from "core/utils"
77
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"
88

99

@@ -93,7 +93,7 @@ export default class Response extends React.Component {
9393
oas3Actions,
9494
} = this.props
9595

96-
let { inferSchema } = fn
96+
let { inferSchema, getSampleSchema } = fn
9797
let isOAS3 = specSelectors.isOAS3()
9898
const { showExtensions } = getConfigs()
9999

Diff for: src/core/json-schema-components.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { List, fromJS } from "immutable"
44
import cx from "classnames"
55
import ImPropTypes from "react-immutable-proptypes"
66
import DebounceInput from "react-debounce-input"
7-
import { stringify, getSampleSchema } from "core/utils"
8-
//import "less/json-schema-form"
7+
import { stringify } from "core/utils"
98

109
const noop = ()=> {}
1110
const JsonSchemaPropShape = {
@@ -156,9 +155,10 @@ export class JsonSchema_array extends PureComponent {
156155
}
157156

158157
addItem = () => {
158+
const { fn } = this.props
159159
let newValue = valueOrEmptyList(this.state.value)
160160
this.setState(() => ({
161-
value: newValue.push(getSampleSchema(this.state.schema.get("items"), false, {
161+
value: newValue.push(fn.getSampleSchema(this.state.schema.get("items"), false, {
162162
includeWriteOnly: true
163163
}))
164164
}), this.onChange)

Diff for: src/core/plugins/json-schema-2020-12/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ const JSONSchema202012Plugin = () => ({
104104
},
105105
fn: {
106106
upperFirst,
107-
memoizedSampleFromSchema: null,
108107
jsonSchema202012: {
109108
isExpandable,
110109
hasKeyword,

Diff for: src/core/plugins/oas3/components/request-body.jsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import React from "react"
22
import PropTypes from "prop-types"
33
import ImPropTypes from "react-immutable-proptypes"
44
import { Map, OrderedMap, List } from "immutable"
5-
import { getCommonExtensions, getSampleSchema, stringify, isEmptyValue } from "core/utils"
5+
import { getCommonExtensions, stringify, isEmptyValue } from "core/utils"
66
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"
77

8-
export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExamplesKey) => {
8+
export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExamplesKey, fn) => {
99
const mediaTypeValue = requestBody.getIn(["content", mediaType])
1010
const schema = mediaTypeValue.get("schema").toJS()
1111

@@ -19,7 +19,7 @@ export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExample
1919
])
2020
: exampleSchema
2121

22-
const exampleValue = getSampleSchema(
22+
const exampleValue = fn.getSampleSchema(
2323
schema,
2424
mediaType,
2525
{
@@ -92,6 +92,7 @@ const RequestBody = ({
9292
requestBody,
9393
contentType,
9494
key,
95+
fn,
9596
), val)
9697
}
9798
return container

Diff for: src/core/plugins/oas3/selectors.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const shouldRetainRequestBodyValue = onlyOAS3((state, path, method) => {
7676

7777
export const selectDefaultRequestBodyValue =
7878
(state, path, method) => (system) => {
79-
const { oas3Selectors, specSelectors } = system.getSystem()
79+
const { oas3Selectors, specSelectors, fn } = system.getSystem()
8080

8181
if (specSelectors.isOAS3()) {
8282
const currentMediaType = oas3Selectors.requestContentType(path, method)
@@ -94,7 +94,8 @@ export const selectDefaultRequestBodyValue =
9494
method,
9595
"requestBody",
9696
"requestBody"
97-
)
97+
),
98+
fn
9899
)
99100
}
100101
}

Diff for: src/core/utils.js

+4-80
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
If you're refactoring something in here, feel free to break it out to a file
1010
in `./helpers` if you have the time.
1111
*/
12-
1312
import Im, { fromJS, Set } from "immutable"
1413
import { sanitizeUrl as braintreeSanitizeUrl } from "@braintree/sanitize-url"
1514
import camelCase from "lodash/camelCase"
@@ -19,14 +18,11 @@ import find from "lodash/find"
1918
import some from "lodash/some"
2019
import eq from "lodash/eq"
2120
import isFunction from "lodash/isFunction"
22-
import { memoizedSampleFromSchema, memoizedCreateXMLExample } from "core/plugins/samples/fn"
2321
import win from "./window"
2422
import cssEscape from "css.escape"
2523
import getParameterSchema from "../helpers/get-parameter-schema"
2624
import randomBytes from "randombytes"
2725
import shaJs from "sha.js"
28-
import YAML, { JSON_SCHEMA } from "js-yaml"
29-
3026

3127
const DEFAULT_RESPONSE_KEY = "default"
3228

@@ -599,86 +595,14 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
599595

600596
let paramRequired = param.get("required")
601597

602-
let { schema: paramDetails, parameterContentMediaType } = getParameterSchema(param, { isOAS3 })
598+
let {
599+
schema: paramDetails,
600+
parameterContentMediaType
601+
} = getParameterSchema(param, { isOAS3 })
603602

604603
return validateValueBySchema(value, paramDetails, paramRequired, bypassRequiredCheck, parameterContentMediaType)
605604
}
606605

607-
const getXmlSampleSchema = (schema, config, exampleOverride) => {
608-
if (schema && !schema.xml) {
609-
schema.xml = {}
610-
}
611-
if (schema && !schema.xml.name) {
612-
if (!schema.$$ref && (schema.type || schema.items || schema.properties || schema.additionalProperties)) {
613-
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- XML example cannot be generated; root element name is undefined -->"
614-
}
615-
if (schema.$$ref) {
616-
let match = schema.$$ref.match(/\S*\/(\S+)$/)
617-
schema.xml.name = match[1]
618-
}
619-
}
620-
621-
return memoizedCreateXMLExample(schema, config, exampleOverride)
622-
}
623-
624-
const shouldStringifyTypesConfig = [
625-
{
626-
when: /json/,
627-
shouldStringifyTypes: ["string"]
628-
}
629-
]
630-
631-
const defaultStringifyTypes = ["object"]
632-
633-
const getStringifiedSampleForSchema = (schema, config, contentType, exampleOverride) => {
634-
const res = memoizedSampleFromSchema(schema, config, exampleOverride)
635-
const resType = typeof res
636-
637-
const typesToStringify = shouldStringifyTypesConfig.reduce(
638-
(types, nextConfig) => nextConfig.when.test(contentType)
639-
? [...types, ...nextConfig.shouldStringifyTypes]
640-
: types,
641-
defaultStringifyTypes)
642-
643-
return some(typesToStringify, x => x === resType)
644-
? JSON.stringify(res, null, 2)
645-
: res
646-
}
647-
648-
const getYamlSampleSchema = (schema, config, contentType, exampleOverride) => {
649-
const jsonExample = getStringifiedSampleForSchema(schema, config, contentType, exampleOverride)
650-
let yamlString
651-
try {
652-
yamlString = YAML.dump(YAML.load(jsonExample), {
653-
654-
lineWidth: -1 // don't generate line folds
655-
}, { schema: JSON_SCHEMA })
656-
if(yamlString[yamlString.length - 1] === "\n") {
657-
yamlString = yamlString.slice(0, yamlString.length - 1)
658-
}
659-
} catch (e) {
660-
console.error(e)
661-
return "error: could not generate yaml example"
662-
}
663-
return yamlString
664-
.replace(/\t/g, " ")
665-
}
666-
667-
export const getSampleSchema = (schema, contentType="", config={}, exampleOverride = undefined) => {
668-
if(schema && isFunc(schema.toJS))
669-
schema = schema.toJS()
670-
if(exampleOverride && isFunc(exampleOverride.toJS))
671-
exampleOverride = exampleOverride.toJS()
672-
673-
if (/xml/.test(contentType)) {
674-
return getXmlSampleSchema(schema, config, exampleOverride)
675-
}
676-
if (/(yaml|yml)/.test(contentType)) {
677-
return getYamlSampleSchema(schema, config, contentType, exampleOverride)
678-
}
679-
return getStringifiedSampleForSchema(schema, config, contentType, exampleOverride)
680-
}
681-
682606
export const parseSearch = () => {
683607
let map = {}
684608
let search = win.location.search

0 commit comments

Comments
 (0)