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

Clone schemas #580

Merged
merged 4 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function transformDefsToComponents (jsonSchema) {
if (typeof jsonSchema === 'object' && jsonSchema !== null) {
// Handle patternProperties, that is not part of OpenAPI definitions
if (jsonSchema.patternProperties) {
jsonSchema.additionalProperties = Object.values(jsonSchema.patternProperties)[0];
jsonSchema.additionalProperties = Object.values(jsonSchema.patternProperties)[0]
delete jsonSchema.patternProperties
} else if (jsonSchema.const) {
// OAS 3.1 supports `const` but it is not supported by `swagger-ui`
Expand Down
3 changes: 2 additions & 1 deletion lib/util/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const fs = require('fs')
const path = require('path')
const Ref = require('json-schema-resolver')
const cloner = require('rfdc')({ proto: true, circles: false })
const { rawRequired } = require('../symbols')
const { xConsume } = require('../constants')

Expand All @@ -21,7 +22,7 @@ function addHook (fastify, pluginOptions) {
// when schemaId is the same in difference instance
// the latter will lost
instance.addHook('onReady', (done) => {
const allSchemas = instance.getSchemas()
const allSchemas = cloner(instance.getSchemas())
for (const schemaId of Object.keys(allSchemas)) {
if (!sharedSchemasMap.has(schemaId)) {
sharedSchemasMap.set(schemaId, allSchemas[schemaId])
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"fastify-static": "^4.0.0",
"js-yaml": "^4.0.0",
"json-schema-resolver": "^1.3.0",
"openapi-types": "^10.0.0"
"openapi-types": "^10.0.0",
"rfdc": "^1.3.0"
},
"standard": {
"ignore": [
Expand Down
30 changes: 30 additions & 0 deletions test/spec/openapi/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,36 @@ test('support $ref in response schema', async (t) => {
await Swagger.validate(openapiObject)
})

test('support $ref for enums in other schemas', async (t) => {
const fastify = Fastify()

const enumSchema = { $id: 'order', anyOf: [{ type: 'string', const: 'foo' }, { type: 'string', const: 'bar' }] }
const enumRef = { $ref: 'order' }
const objectWithEnumSchema = { $id: 'object', type: 'object', properties: { type: enumRef }, required: ['type'] }

await fastify.register(fastifySwagger, openapiOption)
await fastify.register(async (instance) => {
instance.addSchema(enumSchema)
instance.addSchema(objectWithEnumSchema)
instance.post('/', { schema: { body: { type: 'object', properties: { order: { $ref: 'order' } } } } }, async () => ({ result: 'OK' }))
})

await fastify.ready()

const responseBeforeSwagger = await fastify.inject({ method: 'POST', url: '/', payload: { order: 'foo' } })

t.equal(responseBeforeSwagger.statusCode, 200)
const openapiObject = fastify.swagger()

t.equal(typeof openapiObject, 'object')

await Swagger.validate(openapiObject)

const responseAfterSwagger = await fastify.inject({ method: 'POST', url: '/', payload: { order: 'foo' } })

t.equal(responseAfterSwagger.statusCode, 200)
})

test('support nested $ref schema : complex case without modifying buildLocalReference', async (t) => {
const fastify = Fastify()
fastify.register(fastifySwagger, { openapi: {} })
Expand Down