You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/dev/apidef-oas.md
+31-29Lines changed: 31 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,13 +10,11 @@ Make sure `json` and `bson` tags are added to the fields.
10
10
11
11
If an `enabled` flag is specified in the OAS contract, make sure a corresponding `disabled` or `enabled` flag is added in the classic API definition.
12
12
13
-
Also make sure that `disabled`/`enabled` flag toggles the feature on or off.
13
+
Also make sure that `disabled`/`enabled` flag toggles the feature on or off.
14
14
15
15
### Why `disabled` or `enabled` in classic API definition?
16
16
17
-
Historically, almost every feature/middleware in Tyk is considered enabled by default when value for feature controls are non zero values.
18
-
It is disabled when the feature controls are having zero values.
19
-
For this reason, whenever an existing feature is migrated to OAS, and OAS has an `enabled` flag then a `disabled` flag is added to give explicit control to turn off the feature.
17
+
Historically, almost every feature/middleware in Tyk is considered enabled by default when value for feature controls are non zero values. It is disabled when the feature controls are having zero values. For this reason, whenever an existing feature is migrated to OAS, and OAS has an `enabled` flag then a `disabled` flag is added to give explicit control to turn off the feature.
20
18
21
19
Please also make sure that the disabled flags are set to true in `APIDefinition.SetDisabledFlags()`, so that it is not enabled in OAS by default.
22
20
@@ -28,27 +26,29 @@ Ensure OAS fields follow `camelCase` notation for `json` and `bson` tags.
28
26
29
27
For fields that are required:
30
28
31
-
1. Do not use `omitempty` in struct tags.
29
+
1. Do not use `omitempty` in struct tags.
32
30
33
-
2. Do not use pointer types for required fields.
31
+
2. Do not use pointer types for required fields.
34
32
35
-
3. Add a comment `// required` towards the end of a required field so that automation generates docs accordingly.
33
+
3. Add a comment `// required` towards the end of a required field so that automation generates docs accordingly.
36
34
37
-
4. As a convention, we also try to add the corresponding classic API definition fields in godoc in the following format
38
-
```
39
-
// Tyk classic API definition: `!use_keyless`.
40
-
```
41
-
This might not be perfect at this moment, but we aim to keep this link so that customers find it easier to follow the docs.
35
+
4. As a convention, we also try to add the corresponding classic API definition fields in godoc in the following format
36
+
37
+
```
38
+
// Tyk classic API definition: `!use_keyless`.
39
+
```
40
+
41
+
This might not be perfect at this moment, but we aim to keep this link so that customers find it easier to follow the docs.
42
42
43
43
## Handle Optional Fields
44
44
45
45
For optional fields:
46
46
47
-
1. Add the `omitempty` tag
47
+
1. Add the `omitempty` tag
48
+
49
+
2. Use pointer types for structs.
48
50
49
-
2. Use pointer types for structs.
50
-
51
-
3. Make sure that `omitempty` tag is added for slice fields that are optional.
51
+
3. Make sure that `omitempty` tag is added for slice fields that are optional.
52
52
53
53
## Add Go Doc Comments
54
54
@@ -58,29 +58,31 @@ Add comments in Go doc format for each field to enable automated documentation g
58
58
59
59
Every OAS struct should follow the convention of having `Fill(apidef.APIDefinition)` and `ExtractTo(*apidef.APIDefinition)` methods:
60
60
61
-
`Fill` populates the struct from a classic API definition.
61
+
`Fill` populates the struct from a classic API definition.
62
62
63
-
`ExtractTo` extracts the contents of an OAS API definition into a classic API definition.
63
+
`ExtractTo` extracts the contents of an OAS API definition into a classic API definition.
64
64
65
65
## Implement Fill Method Pattern
66
66
67
67
Each `Fill` method should follow this pattern:
68
68
69
69
```go
70
70
if u.RateLimit == nil {
71
-
u.RateLimit = &RateLimit{}
71
+
u.RateLimit = &RateLimit{}
72
72
}
73
73
74
74
u.RateLimit.Fill(api)
75
75
ifShouldOmit(u.RateLimit) {
76
-
u.RateLimit = nil
76
+
u.RateLimit = nil
77
77
}
78
78
```
79
79
80
80
This ensures the field is reset to empty when not configured in the classic API definition.
81
81
82
82
### Working with `VersionData`
83
+
83
84
A `Main` version will be provided that can be used for `Fill`.
85
+
84
86
```go
85
87
api.VersionData.Versions[Main]
86
88
```
@@ -91,17 +93,19 @@ Similarly, follow this pattern with `ExtractTo`:
91
93
92
94
```go
93
95
if u.RateLimit == nil {
94
-
u.RateLimit = &RateLimit{}
95
-
deferfunc() {
96
-
u.RateLimit = nil
97
-
}()
96
+
u.RateLimit = &RateLimit{}
97
+
deferfunc() {
98
+
u.RateLimit = nil
99
+
}()
98
100
}
99
101
100
102
u.RateLimit.ExtractTo(api)
101
103
```
102
104
103
105
### Working with `VersionData`
106
+
104
107
There are 2 helper functions for `ExtractTo` that will help to handle `VersionData`. You can use them like this:
Write tests for conversion functions. Refer to the example: https://github.com/TykTechnologies/tyk/pull/5979/files#diff-222cc254c0c6c09fa0cf50087860b837a0873e2aef3c84ec7d80b1014c149057R97
120
123
121
124
## Update TestOAS_ExtractTo_ResetAPIDefinition
122
125
123
126
Maintain and update the list of fields that are not OAS compatible in the `TestOAS_ExtractTo_ResetAPIDefinition` test.
124
127
125
128
## Update JSON Schema
126
129
127
-
Update the JSON schema for the `x-tyk-api-gateway` struct in:
Update the JSON schema for the `x-tyk-api-gateway` struct in: https://github.com/TykTechnologies/tyk/blob/master/apidef/oas/schema/x-tyk-api-gateway.json
129
131
130
-
Ensure this schema is updated whenever the OAS API definition is modified.
132
+
Ensure this schema is updated whenever the OAS API definition is modified.
0 commit comments