fixed issue

This commit is contained in:
JasonLandbridge
2024-09-19 12:58:13 +02:00
parent 58f4d1e36e
commit 6ee044e22d
4 changed files with 23 additions and 7 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -31,6 +31,7 @@
"devDependencies": { "devDependencies": {
"@modyfi/vite-plugin-yaml": "^1.1.0", "@modyfi/vite-plugin-yaml": "^1.1.0",
"@redocly/cli": "^1.23.1", "@redocly/cli": "^1.23.1",
"@types/lodash-es": "^4.17.12",
"@types/node": "^22.5.0", "@types/node": "^22.5.0",
"chokidar-cli": "^3.0.0", "chokidar-cli": "^3.0.0",
"prettier": "3.3.3", "prettier": "3.3.3",
@@ -41,6 +42,7 @@
}, },
"dependencies": { "dependencies": {
"ajv": "^8.17.1", "ajv": "^8.17.1",
"ajv-formats": "^3.0.1" "ajv-formats": "^3.0.1",
"lodash-es": "^4.17.21"
} }
} }

View File

@@ -128,6 +128,7 @@ required:
- twoFactorEnabled - twoFactorEnabled
- username - username
- uuid - uuid
- attributionPartner
properties: properties:
adsConsent: adsConsent:
type: type:
@@ -329,3 +330,8 @@ properties:
type: string type: string
description: The account UUID description: The account UUID
example: dae343c1f45beb4f example: dae343c1f45beb4f
attributionPartner:
type:
- string
- "null"
example: null

View File

@@ -2,7 +2,7 @@ import PMSSpec from "../../output/plex-media-server-spec-dereferenced.yaml"
import Ajv from "ajv" import Ajv from "ajv"
import addFormats from "ajv-formats" import addFormats from "ajv-formats"
import { expect } from "vitest" import { expect } from "vitest"
import { merge } from "lodash-es"
/** /**
* Validate a response against the OpenAPI spec * Validate a response against the OpenAPI spec
* NOTE: It accounts for the following scenarios: * NOTE: It accounts for the following scenarios:
@@ -24,7 +24,7 @@ export function validateResponseSpec(
const schema = PMSSpec const schema = PMSSpec
addAdditionalPropertiesFalse(schema.paths) addAdditionalProperties(schema.paths)
ajv.addSchema(schema, "API.yaml") ajv.addSchema(schema, "API.yaml")
addFormats(ajv) addFormats(ajv)
@@ -42,16 +42,24 @@ export function validateResponseSpec(
} }
/** /**
* Recursively add additionalProperties: false to all objects in the schema * Recursively add additionalProperties: false to all objects in the schema and merge allOf schemas
* This is necessary to ensure that the schema is strict and does not allow additional properties without explicitly defined in the openApi spec * This is necessary to ensure that the schema is strict and does not allow additional properties without explicitly defined in the openApi spec
* DOC: https://ajv.js.org/json-schema.html#additionalproperties * DOC: https://ajv.js.org/json-schema.html#additionalproperties
* @param schema * @param schema
*/ */
function addAdditionalPropertiesFalse(schema) { function addAdditionalProperties(schema) {
if (schema === null || typeof schema !== "object") { if (schema === null || typeof schema !== "object") {
return return
} }
// If the current schema has allOf, merge the schemas because AJV handles mentions of allOf as separate schemas that all need to pass at the same time. We use it in the spec to combine multiple schemas into one.
// DOC: https://ajv.js.org/json-schema.html#allof
if (schema.hasOwnProperty("allOf")) {
const merged = merge({}, ...schema.allOf)
delete schema.allOf
Object.assign(schema, merged)
}
// If the current schema defines an object, ensure additionalProperties is false // If the current schema defines an object, ensure additionalProperties is false
if (schema.hasOwnProperty("type") && schema.type === "object") { if (schema.hasOwnProperty("type") && schema.type === "object") {
if (!schema.hasOwnProperty("additionalProperties")) { if (!schema.hasOwnProperty("additionalProperties")) {
@@ -62,11 +70,11 @@ function addAdditionalPropertiesFalse(schema) {
// Recursively handle properties if they exist // Recursively handle properties if they exist
for (const [_, value] of Object.entries(schema)) { for (const [_, value] of Object.entries(schema)) {
if (Array.isArray(value)) { if (Array.isArray(value)) {
value.forEach((x) => addAdditionalPropertiesFalse(x)) value.forEach((x) => addAdditionalProperties(x))
} }
if (typeof value === "object") { if (typeof value === "object") {
addAdditionalPropertiesFalse(value) addAdditionalProperties(value)
} }
} }
} }