Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the creation of schema objects originated from allOf keyword #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
41 changes: 36 additions & 5 deletions src/swaggerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,8 @@ function createParamGroupSchemas(parameters) {
const params = parameters.filter(param => param.in === loc)
return { 'in': loc, schema: createParamsSchema(params, loc) }
})
.filter(param => Object.keys(param.schema.properties || {}).length)
.reduce((map, param) => {
map[param.in] = param.schema
return map
}, {})
.filter(filterNonEmptySchemas)
.reduce(mapSchemaObjects, {})
}

function createParamsSchema(params, loc) {
Expand All @@ -149,6 +146,40 @@ function createParamsSchema(params, loc) {
}
}

function filterNonEmptySchemas(param) {
let schemaProperties = {}

if (param.schema.properties) {
schemaProperties = param.schema.properties
} else if (param.schema.allOf) {
schemaProperties = param.schema.allOf.reduce((schemaProperties, currentAllOfSchema) => {
return Object.assign(schemaProperties, currentAllOfSchema.properties)
}, {})
}

return Object.keys(schemaProperties).length
}

function mapSchemaObjects(map, param) {
if (param.schema.allOf) {
map[param.in] = param.schema.allOf.reduce((aggregatedSchema, currentAllOfSchema) => {
if (currentAllOfSchema.required) {
aggregatedSchema['required'] = (aggregatedSchema['required'] || []).concat(currentAllOfSchema.required)
}

if (currentAllOfSchema.properties) {
aggregatedSchema['properties'] = Object.assign(aggregatedSchema['properties'] || {}, currentAllOfSchema.properties)
}

return aggregatedSchema
}, {})
} else {
map[param.in] = param.schema
}

return map
}

function createResponseSchemas(responses, spec) {
return Object.keys(responses)
.map(id => ({
Expand Down
36 changes: 36 additions & 0 deletions test/_fixture/petstore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ paths:
description: unexpected error
schema:
$ref: '#/definitions/Error'
/feeds/{feedId}:
get:
summary: Info for a specific feed
operationId: showFeedById
tags:
- feeds
parameters:
- name: feedId
in: path
required: true
description: The id of the feed to retrieve
type: string
responses:
200:
description: Expected response to a valid request
schema:
$ref: '#/definitions/Feed'
default:
description: unexpected error
schema:
$ref: '#/definitions/Error'
definitions:
Pet:
required:
Expand All @@ -89,6 +110,21 @@ definitions:
type: array
items:
$ref: '#/definitions/Pet'
Feed:
allOf:
- required:
- id
- brand
properties:
id:
type: integer
format: int64
brand:
type: string
- properties:
amount:
type: integer
format: int64
Error:
required:
- code
Expand Down
23 changes: 23 additions & 0 deletions test/swaggerSpec.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ describe('swaggerSpec', () => {
expect(bodySchema).toEqual(spec.definitions.Pet)
})

it('should resolve parameter body schemas reference', () => {
spec.paths['/feeds/{feedId}'].put = {
operationId: 'updateFeed',
parameters: [ {
in: 'body',
schema: {
$ref: '#/definitions/Feed'
}
} ]
}
const operations = swaggerSpec.getAllOperations(spec)
const opt = operations.find(opt => opt.id === 'updateFeed')
const bodySchema = opt.paramGroupSchemas.body

expect(bodySchema).toEqual({
required: spec.definitions.Feed.allOf[0].required,
properties: Object.assign(
spec.definitions.Feed.allOf[0].properties,
spec.definitions.Feed.allOf[1].properties
)
})
})

it('should resolve parameter array items reference', () => {
spec.paths['/pets/{petId}'].get = {
operationId: 'updatePet',
Expand Down