Skip to content
New issue

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

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

Already on GitHub? # to your account

perf: use optional chaining #850

Merged
merged 1 commit into from
Jan 5, 2025
Merged
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
4 changes: 2 additions & 2 deletions lib/mode/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ module.exports = function (fastify, opts, done) {
}

function swagger (opts) {
if (opts && opts.yaml) {
if (opts?.yaml) {
if (cache.swaggerString) return cache.swaggerString
} else {
if (cache.swaggerObject) return cache.swaggerObject
}

if (opts && opts.yaml) {
if (opts?.yaml) {
const swaggerString = yaml.stringify(swaggerObject, { strict: false })
cache.swaggerString = swaggerString
return swaggerString
Expand Down
4 changes: 2 additions & 2 deletions lib/spec/openapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
const defOpts = prepareDefaultOptions(opts)

return function (opts) {
if (opts && opts.yaml) {
if (opts?.yaml) {
if (cache.string) return cache.string
} else {
if (cache.object) return cache.object
Expand Down Expand Up @@ -74,7 +74,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
? defOpts.transformObject({ openapiObject })
: openapiObject

if (opts && opts.yaml) {
if (opts?.yaml) {
cache.string = yaml.stringify(transformObjectResult, { strict: false })
return cache.string
}
Expand Down
14 changes: 7 additions & 7 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function schemaToMediaRecursive (schema) {
function resolveBodyParams (body, schema, consumes, ref) {
const resolved = convertJsonSchemaToOpenapi3(ref.resolve(schema))

if (resolved.content && resolved.content[Object.keys(resolved.content)[0]].schema) {
if (resolved.content?.[Object.keys(resolved.content)[0]].schema) {
for (const contentType in schema.content) {
body.content[contentType] = schemaToMediaRecursive(resolved.content[contentType].schema)
}
Expand All @@ -279,11 +279,11 @@ function resolveBodyParams (body, schema, consumes, ref) {
body.content[consume] = media
})

if (resolved && resolved.required && resolved.required.length) {
if (resolved?.required?.length) {
body.required = true
}

if (resolved && resolved.description) {
if (resolved?.description) {
body.description = resolved.description
}
}
Expand All @@ -294,7 +294,7 @@ function resolveCommonParams (container, parameters, schema, ref, sharedSchemas,
let resolved = convertJsonSchemaToOpenapi3(ref.resolve(schema))

// if the resolved definition is in global schema
if (resolved.$ref && resolved.$ref.startsWith(schemasPath)) {
if (resolved.$ref?.startsWith(schemasPath)) {
const parts = resolved.$ref.split(schemasPath)
const pathParts = parts[1].split('/')
resolved = pathParts.reduce((resolved, pathPart) => resolved[pathPart], ref.definitions().definitions)
Expand Down Expand Up @@ -361,7 +361,7 @@ function resolveResponse (fastifyResponseJson, produces, ref) {

// add schema when type is not 'null'
if (rawJsonSchema.type !== 'null') {
if (resolved.content && resolved.content[Object.keys(resolved.content)[0]].schema) {
if (resolved.content?.[Object.keys(resolved.content)[0]].schema) {
response.content = resolved.content
} else {
const content = {}
Expand Down Expand Up @@ -446,8 +446,8 @@ function prepareOpenapiMethod (schema, ref, openapiObject, url) {

// Parse out the security prop keys to ignore
const securityIgnores = [
...(openapiObject && openapiObject.security ? openapiObject.security : []),
...(schema && schema.security ? schema.security : [])
...(openapiObject?.security || []),
...(schema?.security || [])
]
.reduce((acc, securitySchemeGroup) => {
Object.keys(securitySchemeGroup).forEach((securitySchemeLabel) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/spec/swagger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
const defOpts = prepareDefaultOptions(opts)

return function (opts) {
if (opts && opts.yaml) {
if (opts?.yaml) {
if (cache.string) return cache.string
} else {
if (cache.object) return cache.object
Expand Down Expand Up @@ -66,7 +66,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
? defOpts.transformObject({ swaggerObject })
: swaggerObject

if (opts && opts.yaml) {
if (opts?.yaml) {
cache.string = yaml.stringify(transformObjectResult, { strict: false })
return cache.string
}
Expand Down
6 changes: 3 additions & 3 deletions lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function resolveBodyParams (parameters, schema, ref) {
parameters.push({
name: 'body',
in: 'body',
description: resolved && resolved.description ? resolved.description : undefined,
description: resolved?.description,
schema: resolved
})
}
Expand Down Expand Up @@ -274,8 +274,8 @@ function prepareSwaggerMethod (schema, ref, swaggerObject, url) {

// Parse out the security prop keys to ignore
const securityIgnores = [
...(swaggerObject && swaggerObject.security ? swaggerObject.security : []),
...(schema && schema.security ? schema.security : [])
...(swaggerObject?.security || []),
...(schema?.security || [])
]
.reduce((acc, securitySchemeGroup) => {
Object.keys(securitySchemeGroup).forEach((securitySchemeLabel) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/util/should-route-hide.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
function shouldRouteHide (schema, opts) {
const { hiddenTag, hideUntagged } = opts

if (schema && schema.hide) {
if (schema?.hide) {
return true
}

const tags = (schema && schema.tags) || []
const tags = schema?.tags || []

if (tags.length === 0 && hideUntagged) {
return true
Expand Down
Loading