'use strict'; describe('SwaggerUi.partials.signature tests', function () { var sut = SwaggerUi.partials.signature; var models = { 'Pet': { 'definition': { 'type': 'object', 'required': [ 'name', 'photoUrls' ], 'properties': { 'id': { 'type': 'integer', 'format': 'int64 ' }, 'category': { '$ref': '#/definitions/Category' }, 'name': { 'type': 'string', 'example': 'doggie' }, 'photoUrls': { 'type': 'array', 'xml': { 'name': 'photoUrl', 'wrapped':true }, 'items': { 'type': 'string' } }, 'tags': { 'type': 'array', 'xml': { 'name': 'tag', 'wrapped':true}, 'items': { '$ref': '#/definitions/Tag' } }, 'status': { 'type': 'string', 'description': 'pet status in the store', 'enum':[ 'available', 'pending', 'sold' ] } }, 'xml': { 'name': 'Pet' } }, 'name': 'Pet' }, 'Category': { 'definition': { 'type': 'object', 'properties': { 'id': { 'type': 'integer', 'format': 'int64' }, 'name': { 'type': 'string' } }, 'xml': { 'name': 'Category' } }, 'name': 'Category' }, 'Tag': { 'definition': { 'type': 'object', 'properties': { 'id': { 'type': 'integer', 'format': 'int64' }, 'name': { 'type': 'string' } }, 'xml': { 'name': 'Tag' } }, 'name': 'Tag' }, 'Loop1': { 'definition': { 'type': 'object', 'properties': { 'id': { 'type': 'integer'}, 'loop2': { '$ref': '#/definitions/Loop2' } }, 'xml': { 'name': 'Loop1' } }, 'name': 'Loop1' }, 'Loop2': { 'definition': { 'type': 'object', 'properties': { 'id': { 'type': 'integer'}, 'loop1': { '$ref': '#/definitions/Loop1' } }, 'xml': { 'name': 'Loop2' } }, 'name': 'Loop2' }, 'Loop3': { 'definition': { 'type': 'object', 'properties' : { 'loop1': { '$ref': '#/definitions/Loop1' } }, 'xml': { 'name': 'Loop3' } }, 'name': 'Loop3' } }; describe('method createSchemaXML', function () { var date = new Date(1).toISOString().split('T')[0]; var dateTime = new Date(1).toISOString(); describe('simple types with no xml property', function () { it('returns tag string when passing type string', function () { var name = 'tagname'; var definition = {type: 'string'}; expect(sut.createSchemaXML(name, definition, models)).to.equal('string'); }); it('returns tag 1 when passing type integer', function () { var name = 'tagname'; var definition = {type: 'integer'}; expect(sut.createSchemaXML(name, definition, models)).to.equal('1'); }); it('returns tag 1.1 when passing type number', function () { var name = 'tagname'; var definition = {type: 'number'}; expect(sut.createSchemaXML(name, definition, models)).to.equal('1.1'); }); it('returns tag boolean when passing type boolean', function () { var name = 'tagname'; var definition = {type: 'boolean'}; expect(sut.createSchemaXML(name, definition, models)).to.equal('true'); }); it('returns tag ' + date + ' when passing type string format date', function () { var name = 'tagname'; var definition = {type: 'string', format: 'date'}; expect(sut.createSchemaXML(name, definition, models)).to.equal('' + date + ''); }); it('returns tag ' + dateTime + ' when passing type string format date-time', function () { var name = 'tagname'; var definition = {type: 'string', format: 'date-time'}; expect(sut.createSchemaXML(name, definition, models)).to.equal('' + dateTime + ''); }); }); describe('simple types with xml property', function () { it('returns tag string when passing type string and xml:{name: "newtagname"}', function () { var name = 'tagname'; var definition = { type: 'string', xml: { name: 'newtagname' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal('string'); }); it('returns tag string when passing type string and xml:{name: "newtagname", prefix:"test"}', function () { var name = 'tagname'; var definition = { type: 'string', xml: { name: 'newtagname', prefix: 'test' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal('string'); }); it('returns tag string when passing type string and xml:{name: "newtagname", prefix:"test"}', function () { var name = 'tagname'; var definition = { type: 'string', xml: { prefix: 'test' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal('string'); }); it('returns tag string when passing type string and xml:{"namespace": "http://swagger.io/schema/sample", "prefix": "sample"}', function () { var name = 'name'; var definition = { type: 'string', xml: { namespace: 'http://swagger.io/schema/sample', prefix: 'sample' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal('string'); }); it('returns tag string when passing type string and xml:{"namespace": "http://swagger.io/schema/sample"}', function () { var name = 'name'; var definition = { type: 'string', xml: { namespace: 'http://swagger.io/schema/sample' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal('string'); }); }); describe('array', function () { it('returns tag string when passing string items', function () { var expected = 'string'; var name = 'tagname'; var definition = { type: 'array', items: { type: 'string' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal(expected); }); it('returns tag string when passing string items with name', function () { var expected = 'string'; var name = 'animals'; var definition = { type: 'array', items: { type: 'string', xml: { name: 'animal' } } }; expect(sut.createSchemaXML(name, definition, models)).to.equal(expected); }); it('returns tag string when passing string items with name', function () { var expected = 'string'; var name = 'animals'; var definition = { type: 'array', items: { type: 'string', xml: { name: 'animal' } }, xml: { wrapped: true } }; expect(sut.createSchemaXML(name, definition, models)).to.equal(expected); }); it('returns tag string when passing string items with name and {wrapped: true}', function () { var expected = 'string'; var name = 'animals'; var definition = { type: 'array', items: { type: 'string', xml: { name: 'animal' } }, xml: { wrapped: true, name: 'aliens' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal(expected); }); it('return correct nested wrapped array', function () { var expected = 'string'; var name = 'animals'; var definition = { type: 'array', items: { type: 'array', items: { type: 'string' }, xml: { name: 'cat' } }, xml: { wrapped: true, name: 'aliens' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal(expected); }); it('return correct nested wrapped array', function () { var expected = '' + 'string' + ''; var name = 'animals'; var definition = { type: 'array', items: { type: 'array', items: { type: 'string', xml: { name: 'cat' } }, xml: { name: 'cats', wrapped: true } }, xml: { wrapped: true, name: 'aliens' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal(expected); }); }); describe('object', function () { it('returns object with 2 properties', function () { var expected = '' + 'string' + '1' + ''; var name = 'animals'; var definition = { type: 'object', properties: { alien: { type: 'string' }, cat: { type: 'integer' } }, xml: { name: 'aliens' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal(expected); }); it('returns object with integer property and array property', function () { var expected = '' + 'string' + '1' + ''; var name = 'animals'; var definition = { type: 'object', properties: { aliens: { type: 'array', items: { type: 'string' } }, cat: { type: 'integer' } }, xml: { name: 'animals' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal(expected); }); it('returns object with integer property and array property', function () { var expected = '' + 'string' + '1' + ''; var name = 'animals'; var definition = { type: 'object', properties: { aliens: { type: 'array', items: { type: 'string', xml: { name: 'alien' } }, xml: { wrapped: true } }, cat: { type: 'integer' } }, xml: { name: 'animals' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal(expected); }); it('returns nested objects', function () { var expected = '' + '' + 'string' + '' + 'string' + ''; var name = 'animals'; var definition = { type: 'object', properties: { aliens: { type: 'object', properties: { alien: { type: 'string', xml: { name: 'alien' } } } }, cat: { type: 'string' } }, xml: { name: 'animals' } }; expect(sut.createSchemaXML(name, definition, models)).to.equal(expected); }); it('returns object with no readonly fields for parameter', function () { var expected = '' + '1'+ ''; var name = 'animals'; var definition = { type: 'object', properties: { id: { type: 'integer' }, cat: { readOnly: true, type: 'string' } }, xml: { name: 'animals' } }; expect(sut.createSchemaXML(name, definition, models, { isParam: true })).to.equal(expected); }); it('returns object with passed parameter as attribute', function () { var expected = '' + 'string' + ''; var name = 'animals'; var definition = { type: 'object', properties: { id: { type: 'integer', xml: { attribute: true } }, dog: { type: 'string' } }, xml: { name: 'animals' } }; expect(sut.createSchemaXML(name, definition, models, true)).to.equal(expected); }); }); describe('schema is in definitions', function () { it('returns array of Tags wrapped into Tags', function () { var expected = '' + '1string' + ''; var schema = { type: 'array', items: { $ref: '#/definitions/Tag' }, xml: { name: 'Tags', wrapped: true } }; expect(sut.createSchemaXML('', schema, models)).to.equal(expected); }); it('returns Object with properties Pet and name', function () { var expected = '' + '1' + '' + '1' + 'string' + '' + 'doggie' + '' + 'string' + '' + '' + '1string' + '' + 'available' + ''; var schema = { $ref: '#/definitions/Pet' }; expect(sut.createSchemaXML('', schema, models)).to.equal(expected); }); it('infinite loop Loop1 => Loop2, Loop2 => Loop1', function () { var expected = '' + '1' + '' + '1' + '' + '' + ''; var schema = { $ref: '#/definitions/Loop1' }; expect(sut.createSchemaXML('', schema, models)).to.equal(expected); }); it('infinite loop Loop3 => Loop1, Loop1 => Loop2, Loop2 => Loop1', function () { var expected = '' + '' + '1' + '' + '1' + '' + '' + '' + ''; var schema = { $ref: '#/definitions/Loop3' }; expect(sut.createSchemaXML('', schema, models)).to.equal(expected); }); }); }); });