Skip to content

Commit 5d98fe4

Browse files
committed
Boolean input and multi items for for array object input.
1 parent acebdcc commit 5d98fe4

File tree

8 files changed

+52
-13
lines changed

8 files changed

+52
-13
lines changed

projects/openapi-viewer/src/lib/openapi-viewer.scss

+8
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,10 @@ oav-method-header {
460460
box-sizing: border-box;
461461
}
462462

463+
oav-code-input {
464+
flex-grow: 1;
465+
}
466+
463467
button {
464468
color: #fff;
465469
background-color: #a7a7ac;
@@ -475,6 +479,10 @@ oav-method-header {
475479
}
476480
}
477481

482+
.small-editor .CodeMirror {
483+
height: 50px;
484+
}
485+
478486
.oav-add-btn {
479487
color: #fff;
480488
background-color: #a7a7ac;

projects/openapi-viewer/src/lib/parameter/parameter.component.html

+7-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
<input type="number" [formControlName]="parameter.name" [placeholder]="parameter.name" [name]="parameter.name" />
3030
</ng-container>
3131

32+
<ng-container *ngIf="displayMode === 'boolean'">
33+
<input type="checkbox" [formControlName]="parameter.name" [name]="parameter.name" />
34+
</ng-container>
35+
3236
<ng-container *ngIf="displayMode === 'file'">
3337
<input type="file" [formControlName]="parameter.name" [placeholder]="parameter.name" [name]="parameter.name" />
3438
</ng-container>
@@ -43,14 +47,14 @@
4347
</select>
4448
</ng-container>
4549

46-
<ng-container *ngIf="displayMode === 'arrayWithPrimitive'">
47-
<oav-multi-items-input [formControlName]="parameter.name"></oav-multi-items-input>
50+
<ng-container *ngIf="displayMode === 'arrayWithPrimitive' || displayMode === 'arrayWithObject'">
51+
<oav-multi-items-input [formControlName]="parameter.name" [type]="itemType"></oav-multi-items-input>
4852
</ng-container>
4953
</div>
5054
<div class="description">
5155
<markdown [data]="parameter.description"></markdown>
5256

53-
<ng-container *ngIf="displayMode === 'object' || displayMode === 'array'">
57+
<ng-container *ngIf="displayMode === 'object' || displayMode === 'array' || displayMode === 'arrayWithObject'">
5458
<oav-json-schema [schema]="parameter.schema"></oav-json-schema>
5559
</ng-container>
5660
</div>

projects/openapi-viewer/src/lib/parameter/parameter.component.ts

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export class ParameterComponent implements OnChanges, OnDestroy {
2222

2323
enum: any[] = null;
2424

25+
itemType: string;
26+
2527
constructor() {}
2628

2729
ngOnChanges(changes: SimpleChanges): void {
@@ -32,6 +34,9 @@ export class ParameterComponent implements OnChanges, OnDestroy {
3234
if (this.displayMode === 'object' || this.displayMode === 'array') {
3335
this.value = JSON.stringify(this.value, null, 2);
3436
}
37+
if (this.displayMode === 'arrayWithObject') {
38+
this.itemType = 'object';
39+
}
3540

3641
const validators = [];
3742
if (this.parameter.required) {

projects/openapi-viewer/src/lib/shared-components/multi-items-input/multi-items-input.component.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<ng-container [formGroup]="formGroup">
22
<div [formArrayName]="name">
33
<div class="multi-item-entry" *ngFor="let control of controls; let i = index">
4-
<input type="text" [formControlName]="i.toString()" (blur)="onTouched()" />
4+
<ng-container *ngIf="type === 'object'">
5+
<oav-code-input [formControlName]="i.toString()" mode="json" class="small-editor"></oav-code-input>
6+
</ng-container>
7+
<ng-container *ngIf="type !== 'object'">
8+
<input type="text" [formControlName]="i.toString()" (blur)="onTouched()" />
9+
</ng-container>
510
<button type="button" (click)="removeValue(i)" [disabled]="disabled">-</button>
611
</div>
712
</div>

projects/openapi-viewer/src/lib/shared-components/multi-items-input/multi-items-input.component.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { AbstractControl, ControlValueAccessor, FormArray, FormControl, FormGrou
1515
export class MultiItemsInputComponent implements OnInit, OnDestroy, ControlValueAccessor {
1616
@Input() name: string;
1717

18+
@Input() type: string;
19+
1820
@Input() required;
1921

2022
disabled = false;

projects/openapi-viewer/src/lib/util/parameter-input.util.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ const displayModes = [
2929
checkV3: (param: BaseParameterObject) => schemaType(param) === 'integer',
3030
getDefault: (param: BaseParameterObject, mediaType: string) => getExample(param, mediaType)
3131
},
32+
{
33+
mode: 'boolean',
34+
checkV2: (param: BaseParameterObject) => param.type === 'boolean',
35+
checkV3: (param: BaseParameterObject) => schemaType(param) === 'boolean',
36+
getDefault: (param: BaseParameterObject, mediaType: string) => getExample(param, mediaType)
37+
},
3238
{
3339
mode: 'object',
3440
checkV2: (param: BaseParameterObject) => param.type === 'object',
@@ -47,6 +53,12 @@ const displayModes = [
4753
checkV3: (param: BaseParameterObject) => schemaType(param) === 'array' && primitiveTypes.includes(itemsType(param.schema)),
4854
getDefault: (param: BaseParameterObject, mediaType: string) => getExample(param, mediaType)
4955
},
56+
{
57+
mode: 'arrayWithObject',
58+
checkV2: (param: BaseParameterObject) => param.type === 'array' && itemsType(param) === 'object',
59+
checkV3: (param: BaseParameterObject) => schemaType(param) === 'array' && itemsType(param.schema) === 'object',
60+
getDefault: (param: BaseParameterObject, mediaType: string) => getExample(param, mediaType, true)
61+
},
5062
{
5163
mode: 'array',
5264
checkV2: (param: BaseParameterObject) => param.type === 'array',
@@ -81,7 +93,17 @@ function itemsType(obj: { items?: any } | any) {
8193
console.warn('items type not found', obj);
8294
}
8395

84-
function getExample(param: BaseParameterObject, mediaType: string): any {
96+
function getExample(param: BaseParameterObject, mediaType: string, onlyDefault = false): any {
97+
if (param.type === 'array' && param.items && 'default' in param.items) {
98+
return [param.items.default];
99+
}
100+
if (isPrimitiveType(param.type) && param.items && 'default' in param.items) {
101+
return param.items.default;
102+
}
103+
if (onlyDefault) {
104+
return;
105+
}
106+
85107
if (param.example !== undefined) {
86108
return param.example;
87109
}
@@ -91,12 +113,6 @@ function getExample(param: BaseParameterObject, mediaType: string): any {
91113
if (param.schema) {
92114
return exampleFromSchema(param.schema);
93115
}
94-
if (param.type === 'array' && param.items && 'default' in param.items) {
95-
return [param.items.default];
96-
}
97-
if (isPrimitiveType(param.type) && param.items && 'default' in param.items) {
98-
return param.items.default;
99-
}
100116
return exampleFromSchema(param as any);
101117
}
102118

src/app/app-routing.module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Routes, RouterModule } from '@angular/router';
44
const routes: Routes = [];
55

66
@NgModule({
7-
imports: [RouterModule.forRoot(routes)],
7+
imports: [RouterModule.forRoot(routes, { useHash: true })],
88
exports: [RouterModule]
99
})
1010
export class AppRoutingModule {}

src/app/app.component.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import { OpenAPIObject } from 'openapi3-ts';
33
import { ActivatedRoute } from '@angular/router';
4-
import { query } from '@angular/animations';
54

65
@Component({
76
selector: 'app-root',

0 commit comments

Comments
 (0)