Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display examples of unknown content type as plain text #462

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -539,6 +550,7 @@ function html(markdown,header,options) {
module.exports = {
statusCodes : statusCodes,
doContentType : doContentType,
hasUnknownContentType : hasUnknownContentType,
languageCheck : languageCheck,
getCodeSamples : getCodeSamples,
inferType : inferType,
Expand All @@ -554,4 +566,3 @@ module.exports = {
toPrimitive: toPrimitive,
html : html
};

10 changes: 10 additions & 0 deletions lib/openapi3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 15 additions & 1 deletion test/contentType.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand All @@ -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(){
Expand All @@ -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']));
});
});
});