Skip to content

Commit 5f59dc3

Browse files
lcawlAnaethelion
andauthoredOct 3, 2024··
Add @doc_tag to override default tag behaviour (#2961)
Co-authored-by: Laurent Saint-Félix <laurent.saintfelix@elastic.co>
1 parent df2dbc1 commit 5f59dc3

File tree

15 files changed

+42
-8
lines changed

15 files changed

+42
-8
lines changed
 

‎api-design-guidelines/naming.md

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ Care should be given to ensure that:
100100
- All member APIs of a given namespace are logically related and form a coherent set.
101101
- Related functionality is not distributed across multiple arbitrary namespaces
102102

103+
NOTE: The endpoint namespaces are used to generate tags in the OpenAPI documents. The tags are ultimately used to group the endpoints in the API documentation. To override the default tag, use `@doc_tag`.
104+
103105
### Use the global namespace sparingly
104106

105107
The top-level global namespace should be treated with particular care. It is traditionally reserved for search and document endpoints only. A case should be made and a broader discussion carried out before new endpoints unrelated to these functions are added to the global namespace.

‎compiler-rs/clients_schema/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,9 @@ pub struct Endpoint {
815815
#[serde(skip_serializing_if = "Option::is_none")]
816816
pub availability: Option<Availabilities>,
817817

818+
#[serde(skip_serializing_if = "Option::is_none")]
819+
pub doc_tag: Option<String>,
820+
818821
/// If missing, there is not yet a request definition for this endpoint.
819822
#[serde(skip_serializing_if = "Option::is_none")]
820823
pub request: Option<TypeName>,

‎compiler-rs/clients_schema_to_openapi/src/paths.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ pub fn add_endpoint(
196196

197197
// Create the operation, it will be repeated if we have several methods
198198
let operation = openapiv3::Operation {
199-
tags: vec![namespace.to_string()],
199+
tags: if let Some(doc_tag) = &endpoint.doc_tag {
200+
vec![doc_tag.clone()]
201+
} else {
202+
vec![namespace.to_string()]
203+
},
200204
summary: sum_desc.summary,
201205
description: sum_desc.description,
202206
// external_docs: tac.convert_external_docs(endpoint),

‎compiler-rs/clients_schema_to_openapi/src/schemas.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const SCHEMA_PLACEHOLDER: ReferenceOr<Schema> = ReferenceOr::Reference {
3838
/// Convert `schema.json` type and value definitions to OpenAPI schemas:
3939
///
4040
/// The `convert_*` functions return a concrete schema and not a reference and do not store them in
41-
/// the OpenAPI `components.schema`. This is the role of `for_type_name` hat creates and stores the
41+
/// the OpenAPI `components.schema`. This is the role of `for_type_name` that creates and stores the
4242
/// schema and returns a reference.
4343
impl<'a> TypesAndComponents<'a> {
4444
/// Convert a value. Returns a schema reference and not a concrete schema, as values can
Binary file not shown.

‎compiler/src/model/build-model.ts

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export function compileEndpoints (): Record<string, model.Endpoint> {
6767
name: api,
6868
description: spec.documentation.description,
6969
docUrl: spec.documentation.url,
70+
docTag: spec.docTag,
7071
// Setting these values by default should be removed
7172
// when we no longer use rest-api-spec stubs as the
7273
// source of truth for stability/visibility.

‎compiler/src/model/json-spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export interface JsonSpec {
6161
description: string
6262
required?: boolean
6363
}
64+
docTag?: string
6465
}
6566

6667
export default function buildJsonSpec (): Map<string, JsonSpec> {

‎compiler/src/model/metamodel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ export class Endpoint {
408408
docId?: string
409409
deprecation?: Deprecation
410410
availability: Availabilities
411-
411+
docTag?: string
412412
/**
413413
* If the request value is `null` it means that there is not yet a
414414
* request type definition for this endpoint.

‎compiler/src/model/utils.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ export function hoistRequestAnnotations (
625625
request: model.Request, jsDocs: JSDoc[], mappings: Record<string, model.Endpoint>, response: model.TypeName | null
626626
): void {
627627
const knownRequestAnnotations = [
628-
'rest_spec_name', 'behavior', 'class_serializer', 'index_privileges', 'cluster_privileges', 'doc_id', 'availability'
628+
'rest_spec_name', 'behavior', 'class_serializer', 'index_privileges', 'cluster_privileges', 'doc_id', 'availability', 'doc_tag'
629629
]
630630
// in most of the cases the jsDocs comes in a single block,
631631
// but it can happen that the user defines multiple single line jsDoc.
@@ -696,6 +696,9 @@ export function hoistRequestAnnotations (
696696
for (const [availabilityName, availabilityValue] of Object.entries(availabilities)) {
697697
endpoint.availability[availabilityName] = availabilityValue
698698
}
699+
} else if (tag === 'doc_tag') {
700+
assert(jsDocs, value.trim() !== '', `Request ${request.name.name}'s @doc_tag cannot be empty`)
701+
endpoint.docTag = value.trim()
699702
} else {
700703
assert(jsDocs, false, `Unhandled tag: '${tag}' with value: '${value}' on request ${request.name.name}`)
701704
}

‎docs/modeling-guide.md

+18
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,24 @@ class Request {
631631
foobar,/guide/en/example
632632
```
633633
634+
#### `@doc_tag`
635+
636+
An OpenAPI tag that is used to group similar endpoints in the API documentation.
637+
If it is absent, by default the tag is derived from the first part of the namespace.
638+
639+
```ts
640+
/**
641+
* @rest_spec_name api
642+
* @doc_tag my tag
643+
*/
644+
class Request {
645+
...
646+
}
647+
```
648+
649+
NOTE: In the OpenAPI specification, operations can have multiple tags. However, we currently support only a single tag.
650+
651+
634652
#### `@codegen_name`
635653
636654
A custom name that can be used to display the property. Useful in Enums and

‎output/openapi/elasticsearch-openapi.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎output/openapi/elasticsearch-serverless-openapi.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎output/schema/schema.json

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎specification/indices/create_data_stream/IndicesCreateDataStreamRequest.ts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { Duration } from '@_types/Time'
2929
* @availability stack since=7.9.0 stability=stable
3030
* @availability serverless stability=stable visibility=public
3131
* @index_privileges create_index
32+
* @doc_tag data stream
3233
*/
3334
export interface Request extends RequestBase {
3435
path_parts: {

‎typescript-generator/src/metamodel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ export class Endpoint {
408408
docId?: string
409409
deprecation?: Deprecation
410410
availability: Availabilities
411-
411+
docTag?: string
412412
/**
413413
* If the request value is `null` it means that there is not yet a
414414
* request type definition for this endpoint.

0 commit comments

Comments
 (0)