Skip to content

Commit

Permalink
Add support for application/smile.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Jul 9, 2024
1 parent ba3d715 commit a54c83b
Show file tree
Hide file tree
Showing 11 changed files with 5,988 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Added test for search with seq_no_primary_term ([#367](https://github.com/opensearch-project/opensearch-api-specification/pull/367))
- Added a linter for parameter sorting ([#369](https://github.com/opensearch-project/opensearch-api-specification/pull/369))
- Added support for `application/cbor` responses ([#371](https://github.com/opensearch-project/opensearch-api-specification/pull/371))
- Added support for `application/smile` responses ([#386](https://github.com/opensearch-project/opensearch-api-specification/pull/386))

### Changed

Expand Down
5,903 changes: 5,902 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"json-schema-to-typescript": "^14.0.4",
"lodash": "^4.17.21",
"qs": "^6.12.1",
"smile-js": "^0.7.0",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.1",
"typescript": "<5.4.0",
Expand Down
10 changes: 10 additions & 0 deletions spec/namespaces/cat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,11 @@ components:
type: array
items:
$ref: '../schemas/cat.health.yaml#/components/schemas/HealthRecord'
application/smile:
schema:
type: array
items:
$ref: '../schemas/cat.health.yaml#/components/schemas/HealthRecord'
cat.help@200:
description: ''
content:
Expand All @@ -892,6 +897,11 @@ components:
type: array
items:
$ref: '../schemas/cat.indices.yaml#/components/schemas/IndicesRecord'
application/smile:
schema:
type: array
items:
$ref: '../schemas/cat.indices.yaml#/components/schemas/IndicesRecord'
cat.master@200:
description: ''
content:
Expand Down
13 changes: 13 additions & 0 deletions tests/cat/health.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,16 @@ chapters:
status: yellow
node.data: '1'
discovered_cluster_manager: 'true'
- synopsis: Cat in different formats (format=smile).
method: GET
path: /_cat/health
parameters:
format: smile
response:
status: 200
content_type: application/yaml
payload:
- node.total: '1'
status: yellow
node.data: '1'
discovered_cluster_manager: 'true'
8 changes: 8 additions & 0 deletions tests/cat/indices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,11 @@ chapters:
response:
status: 200
content_type: application/cbor
- synopsis: Cat in different formats (format=smile).
method: GET
path: /_cat/indices
parameters:
format: smile
response:
status: 200
content_type: application/smile
17 changes: 8 additions & 9 deletions tools/src/tester/ChapterReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { to_json, to_ndjson } from '../helpers'
import qs from 'qs'
import YAML from 'yaml'
import CBOR from 'cbor'
import SMILE from 'smile-js'

export default class ChapterReader {
private readonly _client: OpenSearchHttpClient
Expand Down Expand Up @@ -87,16 +88,14 @@ export default class ChapterReader {
#deserialize_payload(payload: any, content_type: any): any {
if (payload === undefined) return undefined
if (content_type === undefined) return payload
const payload_buffer = Buffer.from(payload as string, 'binary')
switch (content_type) {
case 'text/plain': return this.#deserialize_payload_data(payload as string)
case 'application/json': return payload.length == 0 ? {} : JSON.parse(this.#deserialize_payload_data(payload as string))
case 'application/yaml': return payload.length == 0 ? {} : YAML.parse(this.#deserialize_payload_data(payload as string))
case 'application/cbor': return payload.length == 0 ? {} : CBOR.decode(payload as string)
default: return this.#deserialize_payload_data(payload as string)
case 'text/plain': return payload_buffer.toString()
case 'application/json': return payload.length == 0 ? {} : JSON.parse(payload_buffer.toString())
case 'application/yaml': return payload.length == 0 ? {} : YAML.parse(payload_buffer.toString())
case 'application/cbor': return payload.length == 0 ? {} : CBOR.decode(payload_buffer)
case 'application/smile': return payload.length == 0 ? {} : SMILE.parse(payload_buffer)
default: return payload_buffer.toString()
}
}

#deserialize_payload_data(payload: string): string {
return Buffer.from(payload, 'binary').toString()
}
}
14 changes: 14 additions & 0 deletions tools/tests/tester/ChapterReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@ describe('ChapterReader', () => {
expect(result.content_type).toEqual("application/cbor")
expect(result.payload).toEqual(["x", { "y": 1 }])
})

it('application/smile', async () => {
mocked_axios.request.mockResolvedValue({
status: 200,
headers: {
'content-type': 'application/smile'
},
data: new Uint8Array([0x3a, 0x29, 0x0a, 0x00, 0xfa, 0x83, 0x6e, 0x61, 0x6d, 0x65, 0x42, 0x42, 0x6f, 0x62, 0x82, 0x61, 0x67, 0x65, 0x24, 0xb2, 0x84, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x29, 0x00, 0x40, 0x2c, 0x0c, 0x66, 0x33, 0x19, 0x4c, 0x66, 0x33, 0xfb]).buffer
});

const result = await reader.read({ id: 'id', path: 'path', method: 'POST' }, new StoryOutputs())
expect(result.content_type).toEqual("application/smile")
expect(result.payload).toEqual({ age: 25, name: "Bob", score: 96.8 })
})
})
})

16 changes: 16 additions & 0 deletions tools/tests/tester/fixtures/evals/passed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ chapters:
result: PASSED
payload_schema:
result: PASSED
- title: This GET /_cat/health chapter returns application/smile and should pass.
overall:
result: PASSED
request:
parameters:
format:
result: PASSED
request_body:
result: PASSED
response:
status:
result: PASSED
payload_body:
result: PASSED
payload_schema:
result: PASSED
epilogues:
- title: DELETE /books
overall:
Expand Down
5 changes: 5 additions & 0 deletions tools/tests/tester/fixtures/specs/excerpt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ components:
type: array
items:
type: object
application/smile:
schema:
type: array
items:
type: object
indices.delete@200:
description: ''
content:
Expand Down
10 changes: 10 additions & 0 deletions tools/tests/tester/fixtures/stories/passed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ chapters:
content_type: application/cbor
payload:
- node.total: '1'
- synopsis: This GET /_cat/health chapter returns application/smile and should pass.
path: /_cat/health
parameters:
format: cbor
method: GET
response:
status: 200
content_type: application/smile
payload:
- node.total: '1'

0 comments on commit a54c83b

Please sign in to comment.