diff --git a/src/core/components/response.jsx b/src/core/components/response.jsx
index 548b6dcfb38..839b677b50a 100644
--- a/src/core/components/response.jsx
+++ b/src/core/components/response.jsx
@@ -4,7 +4,7 @@ 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 { isFunc } from "../utils"
+
const getExampleComponent = ( sampleResponse, HighlightCode, getConfigs ) => {
if (
@@ -121,21 +121,6 @@ export default class Response extends React.Component {
specPathWithPossibleSchema = response.has("schema") ? specPath.push("schema") : specPath
}
- const overrideSchemaExample = (oldSchema, newExample) => {
- if(newExample === undefined)
- return oldSchema
-
- if(!oldSchema)
- oldSchema = { }
-
- if(isFunc(oldSchema.toJS))
- oldSchema = oldSchema.toJS()
-
- oldSchema.example = newExample && isFunc(newExample.toJS)
- ? newExample.toJS()
- : newExample
- return oldSchema
- }
let mediaTypeExample
let shouldOverrideSchemaExample = false
let sampleSchema
@@ -170,13 +155,12 @@ export default class Response extends React.Component {
}
}
- const schemaForSampleGeneration = shouldOverrideSchemaExample
- ? overrideSchemaExample(sampleSchema, mediaTypeExample)
- : sampleSchema
-
- const sampleResponse = schemaForSampleGeneration
- ? getSampleSchema(schemaForSampleGeneration, activeContentType, sampleGenConfig)
- : null
+ const sampleResponse = getSampleSchema(
+ sampleSchema,
+ activeContentType,
+ sampleGenConfig,
+ shouldOverrideSchemaExample ? mediaTypeExample : undefined
+ )
let example = getExampleComponent( sampleResponse, HighlightCode, getConfigs )
diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js
index 401ba329141..a5312b9a176 100644
--- a/src/core/plugins/samples/fn.js
+++ b/src/core/plugins/samples/fn.js
@@ -1,7 +1,7 @@
import { objectify, isFunc, normalizeArray, deeplyStripKey } from "core/utils"
import XML from "@kyleshockey/xml"
import memoizee from "memoizee"
-import deepAssign from "@kyleshockey/object-assign-deep"
+import isEmpty from "lodash/isEmpty"
const primitives = {
"string": () => "string",
@@ -30,251 +30,314 @@ const primitive = (schema) => {
return "Unknown Type: " + schema.type
}
+// do a couple of quick sanity tests to ensure the value
+// looks like a $$ref that swagger-client generates.
+const sanitizeRef = (value) => deeplyStripKey(value, "$$ref", (val) =>
+ typeof val === "string" && val.indexOf("#") > -1)
-export const sampleFromSchema = (schema, config={}) => {
- let { type, example, properties, additionalProperties, items } = objectify(schema)
- let { includeReadOnly, includeWriteOnly } = config
-
-
- if(example !== undefined) {
- return deeplyStripKey(example, "$$ref", (val) => {
- // do a couple of quick sanity tests to ensure the value
- // looks like a $$ref that swagger-client generates.
- return typeof val === "string" && val.indexOf("#") > -1
- })
+const liftSampleHelper = (oldSchema, target) => {
+ if(target.example === undefined && oldSchema.example !== undefined) {
+ target.example = oldSchema.example
}
-
- if(!type) {
- if(properties) {
- type = "object"
- } else if(items) {
- type = "array"
- } else {
- return
- }
+ if(target.default === undefined && oldSchema.default !== undefined) {
+ target.default = oldSchema.default
}
-
- if(type === "object") {
- let props = objectify(properties)
- let obj = {}
- for (var name in props) {
- if ( props[name] && props[name].deprecated ) {
- continue
- }
- if ( props[name] && props[name].readOnly && !includeReadOnly ) {
- continue
- }
- if ( props[name] && props[name].writeOnly && !includeWriteOnly ) {
- continue
- }
- obj[name] = sampleFromSchema(props[name], config)
- }
-
- if ( additionalProperties === true ) {
- obj.additionalProp1 = {}
- } else if ( additionalProperties ) {
- let additionalProps = objectify(additionalProperties)
- let additionalPropVal = sampleFromSchema(additionalProps, config)
-
- for (let i = 1; i < 4; i++) {
- obj["additionalProp" + i] = additionalPropVal
- }
- }
- return obj
+ if(target.enum === undefined && oldSchema.enum !== undefined) {
+ target.enum = oldSchema.enum
+ }
+ if(target.xml === undefined && oldSchema.xml !== undefined) {
+ target.xml = oldSchema.xml
}
+ return target
+}
- if(type === "array") {
- if(Array.isArray(items.anyOf)) {
- return items.anyOf.map(i => sampleFromSchema(i, config))
- }
+export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = undefined, respectXML = false) => {
+ const _attr = {}
+ let { xml, type, example, properties, additionalProperties, items } = objectify(schema)
+ let { includeReadOnly, includeWriteOnly } = config
+ xml = xml || {}
+ let { name, prefix, namespace } = xml
+ let displayName
+ let res = {}
- if(Array.isArray(items.oneOf)) {
- return items.oneOf.map(i => sampleFromSchema(i, config))
+ // set xml naming and attributes
+ if(respectXML) {
+ name = name || "notagname"
+ // add prefix to name if exists
+ displayName = (prefix ? prefix + ":" : "") + name
+ if ( namespace ) {
+ //add prefix to namespace if exists
+ let namespacePrefix = prefix ? ( "xmlns:" + prefix ) : "xmlns"
+ _attr[namespacePrefix] = namespace
}
-
- return [ sampleFromSchema(items, config) ]
- }
-
- if(schema["enum"]) {
- if(schema["default"])
- return schema["default"]
- return normalizeArray(schema["enum"])[0]
}
- if (type === "file") {
- return
+ // init xml default response sample obj
+ if(respectXML) {
+ res[displayName] = []
}
- return primitive(schema)
-}
-
-export const inferSchema = (thing) => {
- if(thing.schema)
- thing = thing.schema
+ const usePlainValue = exampleOverride !== undefined || example !== undefined || schema && schema.default !== undefined
- if(thing.properties) {
- thing.type = "object"
+ const hasOneOf = !usePlainValue && schema && schema.oneOf && schema.oneOf.length > 0
+ const hasAnyOf = !usePlainValue && schema && schema.anyOf && schema.anyOf.length > 0
+ if(!usePlainValue && (hasOneOf || hasAnyOf)) {
+ const someSchema = hasOneOf
+ ? schema.oneOf[0]
+ : schema.anyOf[0]
+ liftSampleHelper(schema, someSchema)
+ return sampleFromSchemaGeneric(someSchema, config, undefined, respectXML)
}
- return thing // Hopefully this will have something schema like in it... `type` for example
-}
-
-
-export const sampleXmlFromSchema = (schema, config={}) => {
- let objectifySchema = deepAssign({}, objectify(schema))
- let { type, properties, additionalProperties, items, example } = objectifySchema
- let { includeReadOnly, includeWriteOnly } = config
- let defaultValue = objectifySchema.default
- let res = {}
- let _attr = {}
- let { xml } = schema
- let { name, prefix, namespace } = xml
- let enumValue = objectifySchema.enum
- let displayName, value
-
- if(!type) {
+ // try recover missing type
+ if(schema && !type) {
if(properties || additionalProperties) {
type = "object"
} else if(items) {
type = "array"
- } else {
+ } else if(!usePlainValue){
return
}
}
- name = name || "notagname"
- // add prefix to name if exists
- displayName = (prefix ? prefix + ":" : "") + name
- if ( namespace ) {
- //add prefix to namespace if exists
- let namespacePrefix = prefix ? ( "xmlns:" + prefix ) : "xmlns"
- _attr[namespacePrefix] = namespace
+ // add to result helper init for xml or json
+ const props = objectify(properties)
+ let addPropertyToResult
+ if(respectXML) {
+ addPropertyToResult = (propName, overrideE = undefined) => {
+ if(schema) {
+ // case it is an xml attribute
+ props[propName].xml = props[propName].xml || {}
+
+ if (props[propName].xml.attribute) {
+ const enumAttrVal = Array.isArray(props[propName].enum)
+ ? props[propName].enum[0]
+ : undefined
+ const attrExample = props[propName].example
+ const attrDefault = props[propName].default
+
+ if(attrExample !== undefined) {
+ _attr[props[propName].xml.name || propName] = attrExample
+ } else if(attrDefault !== undefined) {
+ _attr[props[propName].xml.name || propName] = attrDefault
+ } else if(enumAttrVal !== undefined) {
+ _attr[props[propName].xml.name || propName] = enumAttrVal
+ } else {
+ _attr[props[propName].xml.name || propName] = primitive(props[propName])
+ }
+
+ return
+ }
+ props[propName].xml.name = props[propName].xml.name || propName
+ }
+
+ let t = sampleFromSchemaGeneric(schema && props[propName] || undefined, config, overrideE, respectXML)
+ if (Array.isArray(t)) {
+ res[displayName] = res[displayName].concat(t)
+ } else {
+ res[displayName].push(t)
+ }
+ }
+ } else {
+ addPropertyToResult = (propName, overrideE) =>
+ res[propName] = sampleFromSchemaGeneric(props[propName], config, overrideE, respectXML)
}
- if (type === "array") {
- if (items) {
- items.xml = items.xml || xml || {}
- items.xml.name = items.xml.name || xml.name
+ // check for plain value and if found use it to generate sample from it
+ if(usePlainValue) {
+ let sample
+ if(exampleOverride !== undefined) {
+ sample = sanitizeRef(exampleOverride)
+ } else if(example !== undefined) {
+ sample = sanitizeRef(example)
+ } else {
+ sample = sanitizeRef(schema.default)
+ }
- if (xml.wrapped) {
- res[displayName] = []
- if (Array.isArray(example)) {
- example.forEach((v)=>{
- items.example = v
- res[displayName].push(sampleXmlFromSchema(items, config))
- })
- } else if (Array.isArray(defaultValue)) {
- defaultValue.forEach((v)=>{
- items.default = v
- res[displayName].push(sampleXmlFromSchema(items, config))
- })
- } else {
- res[displayName] = [sampleXmlFromSchema(items, config)]
- }
+ // if json just return
+ if(!respectXML) {
+ return sample
+ }
- if (_attr) {
+ // recover missing type
+ if(!schema) {
+ type = Array.isArray(sample) ? "array" : typeof sample
+ }
+
+ // generate xml sample recursively for array case
+ if(type === "array") {
+ if (!Array.isArray(sample)) {
+ sample = [sample]
+ }
+ const itemSchema = schema
+ ? schema.items
+ : undefined
+ if(itemSchema) {
+ itemSchema.xml = itemSchema.xml || xml || {}
+ itemSchema.xml.name = itemSchema.xml.name || xml.name
+ }
+ const itemSamples = sample
+ .map(s => sampleFromSchemaGeneric(itemSchema, config, s, respectXML))
+ if(xml.wrapped) {
+ res[displayName] = itemSamples
+ if (!isEmpty(_attr)) {
res[displayName].push({_attr: _attr})
}
- return res
}
+ else {
+ res = itemSamples
+ }
+ return res
+ }
- let _res = []
-
- if (Array.isArray(example)) {
- example.forEach((v)=>{
- items.example = v
- _res.push(sampleXmlFromSchema(items, config))
- })
- return _res
- } else if (Array.isArray(defaultValue)) {
- defaultValue.forEach((v)=>{
- items.default = v
- _res.push(sampleXmlFromSchema(items, config))
- })
- return _res
+ // generate xml sample recursively for object case
+ if(type === "object") {
+ for (let propName in sample) {
+ if (!sample.hasOwnProperty(propName)) {
+ continue
+ }
+ if (schema && props[propName] && props[propName].readOnly && !includeReadOnly) {
+ continue
+ }
+ if (schema && props[propName] && props[propName].writeOnly && !includeWriteOnly) {
+ continue
+ }
+ if (schema && props[propName] && props[propName].xml && props[propName].xml.attribute) {
+ _attr[props[propName].xml.name || propName] = example[propName]
+ continue
+ }
+ addPropertyToResult(propName, sample[propName])
+ }
+ if (!isEmpty(_attr)) {
+ res[displayName].push({_attr: _attr})
}
- return sampleXmlFromSchema(items, config)
+ return res
}
+
+ res[displayName] = !isEmpty(_attr) ? [{_attr: _attr}, sample] : sample
+ return res
}
- if (type === "object") {
- let props = objectify(properties)
- res[displayName] = []
- example = example || {}
+ // use schema to generate sample
+ if(type === "object") {
for (let propName in props) {
if (!props.hasOwnProperty(propName)) {
continue
}
- if ( props[propName].readOnly && !includeReadOnly ) {
+ if ( props[propName] && props[propName].deprecated ) {
+ continue
+ }
+ if ( props[propName] && props[propName].readOnly && !includeReadOnly ) {
continue
}
- if ( props[propName].writeOnly && !includeWriteOnly ) {
+ if ( props[propName] && props[propName].writeOnly && !includeWriteOnly ) {
continue
}
+ addPropertyToResult(propName)
+ }
- props[propName].xml = props[propName].xml || {}
+ if ( additionalProperties === true ) {
+ if(respectXML) {
+ res[displayName].push({additionalProp: "Anything can be here"})
+ } else {
+ res.additionalProp1 = {}
+ }
+ } else if ( additionalProperties ) {
+ const additionalProps = objectify(additionalProperties)
+ const additionalPropSample = sampleFromSchemaGeneric(additionalProps, config, undefined, respectXML)
- if (props[propName].xml.attribute) {
- let enumAttrVal = Array.isArray(props[propName].enum) && props[propName].enum[0]
- let attrExample = props[propName].example
- let attrDefault = props[propName].default
- _attr[props[propName].xml.name || propName] = attrExample!== undefined && attrExample
- || example[propName] !== undefined && example[propName] || attrDefault !== undefined && attrDefault
- || enumAttrVal || primitive(props[propName])
+ if(respectXML && additionalProps.xml && additionalProps.xml.name && additionalProps.xml.name !== "notagname")
+ {
+ res[displayName].push(additionalPropSample)
} else {
- props[propName].xml.name = props[propName].xml.name || propName
- if(props[propName].example === undefined && example[propName] !== undefined) {
- props[propName].example = example[propName]
- }
- let t = sampleXmlFromSchema(props[propName])
- if (Array.isArray(t)) {
- res[displayName] = res[displayName].concat(t)
- } else {
- res[displayName].push(t)
+ for (let i = 1; i < 4; i++) {
+ if(respectXML) {
+ const temp = {}
+ temp["additionalProp" + i] = additionalPropSample["notagname"]
+ res[displayName].push(temp)
+ } else {
+ res["additionalProp" + i] = additionalPropSample
+ }
}
-
}
}
-
- if (additionalProperties === true) {
- res[displayName].push({additionalProp: "Anything can be here"})
- } else if (additionalProperties) {
- res[displayName].push({additionalProp: primitive(additionalProperties)})
- }
-
- if (_attr) {
+ if (respectXML && _attr) {
res[displayName].push({_attr: _attr})
}
+
return res
}
- if (example !== undefined) {
- value = example
- } else if (defaultValue !== undefined) {
- //display example if exists
- value = defaultValue
- } else if (Array.isArray(enumValue)) {
+ if(type === "array") {
+ let sampleArray
+ if(respectXML) {
+ items.xml = items.xml || schema.xml || {}
+ items.xml.name = items.xml.name || xml.name
+ }
+ if(Array.isArray(items.anyOf)) {
+ sampleArray = items.anyOf.map(i => sampleFromSchemaGeneric(liftSampleHelper(items, i), config, undefined, respectXML))
+ } else if(Array.isArray(items.oneOf)) {
+ sampleArray = items.oneOf.map(i => sampleFromSchemaGeneric(liftSampleHelper(items, i), config, undefined, respectXML))
+ } else if(!respectXML || respectXML && xml.wrapped) {
+ sampleArray = [sampleFromSchemaGeneric(items, config, undefined, respectXML)]
+ } else {
+ return sampleFromSchemaGeneric(items, config, undefined, respectXML)
+ }
+ if(respectXML && xml.wrapped) {
+ res[displayName] = sampleArray
+ if (!isEmpty(_attr)) {
+ res[displayName].push({_attr: _attr})
+ }
+ return res
+ }
+ return sampleArray
+ }
+
+ let value
+ if (schema && Array.isArray(schema.enum)) {
//display enum first value
- value = enumValue[0]
- } else {
- //set default value
+ value = normalizeArray(schema.enum)[0]
+ } else if(schema) {
+ // display schema default
value = primitive(schema)
+ } else {
+ return
+ }
+ if (type === "file") {
+ return
}
- res[displayName] = _attr ? [{_attr: _attr}, value] : value
+ if(respectXML) {
+ res[displayName] = !isEmpty(_attr) ? [{_attr: _attr}, value] : value
+ return res
+ }
- return res
+ return value
}
-export function createXMLExample(schema, config) {
- let json = sampleXmlFromSchema(schema, config)
+export const inferSchema = (thing) => {
+ if(thing.schema)
+ thing = thing.schema
+
+ if(thing.properties) {
+ thing.type = "object"
+ }
+
+ return thing // Hopefully this will have something schema like in it... `type` for example
+}
+
+export const createXMLExample = (schema, config={}, o) => {
+ const json = sampleFromSchemaGeneric(schema, config, o, true)
if (!json) { return }
return XML(json, { declaration: true, indent: "\t" })
}
+export const sampleFromSchema = (schema, config={}, o) =>
+ sampleFromSchemaGeneric(schema, config, o, false)
+
export const memoizedCreateXMLExample = memoizee(createXMLExample)
export const memoizedSampleFromSchema = memoizee(sampleFromSchema)
diff --git a/src/core/utils.js b/src/core/utils.js
index ff840e4e4db..e362aaac1a4 100644
--- a/src/core/utils.js
+++ b/src/core/utils.js
@@ -1,4 +1,4 @@
-/*
+/*
ATTENTION! This file (but not the functions within) is deprecated.
You should probably add a new file to `./helpers/` instead of adding a new
@@ -133,7 +133,7 @@ export function createObjWithHashedKeys (fdObj) {
trackKeys[pair[0]].length += 1
let hashedKeyCurrent = `${pair[0]}${hashIdx}${trackKeys[pair[0]].length}`
newObj[hashedKeyCurrent] = pair[1]
- }
+ }
}
return newObj
}
@@ -263,13 +263,13 @@ export function extractFileNameFromContentDispositionHeader(value){
/filename="([^;]*);?"/i,
/filename=([^;]*);?/i
]
-
+
let responseFilename
patterns.some(regex => {
responseFilename = regex.exec(value)
return responseFilename !== null
})
-
+
if (responseFilename !== null && responseFilename.length > 1) {
try {
return decodeURIComponent(responseFilename[1])
@@ -541,8 +541,8 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
return errors
}
-const getXmlSampleSchema = (schema, config) => {
- if (!schema.xml || !schema.xml.name) {
+const getXmlSampleSchema = (schema, config, exampleOverride) => {
+ if (schema && (!schema.xml || !schema.xml.name)) {
schema.xml = schema.xml || {}
if (schema.$$ref) {
@@ -554,7 +554,7 @@ const getXmlSampleSchema = (schema, config) => {
return null
}
}
- return memoizedCreateXMLExample(schema, config)
+ return memoizedCreateXMLExample(schema, config, exampleOverride)
}
const shouldStringifyTypesConfig = [
@@ -566,8 +566,8 @@ const shouldStringifyTypesConfig = [
const defaultStringifyTypes = ["object"]
-const getStringifiedSampleForSchema = (schema, config, contentType) => {
- const res = memoizedSampleFromSchema(schema, config)
+const getStringifiedSampleForSchema = (schema, config, contentType, exampleOverride) => {
+ const res = memoizedSampleFromSchema(schema, config, exampleOverride)
const resType = typeof res
const typesToStringify = shouldStringifyTypesConfig.reduce(
@@ -581,12 +581,17 @@ const getStringifiedSampleForSchema = (schema, config, contentType) => {
: res
}
-export const getSampleSchema = (schema, contentType="", config={}) => {
+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)
+ return getXmlSampleSchema(schema, config, exampleOverride)
}
- return getStringifiedSampleForSchema(schema, config, contentType)
+ return getStringifiedSampleForSchema(schema, config, contentType, exampleOverride)
}
export const parseSearch = () => {
@@ -766,7 +771,7 @@ export function paramToIdentifier(param, { returnAll = false, allowHashes = true
}
const paramName = param.get("name")
const paramIn = param.get("in")
-
+
let generatedIdentifiers = []
// Generate identifiers in order of most to least specificity
@@ -774,7 +779,7 @@ export function paramToIdentifier(param, { returnAll = false, allowHashes = true
if (param && param.hashCode && paramIn && paramName && allowHashes) {
generatedIdentifiers.push(`${paramIn}.${paramName}.hash-${param.hashCode()}`)
}
-
+
if(paramIn && paramName) {
generatedIdentifiers.push(`${paramIn}.${paramName}`)
}
diff --git a/test/e2e-cypress/static/documents/bugs/4943.yaml b/test/e2e-cypress/static/documents/bugs/4943.yaml
new file mode 100644
index 00000000000..f1a8a84217d
--- /dev/null
+++ b/test/e2e-cypress/static/documents/bugs/4943.yaml
@@ -0,0 +1,43 @@
+openapi: 3.0.0
+info:
+ description: Test API
+ version: v1
+ title: Test API
+tags:
+ - name: Test
+ description: Test API
+servers:
+ - url: /v1
+paths:
+ /test:
+ post:
+ tags:
+ - Test
+ summary: Test endpoint
+ description: Test
+ operationId: postTest
+ responses:
+ '200':
+ description: Returns response
+ content:
+ application/xml:
+ schema:
+ $ref: '#/components/schemas/test'
+components:
+ schemas:
+ test:
+ type: object
+ properties:
+ a:
+ type: string
+ b:
+ type: integer
+ c:
+ oneOf:
+ - type: object
+ - type: array
+ items:
+ type: string
+ - type: boolean
+ - type: integer
+ - type: number
diff --git a/test/e2e-cypress/static/documents/bugs/6540.yaml b/test/e2e-cypress/static/documents/bugs/6540.yaml
new file mode 100644
index 00000000000..bea61842f0b
--- /dev/null
+++ b/test/e2e-cypress/static/documents/bugs/6540.yaml
@@ -0,0 +1,85 @@
+openapi: 3.0.0
+info:
+ description: Test API
+ version: v1
+ title: Test API
+tags:
+ - name: Test
+ description: Test API
+servers:
+ - url: /v1
+paths:
+ /test:
+ post:
+ tags:
+ - Test
+ summary: Test endpoint
+ description: Test
+ operationId: postTest
+ responses:
+ '200':
+ description: Returns response
+ content:
+ application/xml:
+ schema:
+ $ref: '#/components/schemas/test'
+components:
+ schemas:
+ test:
+ type: object
+ properties:
+ a:
+ type: string
+ b:
+ type: integer
+ c:
+ type: array
+ items:
+ $ref: '#/components/schemas/Things'
+ d:
+ type: array
+ items:
+ anyOf:
+ - $ref: '#/components/schemas/TextObject'
+ - $ref: '#/components/schemas/ImageObject'
+
+
+ Things:
+ type: object
+ oneOf:
+ - $ref: '#/components/schemas/TextObject'
+ - $ref: '#/components/schemas/ImageObject'
+
+ TextObject:
+ required:
+ - data
+ type: object
+ properties:
+ objectType:
+ type: string
+ example: Text
+ xml:
+ name: ObjectType
+ data:
+ type: string
+ example: This is a text
+ xml:
+ name: Data
+ description: Contains a text
+
+ ImageObject:
+ required:
+ - data
+ type: object
+ properties:
+ objectType:
+ type: string
+ example: image
+ xml:
+ name: ObjectType
+ data:
+ type: string
+ example: This is a image
+ xml:
+ name: Data
+ description: Contains a image
diff --git a/test/e2e-cypress/tests/bugs/4943.js b/test/e2e-cypress/tests/bugs/4943.js
new file mode 100644
index 00000000000..5359754511f
--- /dev/null
+++ b/test/e2e-cypress/tests/bugs/4943.js
@@ -0,0 +1,20 @@
+describe("#4943: XML example not rendered correctly with oneOf", () => {
+ it("should render integer property correctly", () => {
+ cy
+ .visit("/?url=/documents/bugs/4943.yaml")
+ .get("#operations-Test-postTest")
+ .click()
+ .get(".microlight")
+ .contains("0")
+ })
+ it("should render oneOf property correctly", () => {
+ cy
+ .visit("/?url=/documents/bugs/4943.yaml")
+ .get("#operations-Test-postTest")
+ .click()
+ .get(".try-out__btn")
+ .click()
+ .get(".microlight")
+ .contains("\n\t")
+ })
+})
diff --git a/test/e2e-cypress/tests/bugs/6540.js b/test/e2e-cypress/tests/bugs/6540.js
new file mode 100644
index 00000000000..62f3948e049
--- /dev/null
+++ b/test/e2e-cypress/tests/bugs/6540.js
@@ -0,0 +1,11 @@
+describe("#6540: XML example not rendered correctly with oneOf", () => {
+ it("should render xml like json", () => {
+ const expected = "\n\n\tstring\n\t0\n\t\n\t\tText\n\t\tThis is a text\n\t\n\t\n\t\timage\n\t\tThis is a image\n\t\n\t\n\t\tText\n\t\tThis is a text\n\t\n\t\n\t\timage\n\t\tThis is a image\n\t\n"
+ cy
+ .visit("/?url=/documents/bugs/6540.yaml")
+ .get("#operations-Test-postTest")
+ .click()
+ .get(".microlight")
+ .contains(expected)
+ })
+})
diff --git a/test/unit/core/plugins/samples/fn.js b/test/unit/core/plugins/samples/fn.js
index dc439c72815..353cc0e3990 100644
--- a/test/unit/core/plugins/samples/fn.js
+++ b/test/unit/core/plugins/samples/fn.js
@@ -512,6 +512,26 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toEqual(expected)
})
})
+
+ it("should use overrideExample when defined", () => {
+ const definition = {
+ type: "object",
+ properties: {
+ foo: {
+ type: "string"
+ }
+ },
+ example: {
+ foo: null
+ }
+ }
+
+ const expected = {
+ foo: "override"
+ }
+
+ expect(sampleFromSchema(definition, {}, expected)).toEqual(expected)
+ })
})
describe("createXMLExample", function () {
@@ -1337,7 +1357,7 @@ describe("createXMLExample", function () {
})
it("returns object with additional props", function () {
- let expected = "\n\n\tstring\n\tstring\n"
+ let expected = "\n\n\tstring\n\tstring\n\tstring\n\tstring\n"
let definition = {
type: "object",
properties: {
@@ -1394,7 +1414,7 @@ describe("createXMLExample", function () {
})
it("returns object with additional props with no type passed", function () {
- let expected = "\n\n\tstring\n"
+ let expected = "\n\n\tstring\n\tstring\n\tstring\n"
let definition = {
additionalProperties: {
type: "string"
@@ -1406,5 +1426,34 @@ describe("createXMLExample", function () {
expect(sut(definition)).toEqual(expected)
})
+
+
+ it("should use overrideExample when defined", () => {
+ const expected = "\n\n\toverride\n"
+
+ const definition = {
+ type: "object",
+ properties: {
+ foo: {
+ type: "string",
+ xml: {
+ name: "foo"
+ }
+ }
+ },
+ example: {
+ foo: null
+ },
+ xml: {
+ name: "bar"
+ }
+ }
+
+ const overrideExample = {
+ foo: "override"
+ }
+
+ expect(sut(definition, {}, overrideExample)).toEqual(expected)
+ })
})
})