Skip to content

Commit b56f904

Browse files
authored
Add documentation on spec documentation (#638)
1 parent 87ca087 commit b56f904

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

docs/doc-comments-guide.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Documenting the API specification
2+
3+
A specification is not only about formalizing data structures, it's also about explaining what these structures and their properties are used for.
4+
5+
Documentation of the TypeScript specification is made using [JSDoc](https://jsdoc.app/) comments, i.e. multiline comments starting with `/**` above a type or field declaration.
6+
7+
Additional lines start with a `*` followed by a space. Long lines are allowed but it's better if text is formatted to a maximum of 120 characters per line.
8+
9+
## Example
10+
11+
```ts
12+
/**
13+
* @rest_spec_name rank_eval
14+
* @since 6.2.0
15+
*/
16+
export interface Request extends RequestBase {
17+
path_parts: {
18+
/**
19+
* List of data streams, indices, and index aliases used to limit the request. Wildcard (`*`) expressions are supported.
20+
*
21+
* To target all data streams and indices in a cluster, omit this parameter or use `_all` or `*`.
22+
*/
23+
index: Indices
24+
}
25+
query_parameters?: {
26+
/**
27+
* Accept no matching indices?
28+
*
29+
* If `false`, the request returns an error if any wildcard expression, index alias, or _all value targets only
30+
* missing or closed indices. This behavior applies even if the request targets other open indices. For example,
31+
* a request targeting `foo*,bar*` returns an error if an index starts with `foo` but no index starts with `bar`.
32+
*
33+
* @server_default true
34+
*/
35+
allow_no_indices?: boolean
36+
37+
expand_wildcards?: ExpandWildcards
38+
39+
/**
40+
* If `true`, missing or closed indices are not included in the response.
41+
* @server_default false
42+
*/
43+
ignore_unavailable?: boolean
44+
45+
search_type?: string
46+
}
47+
body: {
48+
/**
49+
* A set of typical search requests, together with their provided ratings.
50+
*/
51+
requests: RankEvalRequestItem[]
52+
53+
/**
54+
* Definition of the evaluation metric to calculate.
55+
*
56+
* @doc_url https://www.elastic.co/guide/en/elasticsearch/reference/current/search-rank-eval.html#_available_evaluation_metrics
57+
*/
58+
metric?: RankEvalMetric
59+
}
60+
}
61+
```
62+
63+
([original source code](https://github.com/elastic/elasticsearch-specification/blob/main/specification/_global/rank_eval/RankEvalRequest.ts))
64+
65+
66+
## Markup language
67+
68+
The specification is the starting point for a number of generators that produce a large range of artifacts, starting with source code in 10 different programming languages. It's essential for the API documentation to be part of these generated artifacts, e.g. to provide autocompletion help in IDEs when using a client library.
69+
70+
Each of these targets has a different way to format inline documentation: Java code uses HTML, Python uses reStructuredText, Rust uses Markdown, etc. To enable rich documentation to be output, the specification doc comments must not be just plain text, but use a well-defined markup language so that generators can reliably transcode them to their target representation.
71+
72+
**We have settled on using [GitHub Flavored Markdown](https://github.github.com/gfm/) (GFM) for doc comments.** It is based on the well-defined [CommonMark](https://commonmark.org/) specification with a few additions, most notably tables.
73+
74+
GFM also has implementations in most languages, meaning that code generators will be able to easily parse the doc comments and transcode them.
75+
76+
## Structuring a doc-comment
77+
78+
### Terseness
79+
80+
**Doc comments are reference material**: they should be as succinct as possible while capturing all the necessary information to use the elements they're documenting. Remember that they will often show up in small IDE autocompletion popups!
81+
82+
In particular, doc comments are not the right place for tutorials or examples, which should be in dedicated documentation pages. These pages can of course be linked from the doc comments.
83+
84+
API endpoints will also have a `@doc_url` JSDoc tag that links to that API's detailed documentation page.
85+
86+
### Multi-paragraph doc comments
87+
88+
A single sentence is not always enough to explain a type or a field and doc comments will have multiple paragraphs. It is important that **the first paragraph provides a self-contained description** of the item that is being documented. The reason is twofold:
89+
- people skim documentation quickly and should get the gist of the information from that first sentence, and read others if they want to dive in,
90+
- Some IDEs may choose to display only the first paragraph or even the first sentence of a doc-comment in their help popup to keep it small, and then require an additional action from the user to display the full text.

docs/modeling-guide.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ the basic types [here](https://www.typescriptlang.org/docs/handbook/basic-types.
99
while in [behaviors](./behaviors.md) you can find the list of special interfaces used
1010
for describing APIs that can't be represented in the specification.
1111

12+
Refer to the [documentation guide](doc-comments-guide.md) to add documentation to types and fields.
13+
1214
### Dictionary
1315

1416
Represents a dynamic key value map:
@@ -393,7 +395,7 @@ class FooRequest {
393395

394396
#### description
395397

396-
You can add a description for each property, in this case there is no need to use a JSDoc tag.
398+
You can (and should!) add a description for each type and property. For an in-depth explanation of how to write good descriptions, see [Documenting the API specification](doc-comments-guide.md).
397399

398400
```ts
399401
class Foo {

0 commit comments

Comments
 (0)