Skip to content

Commit

Permalink
Fix: delete leftoever undefined array items.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <dblock@amazon.com>
  • Loading branch information
dblock committed Sep 9, 2024
1 parent adaeeb3 commit c708431
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
4 changes: 4 additions & 0 deletions tools/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ export function sort_array_by_keys (values: any[], priorities: string[] = []): s
export function delete_matching_keys(obj: any, condition: (obj: any) => boolean): void {
for (const key in obj) {
var item = obj[key]

if (_.isObject(item)) {
if (condition(item)) {
delete obj[key]
} else {
delete_matching_keys(item, condition)
if (_.isArray(item)) {
obj[key] = _.compact(item)
}
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions tools/tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ describe('helpers', () => {
expect(obj).toStrictEqual({ foo: [{}] })
})
})

describe('an object with an array where some items are removed', () => {
var obj: object

beforeEach(() => {
obj = {
foo: [
{ value: 1 },
{ value: 1 },
{ value: 2 }
]
}
})

test('removes matching values', () => {
delete_matching_keys(obj, (_item: any) => _item.value == 1)
expect(obj).toStrictEqual({ foo: [{ value: 2 }] })
})
})
})

describe('find_refs', () => {
Expand Down
6 changes: 3 additions & 3 deletions tools/tests/merger/OpenApiVersionExtractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('extract() from a merged API spec', () => {
test('has matching responses', () => {
const spec = extractor.extract()
expect(_.keys(spec.paths['/index']?.get?.responses)).toEqual([
'200', '201', '404', '500', '503', 'removed-2.0', 'added-1.3-removed-2.0', 'distributed-excluded-amazon-serverless'
'200', '201', '404', '500', '503', 'removed-2.0', 'removed-2.0-refs', 'added-1.3-removed-2.0', 'distributed-excluded-amazon-serverless'
])
})
})
Expand All @@ -54,7 +54,7 @@ describe('extract() from a merged API spec', () => {
test('has matching responses', () => {
const spec = extractor.extract()
expect(_.keys(spec.paths['/index']?.get?.responses)).toEqual([
'200', '201', '404', '500', '503', 'added-2.0', 'distributed-excluded-amazon-serverless'
'200', '201', '404', '500', '503', 'added-2.0', 'removed-2.0-refs', 'distributed-excluded-amazon-serverless'
])
})

Expand Down Expand Up @@ -86,7 +86,7 @@ describe('extract() from a merged API spec', () => {
test('has matching responses', () => {
const spec = extractor.extract()
expect(_.keys(spec.paths['/index']?.get?.responses)).toEqual([
'200', '201', '404', '500', '503', 'added-2.0', 'added-2.1', 'distributed-excluded-amazon-serverless'
'200', '201', '404', '500', '503', 'added-2.0', 'removed-2.0-refs', 'added-2.1', 'distributed-excluded-amazon-serverless'
])
})
})
Expand Down
10 changes: 10 additions & 0 deletions tools/tests/merger/fixtures/extractor/opensearch/expected_1.3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ paths:
removed-2.0:
$ref: '#/components/responses/info@removed-2.0'
x-version-removed: '2.0'
removed-2.0-refs:
$ref: '#/components/responses/info@removed-2.0-refs'
added-1.3-removed-2.0:
$ref: '#/components/responses/info@added-1.3-removed-2.0'
distributed-excluded-amazon-serverless:
Expand Down Expand Up @@ -114,13 +116,21 @@ components:
description: Distributed in opensearch.org, AOS and AOSS.
info@removed-2.0:
description: Removed in 2.0 via attribute next to ref.
info@removed-2.0-refs:
description: One of the ref values removed in 2.0.
schema:
oneOf:
- $ref: '#/components/schemas/_common:Type'
- $ref: '#/components/schemas/_common:OldId'
nodes.info@200:
description: All nodes.
content:
application/json:
schema:
type: object
schemas:
_common:OldId:
type: string
_common:Type:
type: string
x-version-removed: '2.0'
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ paths:
added-2.0:
$ref: '#/components/responses/info@added-2.0'
x-version-added: '2.0'
removed-2.0-refs:
$ref: '#/components/responses/info@removed-2.0-refs'
distributed-excluded-amazon-serverless:
$ref: '#/components/responses/info@distributed-all'
x-distributions-excluded:
Expand Down Expand Up @@ -150,6 +152,11 @@ components:
description: Added in 2.0 via attribute next to ref.
info@distributed-all:
description: Distributed in opensearch.org, AOS and AOSS.
info@removed-2.0-refs:
description: One of the ref values removed in 2.0.
schema:
oneOf:
- $ref: '#/components/schemas/_common:OldId'
nodes.info@200:
description: All nodes.
content:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ paths:
removed-2.0:
$ref: '#/components/responses/info@removed-2.0'
x-version-removed: '2.0'
removed-2.0-refs:
$ref: '#/components/responses/info@removed-2.0-refs'
added-1.3-removed-2.0:
$ref: '#/components/responses/info@added-1.3-removed-2.0'
added-2.1:
Expand Down Expand Up @@ -83,6 +85,12 @@ components:
description: Added in 2.0 via attribute next to ref.
info@removed-2.0:
description: Removed in 2.0 via attribute next to ref.
info@removed-2.0-refs:
description: One of the ref values removed in 2.0.
schema:
oneOf:
- $ref: '../schemas/_common.yaml#/components/schemas/Type'
- $ref: '../schemas/_common.yaml#/components/schemas/OldId'
info@added-1.3-removed-2.0:
description: Added in 1.3, removed in 2.0 via attribute in response body.
x-version-added: '1.3'
Expand Down

0 comments on commit c708431

Please sign in to comment.