MCPcopy
hub / github.com/fastify/fastify / getSchemaSerializer

Function getSchemaSerializer

lib/schemas.js:145–208  ·  view source on GitHub ↗

* Search for the right JSON schema compiled function in the request context * setup by the route configuration `schema.response`. * It will look for the exact match (eg 200) or generic (eg 2xx) * * @param {object} context the request context * @param {number} statusCode the http status code *

(context, statusCode, contentType)

Source from the content-addressed store, hash-verified

143 * the reply or false if it is not set
144 */
145function getSchemaSerializer (context, statusCode, contentType) {
146 const responseSchemaDef = context[kSchemaResponse]
147 if (!responseSchemaDef) {
148 return false
149 }
150 if (responseSchemaDef[statusCode]) {
151 if (responseSchemaDef[statusCode].constructor === Object) {
152 const ct = new ContentType(contentType)
153 if (ct.isValid) {
154 if (responseSchemaDef[statusCode][ct.mediaType]) {
155 return responseSchemaDef[statusCode][ct.mediaType]
156 }
157
158 // fallback to match all media-type
159 if (responseSchemaDef[statusCode]['*/*']) {
160 return responseSchemaDef[statusCode]['*/*']
161 }
162
163 return false
164 }
165 }
166 return responseSchemaDef[statusCode]
167 }
168 const fallbackStatusCode = (statusCode + '')[0] + 'xx'
169 if (responseSchemaDef[fallbackStatusCode]) {
170 if (responseSchemaDef[fallbackStatusCode].constructor === Object) {
171 const ct = new ContentType(contentType)
172 if (ct.isValid) {
173 if (responseSchemaDef[fallbackStatusCode][ct.mediaType]) {
174 return responseSchemaDef[fallbackStatusCode][ct.mediaType]
175 }
176
177 // fallback to match all media-type
178 if (responseSchemaDef[fallbackStatusCode]['*/*']) {
179 return responseSchemaDef[fallbackStatusCode]['*/*']
180 }
181
182 return false
183 }
184 }
185
186 return responseSchemaDef[fallbackStatusCode]
187 }
188 if (responseSchemaDef.default) {
189 if (responseSchemaDef.default.constructor === Object) {
190 const ct = new ContentType(contentType)
191 if (ct.isValid) {
192 if (responseSchemaDef.default[ct.mediaType]) {
193 return responseSchemaDef.default[ct.mediaType]
194 }
195
196 // fallback to match all media-type
197 if (responseSchemaDef.default['*/*']) {
198 return responseSchemaDef.default['*/*']
199 }
200
201 return false
202 }

Callers 2

fallbackErrorHandlerFunction · 0.85
serializeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected