Skip to content

Commit c35ea3a

Browse files
authored
Add value body identifier (#560)
1 parent 26238e6 commit c35ea3a

File tree

10 files changed

+76
-1
lines changed

10 files changed

+76
-1
lines changed

compiler/model/build-model.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ import {
5050
modelTypeAlias,
5151
parseVariantNameTag,
5252
parseVariantsTag,
53-
verifyUniqueness
53+
verifyUniqueness,
54+
parseJsDocTags
5455
} from './utils'
5556

5657
const specsFolder = join(__dirname, '..', '..', 'specification')
@@ -214,6 +215,14 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int
214215
)
215216
)]
216217

218+
const pathAndQueryProperties = (declaration.getMembers() as any[]).flatMap(member => {
219+
const property = visitRequestOrResponseProperty(member)
220+
if (property.name === 'path_parts' || property.name === 'query_parameters') {
221+
return property.properties.map(property => property.name)
222+
} else if (property.name === 'body') {
223+
return undefined
224+
}
225+
})
217226
for (const member of declaration.getMembers()) {
218227
// we are visiting `path_parts, `query_parameters` or `body`
219228
assert(
@@ -242,6 +251,22 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int
242251
type.body = { kind: 'no_body' }
243252
} else {
244253
type.body = { kind: 'value', value: property.valueOf }
254+
const openGenerics = declaration.getTypeParameters().map(modelGenerics)
255+
if ((property.valueOf.kind === 'instance_of' && openGenerics.includes(property.valueOf.type.name)) ||
256+
property.valueOf.kind === 'array_of') {
257+
const tags = parseJsDocTags(member.getJsDocs())
258+
assert(
259+
member,
260+
tags.identifier != null,
261+
'You should configure a body @identifier'
262+
)
263+
assert(
264+
member.getJsDocs(),
265+
!pathAndQueryProperties.includes(tags.identifier),
266+
`The identifier '${tags.identifier}' already exists as a property in the path or query.`
267+
)
268+
type.body.identifier = tags.identifier
269+
}
245270
}
246271
} else if (property.properties.length > 0) {
247272
type.body = { kind: 'properties', properties: property.properties }

compiler/model/metamodel.ts

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ export type Body = ValueBody | PropertiesBody | NoBody
265265
export class ValueBody {
266266
kind: 'value'
267267
value: ValueOf
268+
identifier?: string
268269
}
269270

270271
export class PropertiesBody {

docs/modeling-guide.md

+37
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,40 @@ class Foo {
398398
faz: string
399399
}
400400
```
401+
402+
#### `@identifier`
403+
404+
A custom name that can be used to display the property. Useful in Enums an
405+
for request bodies where the document is the entire body.
406+
407+
```ts
408+
export class ConfusionMatrixThreshold {
409+
/**
410+
* True Positive
411+
* @identifier true_positive
412+
*/
413+
tp: integer
414+
/**
415+
* False Positive
416+
* @identifier false_positive
417+
*/
418+
fp: integer
419+
/**
420+
* True Negative
421+
* @identifier true_negative
422+
*/
423+
tn: integer
424+
/**
425+
* False Negative
426+
* @identifier false_negative
427+
*/
428+
fn: integer
429+
}
430+
431+
export interface Request<TDocument> extends RequestBase {
432+
path_parts?: {}
433+
query_parameters?: {}
434+
/** @identifier document */
435+
body?: TDocument
436+
}
437+
```

output/schema/schema.json

+6
Original file line numberDiff line numberDiff line change
@@ -12971,6 +12971,7 @@
1297112971
"CommonQueryParameters"
1297212972
],
1297312973
"body": {
12974+
"identifier": "operations",
1297412975
"kind": "value",
1297512976
"value": {
1297612977
"kind": "array_of",
@@ -13885,6 +13886,7 @@
1388513886
"CommonQueryParameters"
1388613887
],
1388713888
"body": {
13889+
"identifier": "document",
1388813890
"kind": "value",
1388913891
"value": {
1389013892
"kind": "instance_of",
@@ -16865,6 +16867,7 @@
1686516867
"CommonQueryParameters"
1686616868
],
1686716869
"body": {
16870+
"identifier": "document",
1686816871
"kind": "value",
1686916872
"value": {
1687016873
"kind": "instance_of",
@@ -17913,6 +17916,7 @@
1791317916
"CommonQueryParameters"
1791417917
],
1791517918
"body": {
17919+
"identifier": "searches",
1791617920
"kind": "value",
1791717921
"value": {
1791817922
"kind": "array_of",
@@ -18224,6 +18228,7 @@
1822418228
"CommonQueryParameters"
1822518229
],
1822618230
"body": {
18231+
"identifier": "search_templates",
1822718232
"kind": "value",
1822818233
"value": {
1822918234
"kind": "array_of",
@@ -134185,6 +134190,7 @@
134185134190
},
134186134191
{
134187134192
"body": {
134193+
"identifier": "text_files",
134188134194
"kind": "value",
134189134195
"value": {
134190134196
"kind": "array_of",

specification/_global/bulk/BulkRequest.ts

+1
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ export interface Request<TSource> extends RequestBase {
5252
wait_for_active_shards?: WaitForActiveShards
5353
require_alias?: boolean
5454
}
55+
/** @identifier operations */
5556
body?: Array<OperationContainer | TSource>
5657
}

specification/_global/create/CreateRequest.ts

+1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ export interface Request<TDocument> extends RequestBase {
5151
version_type?: VersionType
5252
wait_for_active_shards?: WaitForActiveShards
5353
}
54+
/** @identifier document */
5455
body?: TDocument
5556
}

specification/_global/index/IndexRequest.ts

+1
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ export interface Request<TDocument> extends RequestBase {
5757
wait_for_active_shards?: WaitForActiveShards
5858
require_alias?: boolean
5959
}
60+
/** @identifier document */
6061
body?: TDocument
6162
}

specification/_global/msearch/MultiSearchRequest.ts

+1
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,6 @@ export interface Request extends RequestBase {
8787
*/
8888
typed_keys?: boolean
8989
}
90+
/** @identifier searches */
9091
body?: Array<Header | Body>
9192
}

specification/_global/msearch_template/MultiSearchTemplateRequest.ts

+1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ export interface Request extends RequestBase {
4040
rest_total_hits_as_int?: boolean
4141
typed_keys?: boolean
4242
}
43+
/** @identifier search_templates */
4344
body: Array<TemplateItem>
4445
}

specification/text_structure/find_structure/FindStructureRequest.ts

+1
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,6 @@ export interface Request<TJsonDocument> {
6868
/** The Java time format of the timestamp field in the text. */
6969
timestamp_format?: string
7070
}
71+
/** @identifier text_files */
7172
body: Array<TJsonDocument>
7273
}

0 commit comments

Comments
 (0)