Skip to content

Commit

Permalink
treat examples as objects when possible. Fixes #671
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Wittmann <eric.wittmann@gmail.com>
  • Loading branch information
EricWittmann committed Mar 12, 2019
1 parent 3c5a32b commit 66aff7a
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- Add Example Dialog -->
<div>
<div bsModal #addExampleModal="bs-modal" class="modal fade" id="addExampleModal" tabindex="-1" role="dialog"
aria-labelledby="addExampleModalLabel" role="dialog" aria-hidden="true" *ngIf="isOpen()"
aria-labelledby="addExampleModalLabel" aria-hidden="true" *ngIf="isOpen()"
(onShown)="addExampleInput.focus()" (onHidden)="close()">
<div class="modal-dialog">
<div class="modal-content">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ export class AddExample20DialogComponent {
contentType: this.model.contentType,
value: this.model.value
};
console.info("EVENT: ", event);
// TODO investigate whether to restore this functionality (treat JSON data differently)
// if (this.model.valid && this.model.format === CodeEditorMode.JSON) {
// try {
// event.value = JSON.parse(this.model.value)
// } catch (e) {
// console.error("[AddExampleDialogComponent] Failed to parse example.");
// }
// }

// Convert to jsobject if the data is JSON and is valid
if (this.model.valid && this.model.format === CodeEditorMode.JSON) {
try {
event.value = JSON.parse(this.model.value)
} catch (e) { // should never happen!
console.error("[AddExample20DialogComponent] Failed to parse example.");
}
}
this.onAdd.emit(event);
this.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- Add Example Dialog -->
<div>
<div bsModal #addExampleModal="bs-modal" class="modal fade" id="addExampleModal" tabindex="-1" role="dialog"
aria-labelledby="addExampleModalLabel" role="dialog" aria-hidden="true" *ngIf="isOpen()"
aria-labelledby="addExampleModalLabel" aria-hidden="true" *ngIf="isOpen()"
(onShown)="addExampleInput.focus()" (onHidden)="close()">
<div class="modal-dialog">
<div class="modal-content">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,16 @@ export class AddExampleDialogComponent {
name: this.model.name,
value: this.model.value
};
// TODO investigate whether to restore this functionality (treat JSON data differently)
// if (this.model.valid && this.model.format === CodeEditorMode.JSON) {
// try {
// event.value = JSON.parse(this.model.value)
// } catch (e) {
// console.error("[AddExampleDialogComponent] Failed to parse example.");
// }
// }

// Convert to jsobject if the data is JSON and is valid
if (this.model.valid && this.model.format === CodeEditorMode.JSON) {
try {
event.value = JSON.parse(this.model.value)
} catch (e) { // should never happen!
console.error("[AddExampleDialogComponent] Failed to parse example.");
}
}

this.onAdd.emit(event);
this.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ export class EditExample20DialogComponent {
};

let val: any = value;
if (StringUtils.isJSON(val)) {
try { val = JSON.stringify(JSON.parse(val), null, 4); } catch (e) {}
}
if (typeof val === "object") {
if (typeof val === "object" || Array.isArray(val)) {
val = JSON.stringify(val, null, 4);
}

Expand Down Expand Up @@ -113,14 +110,15 @@ export class EditExample20DialogComponent {
contentType: this.model.contentType,
value: this.model.value
};
// TODO investigate whether to restore this functionality (treat JSON data differently)
// if (this.model.valid && this.model.format === CodeEditorMode.JSON) {
// try {
// event.value = JSON.parse(this.model.value);
// } catch (e) {
// console.error("[EditExample20DialogComponent] Failed to parse example.");
// }
// }

// Convert to jsobject if the data is JSON and is valid
if (this.model.valid && this.model.format === CodeEditorMode.JSON) {
try {
event.value = JSON.parse(this.model.value);
} catch (e) {
console.error("[EditExample20DialogComponent] Failed to parse example.");
}
}
this.onEdit.emit(event);
this.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class EditExampleDialogComponent {
};

let val: any = example.value;
if (typeof val === "object") {
if (typeof val === "object" || Array.isArray(val)) {
val = JSON.stringify(val, null, 4);
}

Expand Down Expand Up @@ -108,14 +108,15 @@ export class EditExampleDialogComponent {
example: this.example,
value: this.model.value
};
// TODO investigate whether to restore this functionality (treat JSON data differently)
// if (this.model.valid && this.model.format === CodeEditorMode.JSON) {
// try {
// event.value = JSON.parse(this.model.value);
// } catch (e) {
// console.error("[EditExampleDialogComponent] Failed to parse example.");
// }
// }

// Convert to jsobject if the data is JSON and is valid
if (this.model.valid && this.model.format === CodeEditorMode.JSON) {
try {
event.value = JSON.parse(this.model.value);
} catch (e) {
console.error("[EditExampleDialogComponent] Failed to parse example.");
}
}
this.onEdit.emit(event);
this.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {AbstractBaseComponent} from "../../common/base-component";
import {DocumentService} from "../../../_services/document.service";
import {SelectionService} from "../../../_services/selection.service";
import {ModelUtils} from "../../../_util/model.util";
import {StringUtils} from "apicurio-ts-core";


@Component({
Expand All @@ -36,16 +37,27 @@ export class DefinitionExampleSectionComponent extends AbstractBaseComponent {

@Input() definition: Oas20SchemaDefinition | Oas30SchemaDefinition;

/**
* C'tor.
* @param changeDetectorRef
* @param documentService
* @param commandService
* @param selectionService
*/
constructor(private changeDetectorRef: ChangeDetectorRef, private documentService: DocumentService,
private commandService: CommandService, private selectionService: SelectionService) {
super(changeDetectorRef, documentService, selectionService);
}

/**
* returns the example.
* Returns the example. Converts to a string if the example is an object.
*/
public example(): string {
return this.definition.example;
let value: string = this.definition.example;
if (typeof value === "object" || Array.isArray(value)) {
value = JSON.stringify(value, null, 4);
}
return value;
}

/**
Expand All @@ -54,8 +66,16 @@ export class DefinitionExampleSectionComponent extends AbstractBaseComponent {
*/
public onExampleChange(newExample: string): void {
console.info("[DefinitionExampleSectionComponent] User changed the data type example.");
let newValue: any = newExample;
if (StringUtils.isJSON(newValue)) {
try {
newValue = JSON.parse(newValue);
} catch (e) {
console.info("[DefinitionExampleSectionComponent] Failed to parse example: ", e);
}
}
let command: ICommand = createChangePropertyCommand(this.definition.ownerDocument(), this.definition,
"example", newExample);
"example", newValue);
this.commandService.emit(command);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<td class="content-type">
<span>{{ contentType }}</span>
</td>
<td class="value pre-actions">{{ exampleValue(contentType) }}</td>
<td class="value pre-actions">{{ exampleDisplayValue(contentType) }}</td>
<td class="actions">
<div>
<icon-button (onClick)="deleteExample(contentType)" [pullRight]="true" type="delete"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,19 @@ export class ResponseTabComponent extends AbstractBaseComponent {
return this.response.examples.exampleContentTypes();
}

public exampleValue(contentType: string): string {
public exampleDisplayValue(contentType: string): string {
let evalue: any = this.response.examples.example(contentType);
if (typeof evalue === "object" || Array.isArray(evalue)) {
evalue = JSON.stringify(evalue);
}
return evalue;
}

public exampleValue(contentType: string): string {
let evalue: any = this.response.examples.example(contentType);
return evalue;
}

public displayType(): SimplifiedType {
return SimplifiedType.fromSchema(this.response.schema);
}
Expand Down

0 comments on commit 66aff7a

Please sign in to comment.