Skip to content

Commit 784aef5

Browse files
fix: update rule dok18
Signed-off-by: Fredrik Nordlander <fredrik.nordlander@digg.se>
1 parent 9dd6a82 commit 784aef5

File tree

5 files changed

+96
-25
lines changed

5 files changed

+96
-25
lines changed

GUIDELINES.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Detta dokument specificerar reglerna som verktyget tillämpar.
3939
- [ID: DOK.11](#id-dok11)
4040
- [ID: DOK.15](#id-dok15)
4141
- [ID: DOK.17](#id-dok17)
42+
- [ID: DOK.18](#id-dok18)
4243
- [ID: DOK.19](#id-dok19)
4344
- [ID: DOK.20](#id-dok20)
4445
2. [Område: Datum- och tidsformat](#område-datum--och-tidsformat)
@@ -355,6 +356,31 @@ I exemplet ovan, så exemplifieras regeln det med ett icke godkänt värde på v
355356

356357
---
357358

359+
### ID: DOK.18
360+
361+
**Krav:** API specifikationen BÖR beskrivas med antingen JSON eller YAML.
362+
363+
**Typ:** BÖR
364+
365+
**JSON Path Plus-uttryck:**
366+
367+
```
368+
$.paths.*.*.responses.*.content'
369+
$.paths.*.*.requestBody.content'
370+
```
371+
372+
**Förklaring:**
373+
374+
Regeln förutsätter att API-specifikationen beskrivs med antingen JSON eller YAM där den regeln letar upp alla content objekt under både requestBody och responses. Om det inte är dem, så returnerar regeln ett fel i form av en varning.
375+
376+
**Exempel:**
377+
378+
![alt text](images/dok18.png)
379+
380+
I exemplet ovan, så exemplifieras regeln med ett ej godkänd värde på content objekten under objektet requestBody samt under objetet responses, eftersom värdet på dessa är satt till: 'text/plain' och inte någon av de godkända typerna 'application/yaml' eller application/json.
381+
382+
---
383+
358384
### ID: DOK.19
359385

360386
**Krav:** Ett API:s resurser och de möjliga operationer som kan utföras på resursen SKALL beskrivas så utförligt och tydligt som möjligt.

apis/dok-api.yaml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,30 @@ paths:
205205
'201':
206206
description: Created
207207
content:
208-
application/json:
208+
text/plain:
209209
schema:
210210
type: string
211211
example:
212212
- id: 19
213213
name: thing1
214-
- id: 20
215-
name: thing2
214+
/items5:
215+
get:
216+
summary: Get items
217+
responses:
218+
'200':
219+
description: error response
220+
content:
221+
application/xml:
222+
schema:
223+
type: object
224+
properties:
225+
id:
226+
type: string
227+
example:
228+
- id: 17
229+
name: Pete
230+
- id: 18
231+
name: Tom
216232
components:
217233
schemas:
218234
Pet:

images/dok18.png

77.5 KB
Loading

tests/unit/dok.test.ts

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,27 @@ testRule('Dok18', [
4949
info: { version: '1.0' },
5050
servers: [{ url: 'https://example.com/my-api/v1' }],
5151
paths: {
52-
'/Dettacase': {
52+
'/item2': {
5353
get: {
54-
description: 'Ogiltigt testfall av CamelCase',
55-
parameters: [
56-
{
57-
name: 'Very_LongName',
58-
in: 'path',
59-
required: false,
60-
},
61-
],
54+
description: 'Ogiltigt testfall av test för giltigt format av API specifikationen',
6255
responses: {
6356
'200': {
64-
description: '',
57+
description: 'Ej godkänt svar',
58+
content: {
59+
'application/xml': {
60+
schema: {
61+
type: 'object',
62+
properties: {
63+
id: {
64+
type: 'string',
65+
},
66+
name: {
67+
type: 'string',
68+
},
69+
},
70+
},
71+
},
72+
},
6573
},
6674
},
6775
},
@@ -70,21 +78,41 @@ testRule('Dok18', [
7078
},
7179
errors: [
7280
{
73-
code: 'Dok20',
74-
message: 'Förväntade returkoder och felkoder SKALL vara fullständigt dokumenterade.',
75-
path: ['paths', '/Dettacase', 'get', 'responses', '200', 'description'],
76-
severity: DiagnosticSeverity.Error,
77-
range: {
78-
start: {
79-
line: 0,
80-
character: 276,
81-
},
82-
end: {
83-
line: 0,
84-
character: 278,
81+
code: 'Dok18',
82+
message: 'API specifikationen BÖR beskrivas med antingen JSON eller YAML.',
83+
path: ['paths', '/item2', 'get', 'responses', '200', 'content'],
84+
severity: DiagnosticSeverity.Warning,
85+
},
86+
],
87+
},
88+
{
89+
name: 'ogiltigt testfall',
90+
document: {
91+
openapi: '3.1.0',
92+
info: { version: '1.0' },
93+
servers: [{ url: 'https://example.com/my-api/v1' }],
94+
paths: {
95+
'/item3': {
96+
post: {
97+
requestBody: {
98+
content: {
99+
'text/plain': {
100+
example1: 'example1',
101+
},
102+
},
103+
},
104+
responses: {},
85105
},
86106
},
87107
},
108+
},
109+
errors: [
110+
{
111+
code: 'Dok18',
112+
message: 'API specifikationen BÖR beskrivas med antingen JSON eller YAML.',
113+
path: ['paths', '/item3', 'post', 'requestBody','content'],
114+
severity: DiagnosticSeverity.Warning,
115+
},
88116
],
89117
},
90118
]);

tests/util/rulesetTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const ruleTypes = [
8282
DokRules.Dok03License,
8383
DokRules.Dok03LicenseName,
8484
DokRules.Dok03LicenseUrl,
85+
DokRules.Dok18,
8586
ResRules.Res02,
8687
ResRules.Res06,
8788
];

0 commit comments

Comments
 (0)