diff --git a/lib/common.js b/lib/common.js index 9c45e409..468e94ed 100644 --- a/lib/common.js +++ b/lib/common.js @@ -63,6 +63,17 @@ function doContentType(ctTypes, ctClass) { return false; } +function hasUnknownContentType(ctTypes) { + for (let type of ctTypes) { + for (let ctClass in Object.keys(contentTypes)) { + for (let target of contentTypes[ctClass]||[]) { + if (type.match(target)) return false; + } + } + } + return true; +} + function languageCheck(language, language_tabs, mutate) { var lcLang = language.toLowerCase(); if (lcLang === 'c#') lcLang = 'csharp'; @@ -539,6 +550,7 @@ function html(markdown,header,options) { module.exports = { statusCodes : statusCodes, doContentType : doContentType, + hasUnknownContentType : hasUnknownContentType, languageCheck : languageCheck, getCodeSamples : getCodeSamples, inferType : inferType, @@ -554,4 +566,3 @@ module.exports = { toPrimitive: toPrimitive, html : html }; - diff --git a/lib/openapi3.js b/lib/openapi3.js index e159ce4f..ea1223a5 100644 --- a/lib/openapi3.js +++ b/lib/openapi3.js @@ -369,6 +369,11 @@ function getBodyParameterExamples(data) { content += xml.getXml(JSON.parse(common.safejson(obj)), '@', '', true, ' ', false) + '\n'; content += '```\n\n'; } + if (common.hasUnknownContentType(data.consumes) && (typeof obj === 'string')) { + content += '```\n'; + content += obj + '\n'; + content += '```\n\n'; + } return content; } @@ -552,6 +557,11 @@ function getResponseExamples(data) { content += xml.getXml(JSON.parse(common.safejson(xmlObj)), '@', '', true, ' ', false) + '\n'; content += '```\n\n'; } + if ((typeof example.value === 'string') && common.hasUnknownContentType(example.cta)) { + content += '```\n'; + content += example.value + '\n'; + content += '```\n\n'; + } } return content; } diff --git a/test/contentType.test.js b/test/contentType.test.js index 637a39c1..36c4793f 100644 --- a/test/contentType.test.js +++ b/test/contentType.test.js @@ -101,6 +101,15 @@ describe('contentType tests',function(){ }); }); + describe('unknown tests', function(){ + it('should match application/x-custom-type',function(){ + assert(common.hasUnknownContentType(['application/x-custom-type'])); + }); + it('should not match text/plain',function(){ + assert(!common.hasUnknownContentType(['text/plain'])); + }); + }); + }); describe('array tests',function(){ @@ -112,6 +121,9 @@ describe('array tests',function(){ it('should match another type and application/json',function(){ assert(common.doContentType(['text/plain','application/json'],'json')); }); + it('should match another type and application/x-custom-type',function(){ + assert(common.hasUnknownContentType(['text/plain','application/x-custom-type'])); + }); }); describe('negative tests',function(){ it('should not match two other types',function(){ @@ -123,6 +135,8 @@ describe('array tests',function(){ it('should not match an unknown format',function(){ assert(!common.doContentType(['application/octet-stream'],'file')); }); + it('should not match known types',function(){ + assert(!common.hasUnknownContentType(['text/plain'])); + }); }); }); -