Skip to content

Commit

Permalink
refactor: use samples plugin to generate schemas examples (#8728)
Browse files Browse the repository at this point in the history
Refs #8577
  • Loading branch information
char0n committed Jun 1, 2023
1 parent 027f53c commit 1ce9ce0
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 98 deletions.
7 changes: 3 additions & 4 deletions src/core/components/param-body.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { PureComponent } from "react"
import PropTypes from "prop-types"
import { fromJS, List } from "immutable"
import { getSampleSchema } from "core/utils"
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"

const NOOP = Function.prototype
Expand Down Expand Up @@ -67,10 +66,10 @@ export default class ParamBody extends PureComponent {
}

sample = (xml) => {
let { param, fn:{inferSchema} } = this.props
let schema = inferSchema(param.toJS())
let { param, fn} = this.props
let schema = fn.inferSchema(param.toJS())

return getSampleSchema(schema, xml, {
return fn.getSampleSchema(schema, xml, {
includeWriteOnly: true
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/components/parameter-row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Map, List } from "immutable"
import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import win from "core/window"
import { getSampleSchema, getExtensions, getCommonExtensions, numberToString, stringify, isEmptyValue } from "core/utils"
import { getExtensions, getCommonExtensions, numberToString, stringify, isEmptyValue } from "core/utils"
import getParameterSchema from "../../helpers/get-parameter-schema.js"

export default class ParameterRow extends Component {
Expand Down Expand Up @@ -94,7 +94,7 @@ export default class ParameterRow extends Component {
}

setDefaultValue = () => {
let { specSelectors, pathMethod, rawParam, oas3Selectors } = this.props
let { specSelectors, pathMethod, rawParam, oas3Selectors, fn } = this.props

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

// getSampleSchema could return null
const generatedSampleValue = schema ? getSampleSchema(schema.toJS(), parameterMediaType, {
const generatedSampleValue = schema ? fn.getSampleSchema(schema.toJS(), parameterMediaType, {

includeWriteOnly: true
}) : null
Expand Down
4 changes: 2 additions & 2 deletions src/core/components/response.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import cx from "classnames"
import { fromJS, Seq, Iterable, List, Map } from "immutable"
import { getExtensions, getSampleSchema, fromJSOrdered, stringify } from "core/utils"
import { getExtensions, fromJSOrdered, stringify } from "core/utils"
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"


Expand Down Expand Up @@ -93,7 +93,7 @@ export default class Response extends React.Component {
oas3Actions,
} = this.props

let { inferSchema } = fn
let { inferSchema, getSampleSchema } = fn
let isOAS3 = specSelectors.isOAS3()
const { showExtensions } = getConfigs()

Expand Down
6 changes: 3 additions & 3 deletions src/core/json-schema-components.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { List, fromJS } from "immutable"
import cx from "classnames"
import ImPropTypes from "react-immutable-proptypes"
import DebounceInput from "react-debounce-input"
import { stringify, getSampleSchema } from "core/utils"
//import "less/json-schema-form"
import { stringify } from "core/utils"

const noop = ()=> {}
const JsonSchemaPropShape = {
Expand Down Expand Up @@ -156,9 +155,10 @@ export class JsonSchema_array extends PureComponent {
}

addItem = () => {
const { fn } = this.props
let newValue = valueOrEmptyList(this.state.value)
this.setState(() => ({
value: newValue.push(getSampleSchema(this.state.schema.get("items"), false, {
value: newValue.push(fn.getSampleSchema(this.state.schema.get("items"), false, {
includeWriteOnly: true
}))
}), this.onChange)
Expand Down
1 change: 0 additions & 1 deletion src/core/plugins/json-schema-2020-12/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ const JSONSchema202012Plugin = () => ({
},
fn: {
upperFirst,
memoizedSampleFromSchema: null,
jsonSchema202012: {
isExpandable,
hasKeyword,
Expand Down
7 changes: 4 additions & 3 deletions src/core/plugins/oas3/components/request-body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from "react"
import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import { Map, OrderedMap, List } from "immutable"
import { getCommonExtensions, getSampleSchema, stringify, isEmptyValue } from "core/utils"
import { getCommonExtensions, stringify, isEmptyValue } from "core/utils"
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"

export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExamplesKey) => {
export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExamplesKey, fn) => {
const mediaTypeValue = requestBody.getIn(["content", mediaType])
const schema = mediaTypeValue.get("schema").toJS()

Expand All @@ -19,7 +19,7 @@ export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExample
])
: exampleSchema

const exampleValue = getSampleSchema(
const exampleValue = fn.getSampleSchema(
schema,
mediaType,
{
Expand Down Expand Up @@ -92,6 +92,7 @@ const RequestBody = ({
requestBody,
contentType,
key,
fn,
), val)
}
return container
Expand Down
5 changes: 3 additions & 2 deletions src/core/plugins/oas3/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const shouldRetainRequestBodyValue = onlyOAS3((state, path, method) => {

export const selectDefaultRequestBodyValue =
(state, path, method) => (system) => {
const { oas3Selectors, specSelectors } = system.getSystem()
const { oas3Selectors, specSelectors, fn } = system.getSystem()

if (specSelectors.isOAS3()) {
const currentMediaType = oas3Selectors.requestContentType(path, method)
Expand All @@ -94,7 +94,8 @@ export const selectDefaultRequestBodyValue =
method,
"requestBody",
"requestBody"
)
),
fn
)
}
}
Expand Down
84 changes: 4 additions & 80 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
If you're refactoring something in here, feel free to break it out to a file
in `./helpers` if you have the time.
*/

import Im, { fromJS, Set } from "immutable"
import { sanitizeUrl as braintreeSanitizeUrl } from "@braintree/sanitize-url"
import camelCase from "lodash/camelCase"
Expand All @@ -19,14 +18,11 @@ import find from "lodash/find"
import some from "lodash/some"
import eq from "lodash/eq"
import isFunction from "lodash/isFunction"
import { memoizedSampleFromSchema, memoizedCreateXMLExample } from "core/plugins/samples/fn"
import win from "./window"
import cssEscape from "css.escape"
import getParameterSchema from "../helpers/get-parameter-schema"
import randomBytes from "randombytes"
import shaJs from "sha.js"
import YAML, { JSON_SCHEMA } from "js-yaml"


const DEFAULT_RESPONSE_KEY = "default"

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

let paramRequired = param.get("required")

let { schema: paramDetails, parameterContentMediaType } = getParameterSchema(param, { isOAS3 })
let {
schema: paramDetails,
parameterContentMediaType
} = getParameterSchema(param, { isOAS3 })

return validateValueBySchema(value, paramDetails, paramRequired, bypassRequiredCheck, parameterContentMediaType)
}

const getXmlSampleSchema = (schema, config, exampleOverride) => {
if (schema && !schema.xml) {
schema.xml = {}
}
if (schema && !schema.xml.name) {
if (!schema.$$ref && (schema.type || schema.items || schema.properties || schema.additionalProperties)) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- XML example cannot be generated; root element name is undefined -->"
}
if (schema.$$ref) {
let match = schema.$$ref.match(/\S*\/(\S+)$/)
schema.xml.name = match[1]
}
}

return memoizedCreateXMLExample(schema, config, exampleOverride)
}

const shouldStringifyTypesConfig = [
{
when: /json/,
shouldStringifyTypes: ["string"]
}
]

const defaultStringifyTypes = ["object"]

const getStringifiedSampleForSchema = (schema, config, contentType, exampleOverride) => {
const res = memoizedSampleFromSchema(schema, config, exampleOverride)
const resType = typeof res

const typesToStringify = shouldStringifyTypesConfig.reduce(
(types, nextConfig) => nextConfig.when.test(contentType)
? [...types, ...nextConfig.shouldStringifyTypes]
: types,
defaultStringifyTypes)

return some(typesToStringify, x => x === resType)
? JSON.stringify(res, null, 2)
: res
}

const getYamlSampleSchema = (schema, config, contentType, exampleOverride) => {
const jsonExample = getStringifiedSampleForSchema(schema, config, contentType, exampleOverride)
let yamlString
try {
yamlString = YAML.dump(YAML.load(jsonExample), {

lineWidth: -1 // don't generate line folds
}, { schema: JSON_SCHEMA })
if(yamlString[yamlString.length - 1] === "\n") {
yamlString = yamlString.slice(0, yamlString.length - 1)
}
} catch (e) {
console.error(e)
return "error: could not generate yaml example"
}
return yamlString
.replace(/\t/g, " ")
}

export const getSampleSchema = (schema, contentType="", config={}, exampleOverride = undefined) => {
if(schema && isFunc(schema.toJS))
schema = schema.toJS()
if(exampleOverride && isFunc(exampleOverride.toJS))
exampleOverride = exampleOverride.toJS()

if (/xml/.test(contentType)) {
return getXmlSampleSchema(schema, config, exampleOverride)
}
if (/(yaml|yml)/.test(contentType)) {
return getYamlSampleSchema(schema, config, contentType, exampleOverride)
}
return getStringifiedSampleForSchema(schema, config, contentType, exampleOverride)
}

export const parseSearch = () => {
let map = {}
let search = win.location.search
Expand Down

0 comments on commit 1ce9ce0

Please sign in to comment.