Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 80aae67

Browse files
authored
Merge pull request #284 from apiaryio/honzajavorek/provide-name
Provide transaction name for each annotation
2 parents 432e485 + 465f193 commit 80aae67

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ Description of an error or warning which occurred during parsing of the API desc
167167

168168
### Deprecated Properties
169169

170+
- name: `Hello world! > Retrieve Message` (string) - Transaction Name, non-deterministic breadcrumb location of the relevant HTTP Transaction within the API description document.
170171
- origin (object) - Object of references to nodes of [API Elements][api-elements] derived from the original API description document.
171172
- filename: `./api-description.apib` (string)
172173
- apiName: `My Api` (string)

compile/compileTransactionName.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module.exports = function compileTransactionName(origin) {
2-
const segments = [];
3-
if (origin.apiName) { segments.push(origin.apiName); }
4-
if (origin.resourceGroupName) { segments.push(origin.resourceGroupName); }
5-
if (origin.resourceName) { segments.push(origin.resourceName); }
6-
if (origin.actionName) { segments.push(origin.actionName); }
7-
if (origin.exampleName) { segments.push(origin.exampleName); }
8-
return segments.join(' > ');
2+
return [
3+
origin.apiName,
4+
origin.resourceGroupName,
5+
origin.resourceName,
6+
origin.actionName,
7+
origin.exampleName,
8+
]
9+
.filter(Boolean)
10+
.join(' > ');
911
};

compile/index.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,7 @@ function compileRequest(httpRequestElement) {
112112

113113
annotations.forEach((annotation) => {
114114
/* eslint-disable no-param-reassign */
115-
annotation.location = (
116-
(httpRequestElement.href ? httpRequestElement.href.sourceMapValue : undefined)
117-
|| (httpRequestElement.parents.find('transition').href ? httpRequestElement.parents.find('transition').href.sourceMapValue : undefined)
118-
|| (httpRequestElement.parents.find('resource').href ? httpRequestElement.parents.find('resource').href.sourceMapValue : undefined)
119-
);
115+
annotation.location = null; // https://github.com/apiaryio/dredd-transactions/issues/275
120116
/* eslint-enable */
121117
});
122118

@@ -152,19 +148,26 @@ function compileResponse(httpResponseElement) {
152148

153149
function compileTransaction(mediaType, filename, httpTransactionElement, exampleNo) {
154150
const origin = compileOrigin(mediaType, filename, httpTransactionElement, exampleNo);
155-
const { request, annotations } = compileRequest(httpTransactionElement.request);
156-
157-
annotations.forEach((annotation) => { annotation.origin = Object.assign({}, origin); }); // eslint-disable-line
151+
const name = compileTransactionName(origin);
158152

153+
const { request, annotations } = compileRequest(httpTransactionElement.request);
154+
annotations.forEach((annotation) => {
155+
/* eslint-disable no-param-reassign */
156+
annotation.name = name;
157+
annotation.origin = Object.assign({}, origin);
158+
/* eslint-enable */
159+
});
159160
if (!request) { return { transaction: null, annotations }; }
160161

161-
const name = compileTransactionName(origin);
162-
const response = compileResponse(httpTransactionElement.response);
163-
164-
const transaction = {
165-
request, response, origin, name,
162+
return {
163+
transaction: {
164+
request,
165+
response: compileResponse(httpTransactionElement.response),
166+
name,
167+
origin,
168+
},
169+
annotations,
166170
};
167-
return { transaction, annotations };
168171
}
169172

170173
function compile(mediaType, apiElements, filename) {

test/schemas/createAnnotationSchema.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const createLocationSchema = require('./createLocationSchema');
22
const createOriginSchema = require('./createOriginSchema');
33

44
const TYPES = ['error', 'warning'];
5-
const COMPONENTS = ['apiDescriptionParser', 'parametersValidation', 'uriTemplateExpansion'];
65

76

87
module.exports = function createAnnotationSchema(options = {}) {
@@ -13,7 +12,7 @@ module.exports = function createAnnotationSchema(options = {}) {
1312
const messageSchema = { type: 'string' };
1413
if (options.message) { messageSchema.pattern = options.message; }
1514

16-
return {
15+
const parseAnnotationSchema = {
1716
type: 'object',
1817
properties: {
1918
type: {
@@ -22,22 +21,40 @@ module.exports = function createAnnotationSchema(options = {}) {
2221
},
2322
component: {
2423
type: 'string',
25-
enum: options.component ? [options.component] : COMPONENTS,
24+
enum: ['apiDescriptionParser'],
2625
},
2726
message: messageSchema,
2827
location: createLocationSchema(),
29-
origin: createOriginSchema({ filename }),
3028
},
3129
required: ['type', 'component', 'message', 'location'],
32-
dependencies: {
33-
origin: {
34-
properties: {
35-
component: {
36-
enum: ['parametersValidation', 'uriTemplateExpansion'],
37-
},
38-
},
30+
additionalProperties: false,
31+
};
32+
if (options.component === 'apiDescriptionParser') {
33+
return parseAnnotationSchema;
34+
}
35+
36+
const compileAnnotationSchema = {
37+
type: 'object',
38+
properties: {
39+
type: {
40+
type: 'string',
41+
enum: options.type ? [options.type] : TYPES,
42+
},
43+
component: {
44+
type: 'string',
45+
enum: options.component ? [options.component] : ['parametersValidation', 'uriTemplateExpansion'],
3946
},
47+
message: messageSchema,
48+
location: { type: 'null' }, // https://github.com/apiaryio/dredd-transactions/issues/275
49+
name: { type: 'string' },
50+
origin: createOriginSchema({ filename }),
4051
},
52+
required: ['type', 'component', 'message', 'location', 'name', 'origin'],
4153
additionalProperties: false,
4254
};
55+
if (['parametersValidation', 'uriTemplateExpansion'].includes(options.component)) {
56+
return compileAnnotationSchema;
57+
}
58+
59+
return { anyOf: [parseAnnotationSchema, compileAnnotationSchema] };
4360
};

0 commit comments

Comments
 (0)