From f9e54a26bf7f4d755d170b0b5f76373214374b8b Mon Sep 17 00:00:00 2001 From: Mahtis Michel Date: Wed, 10 Mar 2021 20:18:54 +0100 Subject: [PATCH] fix(sample-gen): allOf, oneOf lifting should consider properties and items (#7041) --- src/core/plugins/samples/fn.js | 48 +++++++- test/unit/core/plugins/samples/fn.js | 163 +++++++++++++++++++++++++++ 2 files changed, 206 insertions(+), 5 deletions(-) diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js index 2077c65b..9dd9b9ea 100644 --- a/src/core/plugins/samples/fn.js +++ b/src/core/plugins/samples/fn.js @@ -35,12 +35,12 @@ const primitive = (schema) => { const sanitizeRef = (value) => deeplyStripKey(value, "$$ref", (val) => typeof val === "string" && val.indexOf("#") > -1) -const liftSampleHelper = (oldSchema, target) => { +const liftSampleHelper = (oldSchema, target, config = {}) => { if(target.example === undefined && oldSchema.example !== undefined) { target.example = oldSchema.example } if(target.default === undefined && oldSchema.default !== undefined) { - target.default = oldSchema.default + target.default = oldSchema.defaultfn } if(target.enum === undefined && oldSchema.enum !== undefined) { target.enum = oldSchema.enum @@ -51,6 +51,43 @@ const liftSampleHelper = (oldSchema, target) => { if(target.type === undefined && oldSchema.type !== undefined) { target.type = oldSchema.type } + if(oldSchema.properties) { + if(!target.properties) { + target.properties = {} + } + let props = objectify(oldSchema.properties) + for (let propName in props) { + if (!props.hasOwnProperty(propName)) { + continue + } + if ( props[propName] && props[propName].deprecated ) { + continue + } + if ( props[propName] && props[propName].readOnly && !config.includeReadOnly ) { + continue + } + if ( props[propName] && props[propName].writeOnly && !config.includeWriteOnly ) { + continue + } + if(!target.properties[propName]) { + target.properties[propName] = props[propName] + if(!oldSchema.required && Array.isArray(oldSchema.required) && oldSchema.required.indexOf(propName) !== -1) { + if(!target.required) { + target.required = [propName] + } else { + target.required.push(propName) + } + } + } + } + } + if(oldSchema.items) { + if(!target.items) { + target.items = {} + } + target.items = liftSampleHelper(oldSchema.items, target.items, config) + } + return target } @@ -65,7 +102,7 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und ? schema.oneOf[0] : schema.anyOf[0] ) - liftSampleHelper(schemaToAdd, schema) + liftSampleHelper(schemaToAdd, schema, config) if(!schema.xml && schemaToAdd.xml) { schema.xml = schemaToAdd.xml } @@ -348,10 +385,11 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und 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)) + sampleArray = items.anyOf.map(i => sampleFromSchemaGeneric(liftSampleHelper(items, i, config), config, undefined, respectXML)) } else if(Array.isArray(items.oneOf)) { - sampleArray = items.oneOf.map(i => sampleFromSchemaGeneric(liftSampleHelper(items, i), config, undefined, respectXML)) + sampleArray = items.oneOf.map(i => sampleFromSchemaGeneric(liftSampleHelper(items, i, config), config, undefined, respectXML)) } else if(!respectXML || respectXML && xml.wrapped) { sampleArray = [sampleFromSchemaGeneric(items, config, undefined, respectXML)] } else { diff --git a/test/unit/core/plugins/samples/fn.js b/test/unit/core/plugins/samples/fn.js index cbda7eec..c519c187 100644 --- a/test/unit/core/plugins/samples/fn.js +++ b/test/unit/core/plugins/samples/fn.js @@ -607,6 +607,169 @@ describe("sampleFromSchema", () => { expect(sampleFromSchema(definition, {}, expected)).toEqual(expected) }) + + it("should merge properties with anyOf", () => { + const definition = { + type: "object", + properties: { + foo: { + type: "string" + } + }, + anyOf: [ + { + type: "object", + properties: { + bar: { + type: "boolean" + } + } + } + ] + } + + const expected = { + foo: "string", + bar: true + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("should merge array item properties with anyOf", () => { + const definition = { + type: "array", + items: { + type: "object", + properties: { + foo: { + type: "string" + } + }, + anyOf: [ + { + type: "object", + properties: { + bar: { + type: "boolean" + } + } + } + ] + } + } + + const expected = [ + { + foo: "string", + bar: true + } + ] + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("should merge properties with oneOf", () => { + const definition = { + type: "object", + properties: { + foo: { + type: "string" + } + }, + oneOf: [ + { + type: "object", + properties: { + bar: { + type: "boolean" + } + } + } + ] + } + + const expected = { + foo: "string", + bar: true + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("should merge array item properties with oneOf", () => { + const definition = { + type: "array", + items: { + type: "object", + properties: { + foo: { + type: "string" + } + }, + oneOf: [ + { + type: "object", + properties: { + bar: { + type: "boolean" + } + } + } + ] + } + } + + const expected = [ + { + foo: "string", + bar: true + } + ] + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("should lift items with anyOf", () => { + const definition = { + type: "array", + anyOf: [ + { + type: "array", + items: { + type: "boolean" + } + } + ] + } + + const expected = [ + true + ] + + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("should lift items with oneOf", () => { + const definition = { + type: "array", + oneOf: [ + { + type: "array", + items: { + type: "boolean" + } + } + ] + } + + const expected = [ + true + ] + + expect(sampleFromSchema(definition)).toEqual(expected) + }) }) describe("createXMLExample", function () {