From e95fde8e561f2c7f1e2bc3c4a3bf0ba0a0326af1 Mon Sep 17 00:00:00 2001 From: Standa Opichal Date: Thu, 29 Nov 2018 12:55:45 +0100 Subject: [PATCH 1/3] fix(json-schema): always pass swagger --- src/json-schema.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/json-schema.js b/src/json-schema.js index 624caa9..b644580 100644 --- a/src/json-schema.js +++ b/src/json-schema.js @@ -77,6 +77,8 @@ function convertSubSchema(schema, references, swagger) { return { $ref: schema.$ref }; } + const recurseConvertSubSchema = s => convertSubSchema(s, references, swagger); + let actualSchema = _.omit(schema, ['discriminator', 'readOnly', 'xml', 'externalDocs', 'example']); actualSchema = _.omitBy(actualSchema, isExtension); @@ -102,52 +104,52 @@ function convertSubSchema(schema, references, swagger) { } if (schema.allOf) { - actualSchema.allOf = schema.allOf.map(s => convertSubSchema(s, references)); + actualSchema.allOf = schema.allOf.map(recurseConvertSubSchema); } if (schema.anyOf) { - actualSchema.anyOf = schema.anyOf.map(s => convertSubSchema(s, references)); + actualSchema.anyOf = schema.anyOf.map(recurseConvertSubSchema); } if (schema.oneOf) { - actualSchema.oneOf = schema.oneOf.map(s => convertSubSchema(s, references)); + actualSchema.oneOf = schema.oneOf.map(recurseConvertSubSchema); } if (schema.not) { - actualSchema.not = convertSubSchema(schema.not, references); + actualSchema.not = recurseConvertSubSchema(schema.not); } // Array if (schema.items) { if (Array.isArray(schema.items)) { - actualSchema.items = schema.items.map(s => convertSubSchema(s, references)); + actualSchema.items = schema.items.map(recurseConvertSubSchema); } else { - actualSchema.items = convertSubSchema(schema.items, references); + actualSchema.items = recurseConvertSubSchema(schema.items); } } if (schema.additionalItems && typeof schema.additionalItems === 'object') { - actualSchema.additionalItems = convertSubSchema(schema.additionalItems, references); + actualSchema.additionalItems = recurseConvertSubSchema(schema.additionalItems); } // Object if (schema.properties) { Object.keys(schema.properties).forEach((key) => { - actualSchema.properties[key] = convertSubSchema(schema.properties[key], references); + actualSchema.properties[key] = recurseConvertSubSchema(schema.properties[key]); }); } if (schema.patternProperties) { Object.keys(schema.patternProperties).forEach((key) => { actualSchema.patternProperties[key] = - convertSubSchema(schema.patternProperties[key], references); + recurseConvertSubSchema(schema.patternProperties[key]); }); } if (schema.additionalProperties && typeof schema.additionalProperties === 'object') { - actualSchema.additionalProperties = convertSubSchema(schema.additionalProperties, references); + actualSchema.additionalProperties = recurseConvertSubSchema(schema.additionalProperties); } return actualSchema; From fbf8d2e06436bdeed7807625034591ce405873c4 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Thu, 29 Nov 2018 11:12:38 -0800 Subject: [PATCH 2/3] fix: Support example references in definitions --- CHANGELOG.md | 7 +++++++ src/json-schema.js | 1 + test/json-schema.js | 46 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9ae8bd..8ba2e32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Fury Swagger Parser Changelog +## Master + +### Bug Fixes + +- Fixes a regression introduced in 0.22.3 which caused a reference (`$ref`) + from a definition to another definitions example value to fail. + ## 0.22.3 (2018-11-27) ### Bug Fixes diff --git a/src/json-schema.js b/src/json-schema.js index b644580..b7232bf 100644 --- a/src/json-schema.js +++ b/src/json-schema.js @@ -81,6 +81,7 @@ function convertSubSchema(schema, references, swagger) { let actualSchema = _.omit(schema, ['discriminator', 'readOnly', 'xml', 'externalDocs', 'example']); actualSchema = _.omitBy(actualSchema, isExtension); + actualSchema = _.cloneDeep(actualSchema); if (schema.type === 'file') { // file is not a valid JSON Schema type let's pick string instead diff --git a/test/json-schema.js b/test/json-schema.js index 73c7e0c..c4199ca 100644 --- a/test/json-schema.js +++ b/test/json-schema.js @@ -588,5 +588,51 @@ describe('Swagger Schema to JSON Schema', () => { }, }); }); + + it('can convert a Swagger Schema definition with a reference to Swagger Schema example', () => { + // This test came from a regression that caused the User schema to be + // mutated during the Swagger Schema to JSON Schema conversion and + // thus breaking the reference to `example` (`examples` in JSON Schema) + + const result = convertSchemaDefinitions({ + User: { + type: 'object', + properties: { + name: { + type: 'string', + example: 'Doe', + }, + }, + }, + Comment: { + type: 'object', + example: { + name: { + $ref: '#/definitions/User/properties/name/example', + }, + }, + }, + }); + + expect(result).to.deep.equal({ + User: { + type: 'object', + properties: { + name: { + type: 'string', + examples: ['Doe'], + }, + }, + }, + Comment: { + type: 'object', + examples: [ + { + name: 'Doe', + }, + ], + }, + }); + }); }); }); From 22f5a272ce1b6b1c127b55bfd9ad8bdf2f52ab11 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Thu, 29 Nov 2018 11:20:27 -0800 Subject: [PATCH 3/3] chore: Release 0.22.4 --- CHANGELOG.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ba2e32..7e11fec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Fury Swagger Parser Changelog -## Master +## 0.22.4 (2018-11-29) ### Bug Fixes diff --git a/package.json b/package.json index 02527c6..38658f9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fury-adapter-swagger", - "version": "0.22.3", + "version": "0.22.4", "description": "Swagger 2.0 parser for Fury.js", "main": "./lib/adapter.js", "tonicExampleFilename": "tonic-example.js",