Skip to content

Introduce GenericResponseBody behaviour #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/model/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export const knownBehaviors = [
'ArrayResponseBase',
'EmptyResponseBase',
'CommonQueryParameters',
'CommonCatQueryParameters'
'CommonCatQueryParameters',
'GenericResponseBody'
]

/**
Expand Down
11 changes: 11 additions & 0 deletions docs/behaviors.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ Since these can break the request structure these are listed explicitly as a beh
```ts
class CatRequestBase extends RequestBase implements CommonCatQueryParameters {}
```

## GenericResponseBody

Some APIs does not return a structured body, but a generic value instead.
For examplke the `get_source` API return the source document.

```ts
class SourceResponse<TDocument>
extends ResponseBase
implements GenericResponseBody<TDocument> {}
```
47 changes: 35 additions & 12 deletions output/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -122210,28 +122210,36 @@
"query": []
},
{
"generics": [
{
"name": "TDocument",
"namespace": "_global.get_source"
}
"attachedBehaviors": [
"GenericResponseBody"
],
"inherits": [
"behaviors": [
{
"generics": [
{
"kind": "instance_of",
"type": {
"name": "Field",
"namespace": "_types"
"name": "TDocument",
"namespace": "_global.get_source"
}
},
{
"kind": "user_defined_value"
}
],
"type": {
"name": "DictionaryResponseBase",
"name": "GenericResponseBody",
"namespace": "_spec_utils"
}
}
],
"generics": [
{
"name": "TDocument",
"namespace": "_global.get_source"
}
],
"inherits": [
{
"type": {
"name": "ResponseBase",
"namespace": "_types"
}
}
Expand Down Expand Up @@ -137654,6 +137662,21 @@
}
}
]
},
{
"description": "Some APIs does not return a structured body, but a generic value instead.\nFor examplke the `get_source` API return the source document.",
"generics": [
{
"name": "T",
"namespace": "_spec_utils"
}
],
"kind": "interface",
"name": {
"name": "GenericResponseBody",
"namespace": "_spec_utils"
},
"properties": []
}
]
}
6 changes: 4 additions & 2 deletions output/typescript/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11809,8 +11809,7 @@ export interface SearchSourceFilteringSourceFilter {
export interface GetSourceSourceRequest extends GetGetRequest {
}

export interface GetSourceSourceResponse<TDocument = unknown> extends DictionaryResponseBase<Field, any> {
}
export type GetSourceSourceResponse<TDocument = unknown> = TDocument

export interface QueryDslSpanContainingSpanContainingQuery extends QueryDslAbstractionsQueryQueryBase {
big?: QueryDslSpanSpanQuery
Expand Down Expand Up @@ -13495,3 +13494,6 @@ export interface SpecUtilsCommonQueryParameters {
source_query_string?: string
}

export interface SpecUtilsGenericResponseBody<T = unknown> {
}

15 changes: 5 additions & 10 deletions specification/_global/get_source/SourceResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@
* under the License.
*/

import { DictionaryResponseBase } from '@_types/Base'
import { Field } from '@_types/common'
import { UserDefinedValue } from '_spec_utils/UserDefinedValue'
import { GenericResponseBody } from '_spec_utils/behaviors'
import { ResponseBase } from '@_types/Base'

// TODO: this is weird as we likely require a special behaviour
// the generic is not respected
// --> export class SourceResponse<TDocument> implements GenericBodyResponse<TDocument> {}
export class SourceResponse<TDocument> extends DictionaryResponseBase<
Field,
UserDefinedValue
> {}
export class SourceResponse<TDocument>
extends ResponseBase
implements GenericResponseBody<TDocument> {}
7 changes: 7 additions & 0 deletions specification/_spec_utils/behaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,10 @@ export interface CommonCatQueryParameters {
s?: string[]
v?: boolean
}

/**
* Some APIs does not return a structured body, but a generic value instead.
* For examplke the `get_source` API return the source document.
* @behavior Defines a trait that the response body is the given generic.
*/
export interface GenericResponseBody<T> {}
10 changes: 9 additions & 1 deletion typescript-generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const model: M.Model = JSON.parse(readFileSync(join(__dirname, '..', 'output', '
const skipBehaviors = [
'ArrayResponseBase',
'EmptyResponseBase',
'AdditionalProperties'
'AdditionalProperties',
'GenericResponseBody'
]

let definitions = `/*
Expand Down Expand Up @@ -174,6 +175,13 @@ function buildBehaviorInterface (type: M.Interface): string {
return serializeAdditionalPropertiesType(type)
}

if (type.attachedBehaviors.includes('GenericResponseBody')) {
const generics = type.generics
assert(generics)
const name = generics[0].name
return `export type ${createName(type.name)}${buildGenerics(generics)} = ${name}\n`
}

if (type.attachedBehaviors.includes('ArrayResponseBase')) {
const behavior = lookupBehavior(type, 'ArrayResponseBase')
assert(behavior)
Expand Down