updating code for nullable types and tests
This commit is contained in:
Родитель
d43d2076a5
Коммит
da2a3df1f5
|
@ -53,6 +53,12 @@ exports.builder = {
|
|||
boolean: true,
|
||||
default: false
|
||||
},
|
||||
n: {
|
||||
alias: 'nullable',
|
||||
describe: 'Should nullable be resolved?',
|
||||
boolean: true,
|
||||
default: false
|
||||
},
|
||||
d: {
|
||||
alias: 'outputDir',
|
||||
describe: 'Output directory where the resolved swagger spec will be stored.',
|
||||
|
@ -74,6 +80,7 @@ exports.handler = function (argv) {
|
|||
vOptions.shouldResolveParameterizedHost = argv.t;
|
||||
vOptions.shouldResolvePureObjects = argv.p;
|
||||
vOptions.shouldResolveDiscriminator = argv.c;
|
||||
vOptions.shouldResolveNullableTypes = argv.n;
|
||||
|
||||
function execResolve() {
|
||||
if (specPath.match(/.*composite.*/ig) !== null) {
|
||||
|
|
|
@ -580,27 +580,83 @@ exports.relaxModelLikeEntities = function relaxModelLikeEntities(model) {
|
|||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relaxes the entity to be a oneOf: [the current type OR null type] if the condition is satisfied
|
||||
* @param {object} entity - The entity to be relaxed
|
||||
* @param {Boolean|undefined} isPropRequired - states whether the property is required.
|
||||
* If true then it is required. If false or undefined then it is not required.
|
||||
* @returns {object} entity - The processed entity
|
||||
*/
|
||||
exports.allowNullType = function allowNullType(entity, isPropRequired) {
|
||||
if (entity.type) { //not sure if I need to consider additionalProperties
|
||||
// if entity has a type
|
||||
if (entity && entity.type) {
|
||||
// if type is an array
|
||||
if (entity.type === "array") {
|
||||
if (entity.items) {
|
||||
// if items object contains inline properties
|
||||
if (entity.items.properties) {
|
||||
entity.items = exports.allowNullableTypes(entity.items)
|
||||
} else {
|
||||
entity.items = exports.allowNullType(entity.items);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// takes care of string 'false' and 'true'
|
||||
if (typeof entity['x-nullable'] === 'string') {
|
||||
if (entity['x-nullable'].toLowerCase() === 'false') {
|
||||
entity['x-nullable'] = false;
|
||||
} else if (entity['x-nullable'].toLowerCase() === 'true') {
|
||||
entity['x-nullable'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
let isPropNullable = entity['x-nullable'] && typeof entity['x-nullable'] === 'boolean';
|
||||
if ((typeof isPropNullable === "undefined" && !isPropRequired) || isPropNullable) {
|
||||
if ((isPropNullable === undefined && !isPropRequired) || isPropNullable) {
|
||||
let savedEntity = entity
|
||||
if (savedEntity.in) {
|
||||
entity.oneOf = [{ "type": entity.type }, { "type": "null" }];
|
||||
delete entity.type
|
||||
|
||||
}
|
||||
else {
|
||||
entity = {};
|
||||
entity.oneOf = [savedEntity, { "type": "null" }];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if there's a $ref
|
||||
if (entity && entity["$ref"]) {
|
||||
let isPropNullable = entity['x-nullable'] && typeof entity['x-nullable'] === 'boolean';
|
||||
if ((isPropNullable === undefined && !isPropRequired) || isPropNullable) {
|
||||
let savedEntity = entity
|
||||
entity = {};
|
||||
entity.oneOf = [savedEntity, { "type": "null" }];
|
||||
}
|
||||
}
|
||||
return entity;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relaxes/Transforms model definition to allow null values
|
||||
*/
|
||||
exports.allowNullableTypes = function allowNullableTypes(model) {
|
||||
if (model.properties) {
|
||||
// process additionalProperties if present
|
||||
if (model && typeof model.additionalProperties === 'object') {
|
||||
if (model.additionalProperties.properties || model.additionalProperties.additionalProperties) {
|
||||
model.additionalProperties = exports.allowNullableTypes(model.additionalProperties)
|
||||
} else {
|
||||
// there shouldn't be more properties nesting at this point
|
||||
model.additionalProperties = exports.allowNullType(model.additionalProperties);
|
||||
}
|
||||
}
|
||||
if (model && model.properties) {
|
||||
let modelProperties = model.properties;
|
||||
for (let propName in modelProperties) {
|
||||
let isPropRequired = model.required ? model.required.some((p) => { return p == propName; }) : false
|
||||
if (modelProperties[propName].properties) {
|
||||
let isPropRequired = model.required ? model.required.some((p) => { return p == propName; }) : false;
|
||||
// process properties if present
|
||||
if (modelProperties[propName].properties || modelProperties[propName].additionalProperties) {
|
||||
modelProperties[propName] = exports.allowNullableTypes(modelProperties[propName]);
|
||||
}
|
||||
else {
|
||||
|
@ -608,11 +664,49 @@ exports.allowNullableTypes = function allowNullableTypes(model) {
|
|||
}
|
||||
}
|
||||
}
|
||||
//this needs to happen at the end, as model is modified in allowNullType to have only oneOf value with the original contents or null
|
||||
model = exports.allowNullType(model);
|
||||
|
||||
if (model && model.type) {
|
||||
if (model.type == 'array') {
|
||||
if (model.items) {
|
||||
// if items object contains inline properties
|
||||
if (model.items.properties) {
|
||||
model.items = exports.allowNullableTypes(model.items)
|
||||
}
|
||||
else {
|
||||
model.items = exports.allowNullType(model.items);
|
||||
}
|
||||
}
|
||||
}
|
||||
// if we have a top level "object" with x-nullable set, we need to relax the model at that level
|
||||
else if (model.type == "object" && model['x-nullable']) {
|
||||
model = exports.allowNullType(model)
|
||||
}
|
||||
}
|
||||
|
||||
// if model is a parameter (contains "in" property" we want to relax the parameter
|
||||
if (model && model.in && model['x-nullable']) {
|
||||
model = exports.allowNullType(model, model["required"])
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relaxes/Transforms parameter definition to allow null values for non-path parameters
|
||||
*/
|
||||
exports.allowNullableParams = function allowNullableParams(parameter) {
|
||||
if (parameter["in"] && parameter["in"] === "body" && parameter["schema"]) {
|
||||
parameter["schema"] = exports.allowNullableTypes(parameter["schema"]);
|
||||
}
|
||||
else {
|
||||
if (parameter["in"] && parameter["in"] !== "path") {
|
||||
parameter = exports.allowNullType(parameter, parameter["required"])
|
||||
}
|
||||
}
|
||||
return parameter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sanitizes the file name by replacing special characters with
|
||||
* empty string and by replacing space(s) with _.
|
||||
|
|
|
@ -621,13 +621,26 @@ class SpecResolver {
|
|||
//scan definitions and properties of every model in definitions
|
||||
for (let defName in definitions) {
|
||||
let model = definitions[defName];
|
||||
utils.allowNullableTypes(model);
|
||||
definitions[defName] = utils.allowNullableTypes(model);
|
||||
}
|
||||
//scan every operation response
|
||||
for (let path in spec.paths) {
|
||||
let pathObj = spec.paths[path];
|
||||
//need to handle paramaters at this level
|
||||
if (pathObj.parameters) {
|
||||
for (let parameter in pathOpj.parameters) {
|
||||
pathObj.parameters[parameter] = utils.allowNullableParams(pathObj.parameters[parameter]);
|
||||
}
|
||||
}
|
||||
for (let verb in pathObj) {
|
||||
let operation = pathObj[verb];
|
||||
// need to account for parameters, except for path parameters
|
||||
if (operation.parameters) {
|
||||
for (let parameter in operation.parameters) {
|
||||
operation.parameters[parameter] = utils.allowNullableParams(operation.parameters[parameter]);
|
||||
}
|
||||
}
|
||||
// going through responses
|
||||
if (operation.responses) {
|
||||
for (let statusCode in operation.responses) {
|
||||
let response = operation.responses[statusCode];
|
||||
|
@ -638,6 +651,12 @@ class SpecResolver {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// scan parameter definitions
|
||||
for (let parameter in spec.parameters) {
|
||||
spec.parameters[parameter] = utils.allowNullableParams(spec.parameters[parameter]);
|
||||
}
|
||||
|
||||
return Promise.resolve(self);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": {
|
||||
"value": [
|
||||
{
|
||||
"id": null
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": {
|
||||
"namedProp": {
|
||||
"id": "OID"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": {
|
||||
"namedProp": {
|
||||
"id": null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": {
|
||||
"state": "New"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": {
|
||||
"capacity": null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": "notNull"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": null
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": {
|
||||
"resultProp": null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": {
|
||||
"readOnlyProp": null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"parameters": {
|
||||
"api-version": "2017-12-01",
|
||||
"queryParam": null,
|
||||
"bodyParam": null
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": {
|
||||
"prop1": null,
|
||||
"prop2": null,
|
||||
"id": "ResultId"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": {
|
||||
"requiredProp": null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": [
|
||||
{
|
||||
"id": "itemId"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"parameters": {
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"headers": {},
|
||||
"body": {
|
||||
"notNullable": "notNull"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,522 @@
|
|||
{
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"title": "title",
|
||||
"description": "",
|
||||
"version": "2017-11-01"
|
||||
},
|
||||
"host": "host",
|
||||
"schemes": [
|
||||
"https"
|
||||
],
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"paths": {
|
||||
"/foo/regularOperation": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "regularOperation_Get",
|
||||
"description": "Operation to test invalid_type for nulls",
|
||||
"x-ms-examples": {
|
||||
"Regular Operation": {
|
||||
"$ref": "./examples/regularOperation.json"
|
||||
}
|
||||
},
|
||||
"parameters": [
|
||||
{
|
||||
"$ref": "#/parameters/ApiVersionParameter"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"in": "query",
|
||||
"name": "queryParam",
|
||||
"description": "query param description"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"in": "body",
|
||||
"name": "bodyParam",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Model1"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Result"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/formatInDefinition": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "formatInDefinition_Get",
|
||||
"description": "Operation to test format in model",
|
||||
"x-ms-examples": {
|
||||
"Format in Definition": {
|
||||
"$ref": "./examples/formatInDefinition.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/SkuModel"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/enumInResponse": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "enumInResponse_Get",
|
||||
"description": "Operation to test enum in model",
|
||||
"x-ms-examples": {
|
||||
"Create or Update Configuration": {
|
||||
"$ref": "./examples/enumInResponse.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Configuration"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/readOnlyProp": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "readOnlyProp_Get",
|
||||
"description": "Operation to test read-only prop in model",
|
||||
"x-ms-examples": {
|
||||
"Create or Update Configuration": {
|
||||
"$ref": "./examples/readonlyInResponse.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ReadOnlyPropModel"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/arrayInResponse": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "arrayInResponse_List",
|
||||
"description": "Operation to test array in response model",
|
||||
"x-ms-examples": {
|
||||
"Create or Update Configuration": {
|
||||
"$ref": "./examples/arrayInResponse.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ResultList"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/objectInResponse": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "objectInResponse_Get",
|
||||
"description": "Operation to test array in response model",
|
||||
"x-ms-examples": {
|
||||
"Create or Update Configuration": {
|
||||
"$ref": "./examples/objectInResponse.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ResultObject"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/typeArrayInResponse": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "typeArrayInResponse_Get",
|
||||
"description": "Operation to test array in response model",
|
||||
"x-ms-examples": {
|
||||
"Create or Update Configuration": {
|
||||
"$ref": "./examples/typeArrayInResponse.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ArrayResults"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/xnullableFalse": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "xnullableFalse_Get",
|
||||
"description": "Operation to test invalid_type for nulls",
|
||||
"x-ms-examples": {
|
||||
"x-nullable false operation": {
|
||||
"$ref": "./examples/xnullableFalseOperation.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ModelWithXNullableFalse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/requiredProp": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "requiredProp_Get",
|
||||
"description": "Operation to test invalid_type for nulls",
|
||||
"x-ms-examples": {
|
||||
"required prop operation": {
|
||||
"$ref": "./examples/requiredPropOperation.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/RequiredProp"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/inlineResponse": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "inlineResponse_Get",
|
||||
"description": "Operation to test invalid_type for nulls",
|
||||
"x-ms-examples": {
|
||||
"required prop operation": {
|
||||
"$ref": "./examples/inlineResponse.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"name": "inlineProp",
|
||||
"type": "string",
|
||||
"description": "inlineresponse",
|
||||
"x-nullable": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/refWithNullableAtTopLevelOperation": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "RefWithNullableAtTopLevelOperation_Get",
|
||||
"description": "Operation to test invalid_type for nulls",
|
||||
"x-ms-examples": {
|
||||
"required prop operation": {
|
||||
"$ref": "./examples/nullableTopLevel.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Model1",
|
||||
"x-nullable": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/definitionWithReferenceOperation": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "definitionWithReference_Get",
|
||||
"description": "Operation to test invalid_type for nulls",
|
||||
"x-ms-examples": {
|
||||
"required prop operation": {
|
||||
"$ref": "./examples/definitionWithReferenceOperation.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/DefinitionWithReference"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/definitionWithReferenceNotNullableOperation": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "definitionWithReferenceNotNullableOperation_Get",
|
||||
"description": "Operation to test invalid_type for nulls",
|
||||
"x-ms-examples": {
|
||||
"required prop operation": {
|
||||
"$ref": "./examples/definitionWithReferenceNotNullableOperation.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/DefinitionWithReferenceNotNullable"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/nullableTopLevel": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"getTag"
|
||||
],
|
||||
"operationId": "nullableTopLevel_Get",
|
||||
"description": "Operation to test invalid_type for nulls",
|
||||
"x-ms-examples": {
|
||||
"required prop operation": {
|
||||
"$ref": "./examples/nullableTopLevel.json"
|
||||
}
|
||||
},
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "success response",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/NullableTopLevelObject"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"Result": {
|
||||
"description": "Result decription",
|
||||
"properties": {
|
||||
"prop1": {
|
||||
"type": "string",
|
||||
"description": "prop1 dummy description."
|
||||
},
|
||||
"prop2": {
|
||||
"type": "integer",
|
||||
"description": "prop2 dummy description"
|
||||
}
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/Model1"
|
||||
}
|
||||
]
|
||||
},
|
||||
"Model1": {
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string",
|
||||
"description": "Model Id"
|
||||
}
|
||||
},
|
||||
"description": "Model1 definition."
|
||||
},
|
||||
"SkuModel": {
|
||||
"properties": {
|
||||
"capacity": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"description": "Gets or sets the SKU capacity."
|
||||
}
|
||||
},
|
||||
"description": "dummy description."
|
||||
},
|
||||
"Configuration": {
|
||||
"properties": {
|
||||
"state": {
|
||||
"type": "string",
|
||||
"description": "Gets or sets the state of the configuration.",
|
||||
"enum": [
|
||||
"New",
|
||||
"Edit",
|
||||
"Published"
|
||||
],
|
||||
"x-ms-enum": {
|
||||
"name": "DscConfigurationState",
|
||||
"modelAsString": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "dummy description."
|
||||
},
|
||||
"ReadOnlyPropModel": {
|
||||
"properties": {
|
||||
"readOnlyProp": {
|
||||
"type": "string",
|
||||
"description": "read only prop",
|
||||
"readOnly": true
|
||||
}
|
||||
},
|
||||
"description": "dummy description."
|
||||
},
|
||||
"ResultList": {
|
||||
"properties": {
|
||||
"value": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/Model1"
|
||||
},
|
||||
"description": "Gets or sets a list of configurations."
|
||||
}
|
||||
},
|
||||
"description": "dummy description."
|
||||
},
|
||||
"ResultObject": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"resultProp": {
|
||||
"type": "string",
|
||||
"description": "dummy description."
|
||||
}
|
||||
},
|
||||
"description": "dummy description."
|
||||
},
|
||||
"ArrayResults": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/Model1"
|
||||
},
|
||||
"description": "dummy description."
|
||||
},
|
||||
"ModelWithXNullableFalse": {
|
||||
"properties": {
|
||||
"notNullable": {
|
||||
"type": "string",
|
||||
"description": "property that shouldn't be null.",
|
||||
"x-nullable": false
|
||||
}
|
||||
},
|
||||
"description": "Model with non-nullable property."
|
||||
},
|
||||
"RequiredProp": {
|
||||
"properties": {
|
||||
"requiredProp": {
|
||||
"type": "string",
|
||||
"description": "required property.",
|
||||
"x-nullable": true
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"requiredProp"
|
||||
],
|
||||
"description": "model with required property."
|
||||
},
|
||||
"DefinitionWithReference": {
|
||||
"properties": {
|
||||
"namedProp": {
|
||||
"$ref": "#/definitions/Model1",
|
||||
"description": "property with reference to model"
|
||||
}
|
||||
},
|
||||
"description": "model with ref property."
|
||||
},
|
||||
"DefinitionWithReferenceNotNullable": {
|
||||
"properties": {
|
||||
"namedProp": {
|
||||
"$ref": "#/definitions/Model1",
|
||||
"description": "property with reference to model not nullable",
|
||||
"x-nullable": false
|
||||
}
|
||||
},
|
||||
"description": "model with reF property not nullable."
|
||||
},
|
||||
"NullableTopLevelObject": {
|
||||
"type": "object",
|
||||
"x-nullable": true,
|
||||
"properties": {
|
||||
"refProp": {
|
||||
"$ref": "#/definitions/Model1",
|
||||
"description": "ref to Model1 model"
|
||||
}
|
||||
},
|
||||
"description": "nullable top level object"
|
||||
}
|
||||
},
|
||||
"parameters": {
|
||||
"ApiVersionParameter": {
|
||||
"name": "api-version",
|
||||
"in": "query",
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"description": "Client Api Version."
|
||||
}
|
||||
}
|
||||
}
|
|
@ -137,4 +137,174 @@ describe('Model Validation', function () {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Nullable models - ', function () {
|
||||
it('should pass for regularOperation_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "regularOperation_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for formatInDefinition_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "formatInDefinition_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for enumInResponse_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "enumInResponse_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for readOnlyProp_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "readOnlyProp_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for arrayInResponse_List', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "arrayInResponse_List";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for objectInResponse_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "objectInResponse_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for typeArrayInResponse_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "typeArrayInResponse_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for xnullableFalse_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "xnullableFalse_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for requiredProp_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "requiredProp_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for inlineResponse_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "inlineResponse_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for RefWithNullableAtTopLevelOperation_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "RefWithNullableAtTopLevelOperation_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for definitionWithReference_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "definitionWithReference_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for definitionWithReferenceNotNullableOperation_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "definitionWithReferenceNotNullableOperation_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass for nullableTopLevel_Get', function (done) {
|
||||
let specPath = `${__dirname}/modelValidation/swaggers/specification/nullableTypes/invalid_type_test.json`;
|
||||
let operationIds = "nullableTopLevel_Get";
|
||||
validate.validateExamples(specPath, operationIds, { consoleLogLevel: 'off' }).then((result) => {
|
||||
assert(result.validityStatus === true, `swagger "${specPath}" with operation "${operationIds}" contains model validation errors.`);
|
||||
console.log(result);
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Загрузка…
Ссылка в новой задаче