Skip to content

Commit

Permalink
Adapt to the new ___ ref separator from the spec (#902)
Browse files Browse the repository at this point in the history
* Updated ref name separator to

* Added date to AUTOCUT title and branch to easier tell these PRs apart.

* #

Signed-off-by: Theo Truong <[email protected]>

* #

Signed-off-by: Theo Truong <[email protected]>

* #

* Corrected OpenSearchApi file name caused by MacOS filesystem being case-insensitive

Signed-off-by: Theo Truong <[email protected]>

---------

Signed-off-by: Theo Truong <[email protected]>
  • Loading branch information
nhtruong authored Nov 12, 2024
1 parent dcf53f2 commit 3cbb8e5
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 1,827 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/generate_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ jobs:
with:
token: ${{ steps.github_app_token.outputs.token }}
commit-message: "Updated opensearch-js to reflect the latest OpenSearch API spec (${{ env.date }})"
title: "[AUTOCUT] Update opensearch-js to reflect the latest OpenSearch API spec"
title: "[AUTOCUT] Update opensearch-js to reflect the latest OpenSearch API spec (${{ env.date }})"
body: |
Update `opensearch-js` to reflect the latest [OpenSearch API spec](https://github.com/opensearch-project/opensearch-api-specification/releases/download/main-latest/opensearch-openapi.yaml).
Date: ${{ env.date }}
branch: update-api-from-spec
branch: update-api-from-spec-${{ env.date }}
base: main
signoff: true
labels: |
Expand Down
1,627 changes: 0 additions & 1,627 deletions api/OpenSearchAPI.d.ts

This file was deleted.

187 changes: 0 additions & 187 deletions api/OpenSearchAPI.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
*/

import TypesContainer, { TYPE_COMPONENTS_FOLDER } from './TypesContainer'
import TypesContainer, { SEPARATOR, TYPE_COMPONENTS_FOLDER } from './TypesContainer'
import type { JSONSchema7 as Schema } from 'json-schema'
import type { RawOpenSearchSpec, RawParameter } from '../../spec_parser/types'
import _ from 'lodash'
Expand All @@ -25,7 +25,7 @@ export default class ComponentTypesContainer extends TypesContainer {

private static build_components (spec: RawOpenSearchSpec): void {
const referenced_schemas = _.entries(spec.components.schemas).map(([key, schema]) => {
const [file_name, schema_name] = key.split(':')
const [file_name, schema_name] = key.split(SEPARATOR)
return { file_name, schema_name, schema }
})
const ref_schemas_map = _.groupBy(referenced_schemas, 'file_name')
Expand All @@ -38,7 +38,7 @@ export default class ComponentTypesContainer extends TypesContainer {

private static build_global_params (spec: RawOpenSearchSpec): void {
const params: RawParameter[] = _.entries(spec.components.parameters)
.filter(([key]) => key.startsWith('_global::'))
.filter(([key]) => key.startsWith(`_global${SEPARATOR}`))
.map(([, param]) => param)
const required = params.filter(param => param.required).map(param => param.name)
const properties = _.fromPairs(params.map(param => [param.name, param.schema]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
*/

import TypesContainer from './TypesContainer'
import TypesContainer, { SEPARATOR } from './TypesContainer'
import _ from 'lodash'
import { type ApiFunctionTyping } from '../../spec_parser/ApiFunction'
import type ApiFunction from '../../spec_parser/ApiFunction'
Expand Down Expand Up @@ -38,7 +38,7 @@ export default class FunctionTypesContainer extends TypesContainer {
}

create_ref (key: keyof ApiFunctionTyping): string {
return `${this._func.ns_prototype}/${this._func.prototype_name}:${this._func.types[key]}`
return `${this._func.ns_prototype}/${this._func.prototype_name}${SEPARATOR}${this._func.types[key]}`
}

#build_request (): Schema {
Expand All @@ -49,7 +49,7 @@ export default class FunctionTypesContainer extends TypesContainer {
const required_params = Object.entries(params).filter(([, param]) => param.required).map(([name]) => name)
const required = body?.required ? [...required_params, 'body'] : required_params
const schema: Schema = { properties, required }
return { allOf: [schema, { $ref: '#/components/schemas/_global:Params' }] }
return { allOf: [schema, { $ref: `#/components/schemas/_global${SEPARATOR}Params` }] }
}

#build_response (): Schema {
Expand Down
7 changes: 4 additions & 3 deletions api_generator/src/renderers/render_types/TypesContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { to_pascal_case } from '../../helpers'

type FilePath = string
export const TYPE_COMPONENTS_FOLDER = '_types'
export const SEPARATOR = '___' // separating fileName___schemaName in $ref

export default class TypesContainer {
static readonly REPO = new Map<FilePath, TypesContainer>()
Expand Down Expand Up @@ -47,7 +48,7 @@ export default class TypesContainer {

ref_to_obj (ref: string): string {
if (ref === 'ApiResponse') return ref
const schema_name = ref.split(':')[1]
const schema_name = ref.split(SEPARATOR)[1]
const container = this.ref_to_container(ref)
if (container === this) return schema_name
return `${container.import_name}.${schema_name}`
Expand All @@ -56,10 +57,10 @@ export default class TypesContainer {
ref_to_container (ref: string): TypesContainer {
let file_path: string = 'UNSET'
if (ref.startsWith('#/components')) {
const file_name = ref.split(':')[0].split('/').reverse()[0]
const file_name = ref.split(SEPARATOR)[0].split('/').reverse()[0]
file_path = `${TYPE_COMPONENTS_FOLDER}/${file_name}.d.ts`
} else {
const [folder_name, file_name] = ref.split(':')[0].split('/')
const [folder_name, file_name] = ref.split(SEPARATOR)[0].split('/')
file_path = `${folder_name}/${file_name}.d.ts`
}
const container = TypesContainer.REPO.get(file_path)
Expand Down
2 changes: 1 addition & 1 deletion lib/Client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import { ConnectionOptions as TlsConnectionOptions } from 'tls';
import { URL } from 'url';
import OpenSearchAPI from '../api/OpenSearchAPI';
import OpenSearchAPI from '../api/OpenSearchApi';
import Serializer from './Serializer';
import Helpers from './Helpers';
import Connection, { AgentOptions, agentFn } from './Connection';
Expand Down
2 changes: 1 addition & 1 deletion lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const kChild = Symbol('opensearchjs-child');
const kExtensions = Symbol('opensearchjs-extensions');
const kEventEmitter = Symbol('opensearchjs-event-emitter');

const OpenSearchAPI = require('../api/OpenSearchAPI');
const OpenSearchAPI = require('../api/OpenSearchApi');

class Client extends OpenSearchAPI {
constructor(opts = {}) {
Expand Down

0 comments on commit 3cbb8e5

Please sign in to comment.