Skip to content
This repository was archived by the owner on Jun 18, 2023. It is now read-only.

Commit 7a46ea9

Browse files
timonbackstavshamir
authored andcommitted
feat: Move getSchemaUrl to mapper
Add anchorUrl to models
1 parent b302b75 commit 7a46ea9

File tree

7 files changed

+51
-20
lines changed

7 files changed

+51
-20
lines changed

src/app/channels/channel-main/channel-main.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ <h4>{{ operation.message.description }}</h4>
2929
<h4>
3030
{{ schemaName }}
3131
<span class="payload-name">
32-
<a [href]="getSchemaUrl(operation.message.payload.$ref?.split('/')?.pop())" >{{ operation.message.payload.$ref }}</a>
32+
<a [href]="operation.message.payload.anchorUrl" >{{ operation.message.payload.name }}</a>
3333
</span>
3434
</h4>
3535
<app-schema *ngIf="schema" [schema]="schema"></app-schema>
@@ -38,7 +38,7 @@ <h4>
3838
<h4>
3939
{{ headersSchemaName }}
4040
<span class="header-name">
41-
<a [href]="getSchemaUrl(operation.message.headers.$ref?.split('/')?.pop())">{{ operation.message.headers.$ref }}</a>
41+
<a [href]="operation.message.headers.anchorUrl">{{ operation.message.headers.name }}</a>
4242
</span>
4343
</h4>
4444
<app-schema *ngIf="headers" [schema]="headers"></app-schema>

src/app/channels/channel-main/channel-main.component.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ export class ChannelMainComponent implements OnInit {
3939
this.asyncApiService.getAsyncApis().subscribe(
4040
asyncapi => {
4141
let schemas: Map<string, Schema> = asyncapi.components.schemas;
42-
this.schemaName = this.operation.message.payload.$ref.slice(this.operation.message.payload.$ref.lastIndexOf('/') + 1)
42+
this.schemaName = this.operation.message.payload.name.slice(this.operation.message.payload.name.lastIndexOf('/') + 1)
4343
this.schema = schemas.get(this.schemaName);
4444

4545
this.defaultExample = this.schema.example;
4646
this.exampleTextAreaLineCount = this.defaultExample?.lineCount || 0;
4747

48-
this.headersSchemaName = this.operation.message.headers.$ref.slice(this.operation.message.headers.$ref.lastIndexOf('/') + 1)
48+
this.headersSchemaName = this.operation.message.headers.name.slice(this.operation.message.headers.name.lastIndexOf('/') + 1)
4949
this.headers = schemas.get(this.headersSchemaName);
5050
this.headersExample = this.headers.example;
5151
this.headersTextAreaLineCount = this.headersExample?.lineCount || 0;
@@ -98,8 +98,4 @@ export class ChannelMainComponent implements OnInit {
9898
});
9999
}
100100

101-
public getSchemaUrl(fragment: string): string {
102-
return window.location.pathname + window.location.search + "#" + fragment;
103-
}
104-
105101
}

src/app/schemas/schema/schema.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</td>
88
<td>
99
<span class="type" *ngIf="property.value.type">{{ property.value.type }}</span>
10-
<span class="type" *ngIf="property.value['$ref']">
11-
<a [href]="getSchemaUrl(property.value['$ref']?.split('/')?.pop())">{{ property.value['$ref'] }}</a>
10+
<span class="type" *ngIf="property.value.name">
11+
<a [href]="property.value.anchorUrl">{{ property.value.name }}</a>
1212
</span>
1313
<span class="format" *ngIf="property.value.format">({{ property.value.format }})</span>
1414
<div class="description">{{ property.value.description }}</div>

src/app/schemas/schema/schema.component.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,4 @@ export class SchemaComponent {
1010

1111
@Input() schema: Schema;
1212

13-
public getSchemaUrl(fragment: string): string {
14-
return window.location.pathname + window.location.search + "#" + fragment;
15-
}
1613
}

src/app/shared/asyncapi-mapper.service.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface ServerAsyncApiSchema {
1515
[key: string]: object;
1616
};
1717
required?: string[];
18+
$ref?: string;
1819
}
1920

2021
interface ServerAsyncApiMessage {
@@ -31,6 +32,7 @@ interface ServerAsyncApiInfo {
3132
description?: string;
3233
}
3334

35+
export type ServerAsyncApiChannelMessage = ServerAsyncApiMessage | { oneOf: ServerAsyncApiMessage[] };
3436
export interface ServerAsyncApi {
3537
asyncapi: string;
3638
info: ServerAsyncApiInfo;
@@ -44,11 +46,11 @@ export interface ServerAsyncApi {
4446
[key: string]: {
4547
description?: string;
4648
subscribe?: {
47-
message: ServerAsyncApiMessage | { oneOf: ServerAsyncApiMessage[] };
49+
message: ServerAsyncApiChannelMessage;
4850
bindings?: any;
4951
};
5052
publish?: {
51-
message: ServerAsyncApiMessage | { oneOf: ServerAsyncApiMessage[] };
53+
message: ServerAsyncApiChannelMessage;
5254
bindings?: any;
5355
};
5456
};
@@ -60,6 +62,7 @@ export interface ServerAsyncApi {
6062

6163
@Injectable()
6264
export class AsyncApiMapperService {
65+
static BASE_URL = window.location.pathname + window.location.search + "#";
6366

6467
constructor() {
6568
}
@@ -106,7 +109,7 @@ export class AsyncApiMapperService {
106109
operationType: OperationType): Channel[]
107110
{
108111
if(serverOperation !== undefined) {
109-
let messages: Message[] = 'oneOf' in serverOperation.message ? serverOperation.message.oneOf : [serverOperation.message];
112+
let messages: Message[] = this.mapMessages(serverOperation.message)
110113

111114
return messages.map(message => {
112115
const operation = this.mapOperation(operationType, message, serverOperation.bindings)
@@ -121,6 +124,31 @@ export class AsyncApiMapperService {
121124
return [];
122125
}
123126

127+
private mapMessages(message: ServerAsyncApiChannelMessage): Message[] {
128+
if('oneOf' in message) {
129+
return this.mapServerAsyncApiMessage(message.oneOf)
130+
}
131+
return this.mapServerAsyncApiMessage([message]);
132+
}
133+
134+
private mapServerAsyncApiMessage(messages: ServerAsyncApiMessage[]): Message[] {
135+
return messages.map((v) => {
136+
return {
137+
name: v.name,
138+
title: v.title,
139+
description: v.description,
140+
payload: {
141+
name: v.payload.$ref,
142+
anchorUrl: AsyncApiMapperService.BASE_URL +v.payload.$ref?.split('/')?.pop()
143+
},
144+
headers: {
145+
name: v.headers.$ref,
146+
anchorUrl: AsyncApiMapperService.BASE_URL + v.headers.$ref?.split('/')?.pop()
147+
}
148+
}
149+
})
150+
}
151+
124152
private mapOperation(operationType: OperationType, message: Message, bindings?: any): Operation {
125153
return {
126154
protocol: this.getProtocol(bindings),
@@ -144,8 +172,10 @@ export class AsyncApiMapperService {
144172
const properties = schema.properties !== undefined ? this.mapSchemas(schema.properties) : undefined
145173
const example = schema.example !== undefined ? new Example(schema.example) : undefined
146174
return {
175+
name: schema.$ref,
147176
description: schema.description,
148177
anchorIdentifier: '#' + schemaName,
178+
anchorUrl: AsyncApiMapperService.BASE_URL + schema.$ref?.split('/')?.pop(),
149179
type: schema.type,
150180
format: schema.format,
151181
enum: schema.enum,

src/app/shared/models/channel.model.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export interface Message {
1818
name: string;
1919
title: string;
2020
description?: string;
21-
payload: { $ref: string };
22-
headers: { $ref: string };
21+
payload: {
22+
name: string;
23+
anchorUrl: string;
24+
};
25+
headers: {
26+
name: string
27+
anchorUrl: string;
28+
};
2329
}

src/app/shared/models/schema.model.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Example } from './example.model';
22

33
export interface Schema {
4+
name?: string;
45
description?: string;
5-
anchorIdentifier: string;
6-
type: string;
6+
anchorIdentifier?: string;
7+
anchorUrl?: string;
8+
type?: string;
79
format?: string;
810
enum?: string[];
911
properties?: Map<string, Schema>;

0 commit comments

Comments
 (0)