Skip to content

Commit

Permalink
fix: normalize Swagger definitions before resolving a subtree (#1274)
Browse files Browse the repository at this point in the history
* normalize before subtree resolution; add $$normalized meta tag

* add $$normalized marker to tests
  • Loading branch information
shockey authored Apr 4, 2018
1 parent c2feb09 commit 1796331
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function normalizeSwagger(parsedSpec) {
const {paths} = spec
const map = {}

if (!paths) {
if (!paths || spec.$$normalized) {
return parsedSpec
}

Expand Down Expand Up @@ -206,5 +206,7 @@ export function normalizeSwagger(parsedSpec) {
}
}

spec.$$normalized = true

return parsedSpec
}
8 changes: 7 additions & 1 deletion src/subtree-resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import get from 'lodash/get'
import resolve from '../resolver'
import {normalizeSwagger} from '../helpers'

export default async function resolveSubtree(obj, path, opts = {}) {
const {
Expand All @@ -44,10 +45,15 @@ export default async function resolveSubtree(obj, path, opts = {}) {
modelPropertyMacro
}

const {spec: normalized} = normalizeSwagger({
spec: obj
})

const result = await resolve({
...resolveOptions,
spec: obj,
spec: normalized,
allowMetaPatches: true,
skipNormalization: true
})

if (!returnEntireTree && Array.isArray(path) && path.length) {
Expand Down
8 changes: 8 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ describe('helpers', function () {
const resultSpec = normalizeSwagger(spec)

expect(resultSpec).toEqual({spec: {
$$normalized: true,
consumes: ['application/json'],
paths: {
'/two': {
Expand All @@ -374,6 +375,7 @@ describe('helpers', function () {
const resultSpec = normalizeSwagger(spec)

expect(resultSpec).toEqual({spec: {
$$normalized: true,
consumes: ['application/json'],
paths: {
'/two': {
Expand Down Expand Up @@ -402,6 +404,7 @@ describe('helpers', function () {
const resultSpec = normalizeSwagger(spec)

expect(resultSpec).toEqual({spec: {
$$normalized: true,
produces: ['application/json'],
paths: {
'/two': {
Expand All @@ -427,6 +430,7 @@ describe('helpers', function () {
const resultSpec = normalizeSwagger(spec)

expect(resultSpec).toEqual({spec: {
$$normalized: true,
produces: ['application/json'],
paths: {
'/two': {
Expand Down Expand Up @@ -455,6 +459,7 @@ describe('helpers', function () {
const resultSpec = normalizeSwagger(spec)

expect(resultSpec).toEqual({spec: {
$$normalized: true,
security: ['test'],
paths: {
'/two': {
Expand All @@ -480,6 +485,7 @@ describe('helpers', function () {
const resultSpec = normalizeSwagger(spec)

expect(resultSpec).toEqual({spec: {
$$normalized: true,
security: ['test1'],
paths: {
'/two': {
Expand All @@ -506,6 +512,7 @@ describe('helpers', function () {
const resultSpec = normalizeSwagger(spec)

expect(resultSpec).toEqual({spec: {
$$normalized: true,
paths: {
'/two': {
parameters: [{name: 'a', in: 'path'}],
Expand Down Expand Up @@ -538,6 +545,7 @@ describe('helpers', function () {
const resultSpec = normalizeSwagger(spec)

expect(resultSpec).toEqual({spec: {
$$normalized: true,
paths: {
'/two': {
parameters: [
Expand Down
1 change: 1 addition & 0 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ describe('resolver', () => {
expect(obj.errors).toEqual([])
expect(obj.spec).toEqual({
swagger: '2.0',
$$normalized: true,
paths: {
'/pet': {
post: {
Expand Down
206 changes: 206 additions & 0 deletions test/subtree-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ describe('subtree $ref resolver', function () {
expect(res).toEqual({
errors: [],
spec: {
$$normalized: true,
swagger: '2.0',
consumes: ['application/json'],
paths: {
Expand Down Expand Up @@ -202,6 +203,7 @@ describe('subtree $ref resolver', function () {
expect(res).toEqual({
errors: [],
spec: {
$$normalized: true,
swagger: '2.0',
produces: ['application/json'],
paths: {
Expand All @@ -215,6 +217,210 @@ describe('subtree $ref resolver', function () {
}
})
})
it('should normalize Swagger 2.0 parameters', async () => {
const input = {
swagger: '2.0',
parameters: {
petId: {
name: 'petId',
in: 'path',
description: 'ID of pet to return',
required: true,
type: 'integer',
format: 'int64'
}
},
paths: {
'/': {
parameters: [
{
$ref: '#/parameters/petId'
}
],
get: {
parameters: [
{
name: 'name',
in: 'formData',
description: 'Updated name of the pet',
required: false,
type: 'string'
},
{
name: 'status',
in: 'formData',
description: 'Updated status of the pet',
required: false,
type: 'string'
}
]
}
}
}
}

const res = await resolve(input, ['paths', '/', 'get'], {
returnEntireTree: true
})

expect(res).toEqual({
errors: [],
spec: {
$$normalized: true,
swagger: '2.0',
parameters: {
petId: {
name: 'petId',
in: 'path',
description: 'ID of pet to return',
required: true,
type: 'integer',
format: 'int64'
}
},
paths: {
'/': {
parameters: [
{
$ref: '#/parameters/petId'
}
],
get: {
parameters: [
{
name: 'name',
in: 'formData',
description: 'Updated name of the pet',
required: false,
type: 'string'
},
{
name: 'status',
in: 'formData',
description: 'Updated status of the pet',
required: false,
type: 'string'
},
{
name: 'petId',
in: 'path',
description: 'ID of pet to return',
required: true,
type: 'integer',
format: 'int64',
$$ref: '#/parameters/petId'
}
]
}
}
}
}
})
})
it('should normalize idempotently', async () => {
const input = {
swagger: '2.0',
parameters: {
petId: {
name: 'petId',
in: 'path',
description: 'ID of pet to return',
required: true,
type: 'integer',
format: 'int64'
}
},
paths: {
'/': {
parameters: [
{
$ref: '#/parameters/petId'
}
],
get: {
parameters: [
{
name: 'name',
in: 'formData',
description: 'Updated name of the pet',
required: false,
type: 'string'
},
{
name: 'status',
in: 'formData',
description: 'Updated status of the pet',
required: false,
type: 'string'
}
]
}
}
}
}

const intermediate = await resolve(input, ['paths', '/', 'get'], {
returnEntireTree: true
})

const res = await resolve(intermediate.spec, ['paths', '/', 'get'], {
returnEntireTree: true
})

expect(res).toEqual({
errors: [],
spec: {
swagger: '2.0',
$$normalized: true,
parameters: {
petId: {
name: 'petId',
in: 'path',
description: 'ID of pet to return',
required: true,
type: 'integer',
format: 'int64'
}
},
paths: {
'/': {
parameters: [
{
$ref: '#/parameters/petId'
}
],
get: {
parameters: [
{
name: 'name',
in: 'formData',
description: 'Updated name of the pet',
required: false,
type: 'string'
},
{
name: 'status',
in: 'formData',
description: 'Updated status of the pet',
required: false,
type: 'string'
},
{
name: 'petId',
in: 'path',
description: 'ID of pet to return',
required: true,
type: 'integer',
format: 'int64',
$$ref: '#/parameters/petId'
}
]
}
}
}
}
})
})
it('should resolve complex allOf correctly', async () => {
const input = {
definitions: {
Expand Down

0 comments on commit 1796331

Please sign in to comment.