Skip to content

Commit db06f30

Browse files
Anaetheliongithub-actions[bot]
authored andcommitted
[compiler] Include exceptions to schema (#4695)
* compiler: validate types found only in exceptions * split indices.get_alias for clarity, handle NotFoundAliases with previous comit * making the compiler linter happy again * align generics expansion between rust and typescript * compiler: propagate generics in case someone want to have a generic exception * once again, making the linter happy (cherry picked from commit 1c2d7f3)
1 parent c5ecfba commit db06f30

File tree

5 files changed

+70
-14
lines changed

5 files changed

+70
-14
lines changed

compiler-rs/clients_schema/src/transform/expand_generics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,10 @@ pub fn expand(model: IndexedModel, config: ExpandConfig) -> anyhow::Result<Index
211211

212212
expand_behaviors(&mut resp.behaviors, &mappings, model, ctx)?;
213213
expand_body(&mut resp.body, &mappings, model, ctx)?;
214-
215-
// TODO: exceptions
214+
215+
for exception in &mut resp.exceptions {
216+
expand_body(&mut exception.body, &mappings, model, ctx)?;
217+
}
216218

217219
Ok(resp.into())
218220
}

compiler/src/steps/validate-model.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,23 @@ export default async function validateModel (apiModel: model.Model, restSpec: Ma
497497
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
498498
throw new Error(`Unknown kind: ${typeDef.body.kind}`)
499499
}
500+
501+
if (typeDef.exceptions != null) {
502+
for (const ex of typeDef.exceptions) {
503+
switch (ex.body.kind) {
504+
case 'properties':
505+
validateProperties(ex.body.properties, openGenerics, new Set<string>())
506+
break
507+
case 'value':
508+
validateValueOf(ex.body.value, openGenerics)
509+
break
510+
case 'no_body':
511+
// Nothing to validate
512+
break
513+
}
514+
}
515+
}
516+
500517
context.pop()
501518
}
502519

compiler/src/transform/expand-generics.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
Request,
2828
Response,
2929
Body,
30-
InstanceOf, Inherits, Property
30+
InstanceOf, Inherits, Property, ResponseException
3131
} from '../model/metamodel'
3232
import { readFile, writeFile } from 'fs/promises'
3333
import stringify from 'safe-stable-stringify'
@@ -293,6 +293,17 @@ export function expandGenerics (inputModel: Model, config?: ExpansionConfig): Mo
293293
return addIfNotSeen(resp.name, () => {
294294
const result = { ...resp }
295295
result.body = expandBody(resp.body, genericParamMapping(resp.generics, params))
296+
if (resp.exceptions != null) {
297+
result.exceptions = []
298+
for (const exception of resp.exceptions) {
299+
const except: ResponseException = {
300+
description: exception.description,
301+
statusCodes: exception.statusCodes,
302+
body: expandBody(exception.body, genericParamMapping(resp.generics, params))
303+
}
304+
result.exceptions.push(except)
305+
}
306+
}
296307
result.generics = undefined
297308
return result
298309
})

specification/indices/get_alias/IndicesGetAliasResponse.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
import { ErrorResponseBase } from '@_types/Base'
2121
import { IndexName } from '@_types/common'
22-
import { AliasDefinition } from '@indices/_types/AliasDefinition'
23-
import { AdditionalProperties } from '@spec_utils/behaviors'
22+
import {
23+
IndexAliases,
24+
NotFoundAliases
25+
} from '@indices/get_alias/_types/response'
2426
import { Dictionary } from '@spec_utils/Dictionary'
2527

2628
export class Response {
@@ -33,12 +35,3 @@ export class Response {
3335
}
3436
]
3537
}
36-
37-
export class IndexAliases {
38-
aliases: Dictionary<string, AliasDefinition>
39-
}
40-
41-
class NotFoundAliases implements AdditionalProperties<string, IndexAliases> {
42-
error: string
43-
status: number
44-
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { AliasDefinition } from '@indices/_types/AliasDefinition'
21+
import { AdditionalProperties } from '@spec_utils/behaviors'
22+
import { Dictionary } from '@spec_utils/Dictionary'
23+
24+
export class IndexAliases {
25+
aliases: Dictionary<string, AliasDefinition>
26+
}
27+
28+
export class NotFoundAliases
29+
implements AdditionalProperties<string, IndexAliases>
30+
{
31+
error: string
32+
status: number
33+
}

0 commit comments

Comments
 (0)