Skip to content

Commit fe94108

Browse files
committed
enable postprocessing and optimize the log
Signed-off-by: xil <fridalu66@gmail.com>
1 parent 99bfb7e commit fe94108

21 files changed

Lines changed: 84 additions & 81 deletions

.github/workflows/convert-proto.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,8 @@ jobs:
121121
run: |
122122
java -jar cloned-repo/modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -c tools/proto-convert/src/config/protobuf-generator-config.yaml
123123
124-
# - name: Post Process Protobuf
125-
# run: npm run postprocessing
126-
127-
- name: Reformat proto files
128-
run: |
129-
buf format -w protos/generated
124+
- name: Post Process Protobuf
125+
run: npm run postprocessing
130126

131127
- name: Configure Git User
132128
run: |

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"license": "Apache-2.0",
77
"scripts": {
88
"preprocessing": "ts-node tools/proto-convert/src/PreProcessing.ts",
9-
"backward-compat": "ts-node tools/proto-convert/src/backward-compat/BackwardCompatibleWriter.ts",
10-
"cleanup-common": "ts-node tools/proto-convert/src/backward-compat/CleanupUnusedMessages.ts -i protos/schemas/common.proto",
9+
"backward-compat": "ts-node tools/proto-convert/src/postprocessing/BackwardCompatibleWriter.ts",
10+
"cleanup-common": "ts-node tools/proto-convert/src/postprocessing/CleanupUnusedMessages.ts -i protos/schemas/common.proto",
1111
"postprocessing": "npm run backward-compat && npm run cleanup-common",
12-
"test": "npx jest --testMatch='**/*.test.ts --no-watchman'"
12+
"test": "npx jest --testMatch='**/*.test.ts' --no-watchman"
1313
},
1414
"dependencies": {
1515
"@apidevtools/swagger-parser": "^12.1.0",

protos/schemas/common.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,9 @@ message QueryContainer {
14331433
// To combine relevance scores from multiple queries into one score for a given document.
14341434
HybridQuery hybrid = 32;
14351435

1436+
// Return documents using a template query contains placeholders that are resolved by search request processors during query execution.
1437+
ObjectMap template = 33;
1438+
14361439
}
14371440
}
14381441

@@ -3194,6 +3197,7 @@ enum VersionType {
31943197
VERSION_TYPE_EXTERNAL = 1;
31953198
VERSION_TYPE_EXTERNAL_GTE = 2;
31963199
VERSION_TYPE_INTERNAL = 3;
3200+
VERSION_TYPE_FORCE = 4;
31973201
}
31983202

31993203
enum Refresh {
@@ -3393,6 +3397,7 @@ enum FieldType {
33933397
FIELD_TYPE_WILDCARD = 44;
33943398
FIELD_TYPE_XY_POINT = 45;
33953399
FIELD_TYPE_XY_SHAPE = 46;
3400+
FIELD_TYPE_SEMANTIC = 47;
33963401
}
33973402

33983403
enum SortOrder {
@@ -3444,6 +3449,7 @@ enum HighlighterOrder {
34443449
enum HighlighterTagsSchema {
34453450
HIGHLIGHTER_TAGS_SCHEMA_UNSPECIFIED = 0;
34463451
HIGHLIGHTER_TAGS_SCHEMA_STYLED = 1;
3452+
HIGHLIGHTER_TAGS_SCHEMA_DEFAULT = 2;
34473453
}
34483454

34493455
enum DistanceUnit {

tools/proto-convert/src/Filter.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type OpenAPIV3 } from 'openapi-types'
22
import _ from "lodash";
3-
import Logger from "./utils/logger"
3+
import logger from "./utils/logger"
44
import { getSchemaNames } from "./utils/helper"
55

66
/**
@@ -38,18 +38,18 @@ function traverse_and_enqueue(node: any, queue: string[], visited: Set<string>,
3838
* Schemas in the excluded set are skipped.
3939
*/
4040
export default class Filter {
41-
logger: Logger
4241
protected _spec: Record<string, any>
42+
protected sourceSpec: Record<string, any>
4343
protected targetPaths: string[]
4444
protected excludedSchemas: Set<string>
4545
paths: Record<string, Record<string, OpenAPIV3.PathItemObject>> = {} // namespace -> path -> path_item_object
4646

47-
constructor(logger: Logger, targetPaths: string[], excludedSchemas: Set<string> = new Set()) {
48-
this.logger = logger
47+
constructor(sourceSpec: Record<string, any>, targetPaths: string[], excludedSchemas: Set<string> = new Set()) {
48+
this.sourceSpec = sourceSpec;
4949
this.targetPaths = targetPaths;
5050
this.excludedSchemas = excludedSchemas;
5151
if (this.excludedSchemas.size > 0) {
52-
this.logger.info(`Loaded ${this.excludedSchemas.size} excluded schemas: ${Array.from(this.excludedSchemas).join(', ')}`);
52+
logger.info(`Loaded ${this.excludedSchemas.size} excluded schemas: ${Array.from(this.excludedSchemas).join(', ')}`);
5353
}
5454
this._spec = {
5555
openapi: '3.1.0',
@@ -65,14 +65,14 @@ export default class Filter {
6565
}
6666

6767

68-
filter_spec(spec: Record<string, any>): any {
69-
this._spec.info = spec.info;
68+
filter(): OpenAPIV3.Document {
69+
this._spec.info = this.sourceSpec.info;
7070
for (const p of this.targetPaths) {
71-
if (spec.paths[p] === undefined) {
72-
this.logger.error(`Path not found in spec: ${p}`);
71+
if (this.sourceSpec.paths[p] === undefined) {
72+
logger.error(`Path not found in spec: ${p}`);
7373
continue;
7474
}
75-
this._spec.paths[p] = spec.paths[p];
75+
this._spec.paths[p] = this.sourceSpec.paths[p];
7676
}
7777
this.filter_by_max_parameters(this._spec.paths as OpenAPIV3.PathsObject);
7878
const queue: string[] = [];
@@ -92,13 +92,13 @@ export default class Filter {
9292
this._spec.components[sub_component] = {};
9393
}
9494
if (this._spec.components[sub_component][key] == null) {
95-
if (spec.components != null && spec.components[sub_component] != null && spec.components[sub_component][key] != null) {
96-
this._spec.components[sub_component][key] = spec.components[sub_component][key];
95+
if (this.sourceSpec.components != null && this.sourceSpec.components[sub_component] != null && this.sourceSpec.components[sub_component][key] != null) {
96+
this._spec.components[sub_component][key] = this.sourceSpec.components[sub_component][key];
9797
traverse_and_enqueue(this._spec.components[sub_component][key], queue, visited, this.excludedSchemas);
9898
}
9999
}
100100
}
101-
return this._spec;
101+
return this._spec as OpenAPIV3.Document;
102102
}
103103

104104
filter_by_max_parameters(paths: OpenAPIV3.PathsObject): void {

tools/proto-convert/src/OpenSearchVersionExtractor.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _ from 'lodash';
22
import * as semver from 'semver';
3-
import Logger from './utils/logger';
3+
import logger from './utils/logger';
44
import { deleteMatchingKeys, remove_unused } from './utils/helper';
55
import type { OpenAPIV3 } from 'openapi-types';
66

@@ -10,26 +10,24 @@ import type { OpenAPIV3 } from 'openapi-types';
1010
* x-version-removed: Removes fields if removed version <= target version.
1111
*/
1212
export class OpenSearchVersionExtractor {
13-
private _logger: Logger;
1413
private _spec: OpenAPIV3.Document;
1514
private _target_version: string;
1615

17-
constructor(spec: OpenAPIV3.Document, logger: Logger) {
16+
constructor(spec: OpenAPIV3.Document) {
1817
this._spec = spec;
19-
this._logger = logger;
2018
this._target_version = '';
2119
}
2220

2321

2422
process(currentVersion: string): OpenAPIV3.Document {
2523
const coerced = semver.coerce(currentVersion);
2624
this._target_version = coerced?.toString() || currentVersion;
27-
this._logger.info(`Processing version constraints for OpenSearch ${this._target_version} ...`);
25+
logger.info(`Processing version constraints for OpenSearch ${this._target_version} ...`);
2826

2927
deleteMatchingKeys(this._spec, this.#exclude_per_semver.bind(this));
3028
remove_unused(this._spec);
3129

32-
this._logger.info('Version processing complete');
30+
logger.info('Version processing complete');
3331
return this._spec;
3432
}
3533

tools/proto-convert/src/PreProcessing.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Command, Option } from '@commander-js/extra-typings';
22
import { read_yaml, write_yaml } from './utils/helper';
33
import Filter from './Filter';
44
import { Sanitizer } from './Sanitizer';
5-
import Logger from './utils/logger';
5+
import logger from './utils/logger';
66
import * as path from 'path';
77
import {SchemaModifier} from "./SchemaModifier";
88
import {VendorExtensionProcessor} from "./VendorExtensionProcessor";
@@ -41,17 +41,15 @@ type PreprocessingOpts = {
4141

4242
const opts = command.opts() as PreprocessingOpts;
4343

44-
const logger = new Logger();
45-
4644
try {
4745
logger.info(`PreProcessing ${opts.filtered_path.join(', ')} into ${opts.output} ...`)
4846
const original_spec = read_yaml(opts.input)
49-
const filtered_spec = new Filter(logger, opts.filtered_path, excluded_schemas).filter_spec(original_spec);
50-
const version_processed_spec = new OpenSearchVersionExtractor(filtered_spec, logger).process(opts.opensearchVersion);
51-
const sanitized_spec = new Sanitizer().sanitize(version_processed_spec);
47+
const filtered_spec = new Filter(original_spec, opts.filtered_path, excluded_schemas).filter();
48+
const version_processed_spec = new OpenSearchVersionExtractor(filtered_spec).process(opts.opensearchVersion);
49+
const sanitized_spec = new Sanitizer(version_processed_spec).sanitize();
5250
const consolidated_spec = new GlobalParameterConsolidator(sanitized_spec).consolidate();
53-
const vendor_processed_spec = new VendorExtensionProcessor(consolidated_spec, logger).process();
54-
const schema_modified_spec = new SchemaModifier(vendor_processed_spec, logger).modify();
51+
const vendor_processed_spec = new VendorExtensionProcessor(consolidated_spec).process();
52+
const schema_modified_spec = new SchemaModifier(vendor_processed_spec).modify();
5553
write_yaml(opts.output, schema_modified_spec);
5654

5755
} catch (err) {

tools/proto-convert/src/Sanitizer.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import {traverse} from "./utils/OpenApiTraverser";
88
*/
99
export class Sanitizer {
1010
private static readonly META_PREFIX = "x";
11-
public sanitize(spec: any): any {
12-
this.sanitize_ref(spec);
13-
this.sanitize_spec_name(spec as OpenAPIV3.Document);
14-
return spec;
11+
private spec: OpenAPIV3.Document;
12+
13+
constructor(spec: OpenAPIV3.Document) {
14+
this.spec = spec;
15+
}
16+
17+
public sanitize(): OpenAPIV3.Document {
18+
this.sanitize_ref(this.spec);
19+
this.sanitize_spec_name(this.spec);
20+
return this.spec;
1521
}
1622

1723
sanitize_ref(obj: any): void {

tools/proto-convert/src/SchemaModifier.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ import type {OpenAPIV3} from "openapi-types";
22
import {traverse} from './utils/OpenApiTraverser';
33
import isEqual from 'lodash.isequal';
44
import {compressMultipleUnderscores, isPrimitiveType, resolveObj, isReferenceObject, isEmptyObjectSchema, is_simple_ref} from './utils/helper';
5-
import Logger from "./utils/logger";
5+
import logger from "./utils/logger";
66

77

88
const DEFAULT_MAP_KEY = 'field' // default key for simplified additionalProperties
99
const DEFAULT_MAP_VALUE = 'value' // default value for simplified additionalProperties
1010

1111
export class SchemaModifier {
12-
logger: Logger
1312
root: OpenAPIV3.Document;
14-
constructor(root: OpenAPIV3.Document, logger: Logger = new Logger()) {
13+
constructor(root: OpenAPIV3.Document) {
1514
this.root = root;
16-
this.logger = logger;
1715
}
1816
public modify(): OpenAPIV3.Document {
1917
traverse(this.root, {
@@ -256,7 +254,7 @@ export class SchemaModifier {
256254
}
257255
} else if (complexObject.type === 'object' && complexObject.properties) {
258256
if (complexObject.properties[DEFAULT_MAP_KEY]) {
259-
this.logger.error("Error: additionalProperties key already exists in the schema "+complexObject);
257+
logger.error("Error: additionalProperties key already exists in the schema "+complexObject);
260258
}
261259
complexObject.properties[DEFAULT_MAP_KEY] = this.createAdditionalPropertySchema().properties?.[DEFAULT_MAP_KEY] as OpenAPIV3.SchemaObject || {};
262260
} else if (isPrimitiveType(complexObject)) {
@@ -405,7 +403,7 @@ export class SchemaModifier {
405403
}
406404

407405
if (schema.properties[propertyName]) {
408-
this.logger.warn(`Property '${propertyName}' already exists in schema, skipping additionalProperties conversion`);
406+
logger.warn(`Property '${propertyName}' already exists in schema, skipping additionalProperties conversion`);
409407
return;
410408
}
411409

@@ -437,7 +435,7 @@ export class SchemaModifier {
437435
delete schema.propertyNames;
438436
}
439437

440-
this.logger.info(`Converted additionalProperties to named property '${propertyName}' with type: object`);
438+
logger.info(`Converted additionalProperties to named property '${propertyName}' with type: object`);
441439
}
442440

443441
/**
@@ -473,7 +471,7 @@ export class SchemaModifier {
473471
schema.additionalProperties = items.additionalProperties;
474472
delete (schema as any).items;
475473

476-
this.logger.info(`Removed array wrapper from array of maps schema`);
474+
logger.info(`Removed array wrapper from array of maps schema`);
477475
}
478476
}
479477
}
@@ -514,7 +512,7 @@ export class SchemaModifier {
514512
if (Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
515513
if (this.isOneOfWithSingleProperties(schema.oneOf)) {
516514
this.flattenOneOfToProperties(schema);
517-
this.logger.info(`Converted oneOf pattern to minProperties/maxProperties`);
515+
logger.info(`Converted oneOf pattern to minProperties/maxProperties`);
518516
}
519517
}
520518
}
@@ -587,10 +585,10 @@ export class SchemaModifier {
587585
}
588586
}
589587
(schema as any)['x-oneof-schema'] = true;
590-
this.logger.info(`Added x-oneof-property to properties and marked schema with x-oneof-schema`);
588+
logger.info(`Added x-oneof-property to properties and marked schema with x-oneof-schema`);
591589
} else if (hasNestedPattern) {
592590
(schema as any)['x-oneof-schema'] = true;
593-
this.logger.info(`Marked parent schema with x-oneof-schema (contains nested oneOf pattern)`);
591+
logger.info(`Marked parent schema with x-oneof-schema (contains nested oneOf pattern)`);
594592
}
595593
}
596594

tools/proto-convert/src/VendorExtensionProcessor.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { OpenAPIV3 } from 'openapi-types';
22
import { traverse } from './utils/OpenApiTraverser';
33
import { resolveRef, deleteMatchingKeys, remove_unused } from './utils/helper';
4-
import Logger from './utils/logger';
4+
import logger from './utils/logger';
55
import _ from 'lodash';
66

77
/**
@@ -23,11 +23,9 @@ export class VendorExtensionProcessor {
2323
};
2424

2525
private root: OpenAPIV3.Document;
26-
private logger: Logger;
2726

28-
constructor(root: OpenAPIV3.Document, logger: Logger = new Logger()) {
27+
constructor(root: OpenAPIV3.Document) {
2928
this.root = root;
30-
this.logger = logger;
3129
}
3230

3331
/**
@@ -90,7 +88,7 @@ export class VendorExtensionProcessor {
9088
const oldName = param.name;
9189
param.name = newName;
9290
delete param[VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION];
93-
this.logger.info(`Renamed parameter '${oldName}' -> '${newName}' (${VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION})`);
91+
logger.info(`Renamed parameter '${oldName}' -> '${newName}' (${VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION})`);
9492
}
9593
}
9694

@@ -114,7 +112,7 @@ export class VendorExtensionProcessor {
114112
delete schema.properties[prop];
115113
delete schema.properties[newName][VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION];
116114

117-
this.logger.info(`Renamed property '${prop}' -> '${newName}' (${VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION})`);
115+
logger.info(`Renamed property '${prop}' -> '${newName}' (${VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION})`);
118116
}
119117
}
120118
}
@@ -133,7 +131,7 @@ export class VendorExtensionProcessor {
133131
const oldTitle = subschema.title;
134132
subschema.title = titleValue;
135133
delete subschema[VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION];
136-
this.logger.info(`Set title for ${key} sub-schema: '${oldTitle}' -> '${titleValue}' (${VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION})`);
134+
logger.info(`Set title for ${key} sub-schema: '${oldTitle}' -> '${titleValue}' (${VendorExtensionProcessor.PROTOBUF_NAME_EXTENSION})`);
137135
}
138136
}
139137
}
@@ -180,7 +178,7 @@ export class VendorExtensionProcessor {
180178
}
181179

182180
delete schema[VendorExtensionProcessor.PROTOBUF_TYPE_EXTENSION];
183-
this.logger.info(`Applied ${VendorExtensionProcessor.PROTOBUF_TYPE_EXTENSION}: ${protoType} -> type: ${schema.type}${schema.format ? `, format: ${schema.format}` : ''}`);
181+
logger.info(`Applied ${VendorExtensionProcessor.PROTOBUF_TYPE_EXTENSION}: ${protoType} -> type: ${schema.type}${schema.format ? `, format: ${schema.format}` : ''}`);
184182
}
185183
}
186184

0 commit comments

Comments
 (0)