mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-26 18:43:43 +03:00
Allow JSON-like examples to be shown, which supports things like JSON API
This commit is contained in:
parent
9ccfd52640
commit
e72471feab
|
@ -3,7 +3,7 @@
|
||||||
import { Component, Input, OnInit, ChangeDetectionStrategy } from '@angular/core';
|
import { Component, Input, OnInit, ChangeDetectionStrategy } from '@angular/core';
|
||||||
import { BaseComponent, SpecManager } from '../base';
|
import { BaseComponent, SpecManager } from '../base';
|
||||||
import JsonPointer from '../../utils/JsonPointer';
|
import JsonPointer from '../../utils/JsonPointer';
|
||||||
import { statusCodeType } from '../../utils/helpers';
|
import { statusCodeType, getJsonLike } from '../../utils/helpers';
|
||||||
|
|
||||||
|
|
||||||
function isNumeric(n) {
|
function isNumeric(n) {
|
||||||
|
@ -11,7 +11,7 @@ function isNumeric(n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasExample(response) {
|
function hasExample(response) {
|
||||||
return ((response.examples && response.examples['application/json']) ||
|
return ((response.examples && getJsonLike(response.examples)) ||
|
||||||
response.schema);
|
response.schema);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import * as OpenAPISampler from 'openapi-sampler';
|
||||||
|
|
||||||
import { BaseComponent, SpecManager } from '../base';
|
import { BaseComponent, SpecManager } from '../base';
|
||||||
import { SchemaNormalizer } from '../../services/schema-normalizer.service';
|
import { SchemaNormalizer } from '../../services/schema-normalizer.service';
|
||||||
|
import { getJsonLike } from '../../utils/helpers';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'schema-sample',
|
selector: 'schema-sample',
|
||||||
|
@ -42,8 +43,9 @@ export class SchemaSample extends BaseComponent implements OnInit {
|
||||||
this.pointer += '/schema';
|
this.pointer += '/schema';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (base.examples && base.examples['application/json']) {
|
let jsonLikeSample = base.examples && getJsonLike(base.examples);
|
||||||
sample = base.examples['application/json'];
|
if (jsonLikeSample) {
|
||||||
|
sample = jsonLikeSample;
|
||||||
} else {
|
} else {
|
||||||
let selectedDescendant;
|
let selectedDescendant;
|
||||||
|
|
||||||
|
|
|
@ -114,3 +114,17 @@ export function snapshot(obj) {
|
||||||
|
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isJsonLike(contentType: string): boolean {
|
||||||
|
return contentType.search(/json/i) !== -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getJsonLike(object: object) {
|
||||||
|
const jsonLikeKeys = Object.keys(object).filter(isJsonLike);
|
||||||
|
|
||||||
|
if (!jsonLikeKeys.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return object[jsonLikeKeys.shift()];
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import {statusCodeType} from '../../lib/utils/helpers';
|
import {statusCodeType, isJsonLike, getJsonLike } from '../../lib/utils/helpers';
|
||||||
|
|
||||||
describe('Utils', () => {
|
describe('Utils', () => {
|
||||||
describe('statusCodeType', () => {
|
describe('statusCodeType', () => {
|
||||||
it('Should return info for status codes within 100 and 200', ()=> {
|
it('Should return info for status codes within 100 and 200', ()=> {
|
||||||
|
@ -30,4 +31,34 @@ describe('Utils', () => {
|
||||||
(() => statusCodeType(600)).should.throw('invalid HTTP code');
|
(() => statusCodeType(600)).should.throw('invalid HTTP code');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('isJsonLike', () => {
|
||||||
|
it('Should return true for a string that contains `json`', () => {
|
||||||
|
isJsonLike('application/json').should.be.equal(true);
|
||||||
|
});
|
||||||
|
it('Should return false for a string that does not contain `json`', () => {
|
||||||
|
isJsonLike('application/xml').should.be.equal(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getJsonLike', () => {
|
||||||
|
it('Should return a value when a JSON-like key exists', () => {
|
||||||
|
const examples = {
|
||||||
|
"application/vnd.api+json": {
|
||||||
|
"message": "Hello World"
|
||||||
|
},
|
||||||
|
"application/xml": "<message>Hello World</message>"
|
||||||
|
};
|
||||||
|
|
||||||
|
(getJsonLike(examples).message).should.be.equal("Hello World");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should return undefined when no JSON-like key exists', () => {
|
||||||
|
const examples = {
|
||||||
|
"application/xml": "<message>Hello World</message>"
|
||||||
|
};
|
||||||
|
|
||||||
|
getJsonLike(examples).should.be.equal(false);
|
||||||
|
});
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user