diff --git a/.vscode/launch.json b/.vscode/launch.json index 94128032..addfacb4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -84,7 +84,10 @@ "stopOnEntry": false, "args": [ "--no-timeouts", - "test/modelValidatorTests.js" + "test/modelValidatorTests.ts", + "-r", + "ts-node/register", + "--no-timeouts" ], "env": {} }, diff --git a/ChangeLog.md b/ChangeLog.md index c6dd6f5b..dc8f28bf 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,7 @@ +### 06/19/2018 0.4.49 +- Bug fix: Data is valid against more than one schema from `oneOf` [#248](https://github.com/Azure/oav/pull/248) + The problem occurs when referenced model may also accept `null`. The fix is replacing `oneOf` with `anyOf`. + ### 05/14/2018 0.4.38 - Bug fix: `oav extract-xmsexamples` also extracts request headers. [#241](https://github.com/Azure/oav/pull/241) - Bug fix: x-ms-parametrized-host is not validated correctly [#240](https://github.com/Azure/oav/issues/240) diff --git a/lib/util/utils.ts b/lib/util/utils.ts index 770373ae..36f87264 100644 --- a/lib/util/utils.ts +++ b/lib/util/utils.ts @@ -559,11 +559,12 @@ export interface Model { additionalProperties?: Model|false "x-nullable"?: Unknown in?: Unknown - oneOf?: Unknown + oneOf?: Model[] $ref?: string required?: Unknown[]|false schema?: Model allOf?: Ref[] + anyOf?: Model[] description?: Unknown discriminator?: string "x-ms-discriminator-value"?: string @@ -698,8 +699,9 @@ export function allowNullType(entity: Model, isPropRequired?: boolean|{}): Model && entity.$ref && shouldAcceptNullValue(entity["x-nullable"], isPropRequired)) { const savedEntity = entity - entity = {} - entity.oneOf = [savedEntity, { type: "null" }] + entity = { + anyOf: [savedEntity, { type: "null" }] + } } return entity } diff --git a/lib/validate.ts b/lib/validate.ts index 5216fc27..e0ddcea0 100644 --- a/lib/validate.ts +++ b/lib/validate.ts @@ -3,9 +3,6 @@ import * as fs from "fs" import * as path from "path" -import * as msrest from "ms-rest" -import * as msrestazure from "ms-rest-azure" -import { ResourceManagementClient } from "azure-arm-resource" import { log } from "./util/logging" import * as utils from "./util/utils" import { SpecValidator, SpecValidationResult } from "./validators/specValidator" diff --git a/package-lock.json b/package-lock.json index f7afef53..704da9c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "oav", - "version": "0.4.48", + "version": "0.4.49", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -5663,9 +5663,9 @@ } }, "validator": { - "version": "10.3.0", - "resolved": "https://registry.npmjs.org/validator/-/validator-10.3.0.tgz", - "integrity": "sha512-bn7dcJcdkpSjcujYlf8lrY9VL660h5njEkFzQzQOFMQgJ3Id1C4+MkazHKgHE45NoGsyQYEPmo4dCIbDQ7eTdw==" + "version": "10.4.0", + "resolved": "https://registry.npmjs.org/validator/-/validator-10.4.0.tgz", + "integrity": "sha512-Q/wBy3LB1uOyssgNlXSRmaf22NxjvDNZM2MtIQ4jaEOAB61xsh1TQxsq1CgzUMBV1lDrVMogIh8GjG1DYW0zLg==" }, "z-schema": { "version": "3.22.0", @@ -5675,7 +5675,7 @@ "commander": "2.15.0", "lodash.get": "4.4.2", "lodash.isequal": "4.5.0", - "validator": "10.3.0" + "validator": "10.4.0" } } } diff --git a/package.json b/package.json index 111aa78f..f8b4a2fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "oav", - "version": "0.4.48", + "version": "0.4.49", "author": { "name": "Microsoft Corporation", "email": "azsdkteam@microsoft.com", diff --git a/test/modelValidation/swaggers/specification/nullableTypes/examples/definitionWithReferenceNullOperation.json b/test/modelValidation/swaggers/specification/nullableTypes/examples/definitionWithReferenceNullOperation.json new file mode 100644 index 00000000..600ab30a --- /dev/null +++ b/test/modelValidation/swaggers/specification/nullableTypes/examples/definitionWithReferenceNullOperation.json @@ -0,0 +1,11 @@ +{ + "parameters": {}, + "responses": { + "200": { + "headers": {}, + "body": { + "namedProp": null + } + } + } +} \ No newline at end of file diff --git a/test/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json b/test/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json index 0944ca72..d9ed7042 100644 --- a/test/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json +++ b/test/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json @@ -314,6 +314,29 @@ } } }, + "/foo/definitionWithReferenceNullOperation": { + "get": { + "tags": [ + "getTag" + ], + "operationId": "definitionWithReferenceNull_Get", + "description": "Operation to test invalid_type for nulls", + "x-ms-examples": { + "required prop operation": { + "$ref": "./examples/definitionWithReferenceNullOperation.json" + } + }, + "parameters": [], + "responses": { + "200": { + "description": "success response", + "schema": { + "$ref": "#/definitions/DefinitionWithReference" + } + } + } + } + }, "/foo/definitionWithReferenceNotNullableOperation": { "get": { "tags": [ @@ -389,7 +412,7 @@ }, "definitions": { "Result": { - "description": "Result decription", + "description": "Result description", "properties": { "prop1": { "type": "string", diff --git a/test/modelValidatorTests.ts b/test/modelValidatorTests.ts index eae4359c..a0bd7096 100644 --- a/test/modelValidatorTests.ts +++ b/test/modelValidatorTests.ts @@ -329,6 +329,18 @@ describe("Model Validation", () => { console.log(result) }) + it("should pass for definitionWithReferenceNull_Get", async () => { + const specPath2 = + `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json` + const operationIds = "definitionWithReferenceNull_Get" + const result = await validate.validateExamples( + specPath2, operationIds, { consoleLogLevel: "off" }) + assert( + result.validityStatus === true, + `swagger "${specPath2}" with operation "${operationIds}" contains model validation errors.`) + console.log(result) + }) + it("should pass for definitionWithReferenceNotNullableOperation_Get", async () => { const specPath2 = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`