Skip to content

Commit

Permalink
feat: support text-plain response sample
Browse files Browse the repository at this point in the history
closes #270
  • Loading branch information
RomanHotsiy committed Aug 17, 2017
1 parent 955ef71 commit b84177c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions demo/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ paths:
examples:
application/json: OK
application/xml: <message> OK </message>
text/plain: OK
headers:
X-Rate-Limit:
type: integer
Expand Down
10 changes: 9 additions & 1 deletion lib/components/SchemaSample/schema-sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@
<div class="action-buttons">
<span copy-button [copyText]="xmlSample" class="hint--top-left hint--inversed"> <a>Copy</a> </span>
</div>
<pre class="xml-sample" [innerHtml]="xmlSample | prism:'xml'"></pre>
<pre class="response-sample" [innerHtml]="xmlSample | prism:'xml'"></pre>
</div>
</tab>
<tab tabTitle="text/plain" *ngIf="textSample">
<div class="snippet">
<div class="action-buttons">
<span copy-button [copyText]="xmlSample" class="hint--top-left hint--inversed"> <a>Copy</a> </span>
</div>
<pre class="response-sample">{{textSample}}</pre>
</div>
</tab>
</tabs>
2 changes: 1 addition & 1 deletion lib/components/SchemaSample/schema-sample.scss
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pre {
padding-left: 6px;
}

.redoc-json, .xml-sample {
.redoc-json, .response-sample {
overflow-x: auto;
padding: 20px;
border-radius: $border-radius*2;
Expand Down
9 changes: 4 additions & 5 deletions lib/components/SchemaSample/schema-sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as OpenAPISampler from 'openapi-sampler';
import JsonPointer from '../../utils/JsonPointer';
import { BaseComponent, SpecManager } from '../base';
import { SchemaNormalizer } from '../../services/schema-normalizer.service';
import { getJsonLikeSample, getXmlLikeSample} from '../../utils/helpers';
import { getJsonLikeSample, getXmlLikeSample, getTextLikeSample } from '../../utils/helpers';

@Component({
selector: 'schema-sample',
Expand All @@ -21,6 +21,7 @@ export class SchemaSample extends BaseComponent implements OnInit {
element: any;
sample: any;
xmlSample: string;
textSample: string;
enableButtons: boolean = false;

private _normalizer:SchemaNormalizer;
Expand Down Expand Up @@ -51,10 +52,8 @@ export class SchemaSample extends BaseComponent implements OnInit {
base.examples = requestExamples;
}

let xmlLikeSample = base.examples && getXmlLikeSample(base.examples);
if (xmlLikeSample) {
this.xmlSample = xmlLikeSample;
}
this.xmlSample = base.examples && getXmlLikeSample(base.examples);
this.textSample = base.examples && getTextLikeSample(base.examples);

let jsonLikeSample = base.examples && getJsonLikeSample(base.examples);
if (jsonLikeSample) {
Expand Down
15 changes: 15 additions & 0 deletions lib/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ export function isXmlLike(contentType: string): boolean {
return contentType.search(/xml/i) !== -1;
}

export function isTextLike(contentType: string): boolean {
return contentType.search(/text\/plain/i) !== -1;
}

export function getJsonLikeSample(samples: Object = {}) {
const jsonLikeKeys = Object.keys(samples).filter(isJsonLike);

Expand All @@ -161,3 +165,14 @@ export function getXmlLikeSample(samples: Object = {}) {

return samples[xmlLikeKeys[0]];
}


export function getTextLikeSample(samples: Object = {}) {
const textLikeKeys = Object.keys(samples).filter(isTextLike);

if (!textLikeKeys.length) {
return false;
}

return samples[textLikeKeys[0]];
}

0 comments on commit b84177c

Please sign in to comment.