Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 19 additions & 1 deletion src/core/plugins/json-schema-2020-12-samples/fn/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export const sampleFromSchemaGeneric = (
}
}
const _attr = {}
let { xml, properties, additionalProperties, items, contains } = schema || {}
let { xml, properties, additionalProperties, items, contains, prefixItems } =
schema || {}
let type = getType(schema)
let { includeReadOnly, includeWriteOnly } = config
xml = xml || {}
Expand Down Expand Up @@ -380,6 +381,23 @@ export const sampleFromSchemaGeneric = (
}
}

if (Array.isArray(prefixItems) && prefixItems.length > 0) {
sampleArray.push(
...prefixItems.map((prefixItemSchema) => {
if (respectXML && isJSONSchemaObject(prefixItemSchema)) {
prefixItemSchema.xml = prefixItemSchema.xml || schema.xml || {}
prefixItemSchema.xml.name = prefixItemSchema.xml.name || xml.name
}
return sampleFromSchemaGeneric(
prefixItemSchema,
config,
undefined,
respectXML
)
})
)
}

if (isJSONSchemaObject(items)) {
if (respectXML) {
items.xml = items.xml || schema.xml || {}
Expand Down
17 changes: 17 additions & 0 deletions test/e2e-cypress/e2e/bugs/10400.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @prettier
*/

describe("#10400: prefixItems generates non-null sample values", () => {
it("should generate a tuple sample using prefixItems schemas", () => {
cy.visit("/?url=/documents/bugs/10400.yaml")
.get("#operations-default-postTuple")
.click()
.get(".body-param__example")
.should("exist")
.should((el) => {
const normalized = el.text().replace(/\s+/g, " ")
expect(normalized).to.contain('"tupleField": [ 0, "string" ]')
})
})
})
30 changes: 30 additions & 0 deletions test/e2e-cypress/static/documents/bugs/10400.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
openapi: 3.1.0
info:
title: prefixItems sample
version: 1.0.0
paths:
/tuple:
post:
operationId: postTuple
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Tuple"
responses:
"200":
description: OK
components:
schemas:
Tuple:
type: object
required:
- tupleField
properties:
tupleField:
type: array
minItems: 2
maxItems: 2
prefixItems:
- type: integer
- type: string
51 changes: 51 additions & 0 deletions test/unit/core/plugins/json-schema-2020-12-samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,57 @@ describe("sampleFromSchema", () => {

expect(sampleFromSchema(definition)).toEqual(expected)
})
it("generates samples for prefixItems without items", () => {
const definition = {
type: "array",
prefixItems: [{ type: "integer" }, { type: "string" }],
minItems: 2,
maxItems: 2,
}

const expected = [0, "string"]

expect(sampleFromSchema(definition)).toStrictEqual(expected)
})

it("generates samples for prefixItems with object schemas", () => {
const definition = {
type: "array",
prefixItems: [
{
type: "object",
properties: {
c1: { type: "string" },
},
},
{
type: "object",
properties: {
c3: { type: "string" },
},
},
],
minItems: 2,
maxItems: 2,
}

const expected = [{ c1: "string" }, { c3: "string" }]

expect(sampleFromSchema(definition)).toStrictEqual(expected)
})

it("appends items sample after prefixItems samples", () => {
const definition = {
type: "array",
prefixItems: [{ type: "integer" }],
items: { type: "string" },
minItems: 2,
}

const expected = [0, "string"]

expect(sampleFromSchema(definition)).toStrictEqual(expected)
})
})

describe("discriminator mapping example", () => {
Expand Down