Skip to content
This repository has been archived by the owner on Jul 2, 2019. It is now read-only.

Commit

Permalink
Merge pull request #227 from apiaryio/kylef/mutate-regression
Browse files Browse the repository at this point in the history
Support example references in definitions
  • Loading branch information
kylef authored Nov 29, 2018
2 parents f9fde54 + 22f5a27 commit 1bb0f3a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Fury Swagger Parser Changelog

## 0.22.4 (2018-11-29)

### 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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
23 changes: 13 additions & 10 deletions src/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ 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);
actualSchema = _.cloneDeep(actualSchema);

if (schema.type === 'file') {
// file is not a valid JSON Schema type let's pick string instead
Expand All @@ -102,52 +105,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;
Expand Down
46 changes: 46 additions & 0 deletions test/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
],
},
});
});
});
});

0 comments on commit 1bb0f3a

Please sign in to comment.