Skip to content

Commit 1c00d91

Browse files
committed
fix: default missing type to any type
Per the spec if no type is given then the type default to the Any Type: https://swagger.io/docs/specification/data-models/data-types/#any This commit removes a check that explicitly sets any undefined type to object, instead leaving it blank. Then where type checking is done it adds support for missing types. One existing test was expecting SOME_OBJECT_TYPE because of a missing type, this test was corrected to look for SOME_ANY_TYPE. An additional test was added to explicitly state that missing types are to be considered Any Type. In addition a fix was made that if the value of a type field is not a string, then the parmater value SOME_ERROR_TYPE is generated. This allows the snippet generator to run without error, but presumably in some workflow the openapi validator will catch the spec error. Fixes #89
1 parent cfe92a0 commit 1c00d91

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

openapi-to-har.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,20 @@ const getBaseUrl = function (openApi, path, method) {
437437
* @return {HarParameterObject[]} Array of objects describing the parameters in a given OpenAPI method or path
438438
*/
439439
const getParameterValues = function (openApi, param, location, values) {
440+
var type = (param.type || (param.schema && param.schema.type));
441+
442+
if (typeof type === 'undefined') {
443+
type = "ANY"
444+
} else if (typeof type !== 'string') {
445+
// While this is an error and it'll get by the openapi-snippet
446+
// generator, any openapi validator should flag this error where
447+
// typeof type is not 'string'.
448+
type = "ERROR"
449+
} else {
450+
type = type.toUpperCase()
451+
}
440452
let value =
441-
'SOME_' + (param.type || param.schema.type).toUpperCase() + '_VALUE';
453+
'SOME_' + type + '_VALUE';
442454
if (location === 'path') {
443455
// then default to the original place holder value (e.b. '{id}')
444456
value = `{${param.name}}`;
@@ -500,10 +512,6 @@ const parseParametersToQuery = function (
500512
/^#/.test(param.schema['$ref'])
501513
) {
502514
param.schema = resolveRef(openApi, param.schema['$ref']);
503-
if (typeof param.schema.type === 'undefined') {
504-
// many schemas don't have an explicit type
505-
param.schema.type = 'object';
506-
}
507515
}
508516
}
509517
if (

test/parameter_variations_swagger.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@
6767
"type": "integer",
6868
"format": "int32"
6969
}
70+
},
71+
{
72+
"name": "noType",
73+
"description": "can be any type",
74+
"in": "query"
75+
},
76+
{
77+
"name": "typeNotAString",
78+
"type": 35,
79+
"in": "query"
7080
}
7181
],
7282
"responses": {

test/test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ test('Parameters that are Schema References Are Dereferenced', function (t) {
186186
['node_request']
187187
);
188188
const snippet = result.snippets[0].content;
189-
t.true(/pet: 'SOME_OBJECT_VALUE'/.test(snippet));
189+
t.true(/pet: 'SOME_ANY_VALUE'/.test(snippet));
190190
t.end();
191191
});
192192

@@ -1714,3 +1714,29 @@ test('A reference in an examples object is resolved', function (t) {
17141714
t.match(snippet, /tags=dog%2Ccat/);
17151715
t.end();
17161716
});
1717+
1718+
test('A parameter without an explicit type is assigned the Any Type', function (t) {
1719+
const result = OpenAPISnippets.getEndpointSnippets(
1720+
ParameterVariationsAPI,
1721+
'/pets',
1722+
'get',
1723+
['shell_curl']
1724+
);
1725+
1726+
const snippet = result.snippets[0].content;
1727+
t.match(snippet, /noType=SOME_ANY_VALUE/);
1728+
t.end();
1729+
});
1730+
1731+
test('A parameter with a type that is not a string value (like "boolean" or "object") is an error', function (t) {
1732+
const result = OpenAPISnippets.getEndpointSnippets(
1733+
ParameterVariationsAPI,
1734+
'/pets',
1735+
'get',
1736+
['shell_curl']
1737+
);
1738+
1739+
const snippet = result.snippets[0].content;
1740+
t.match(snippet, /typeNotAString=SOME_ERROR_VALUE/);
1741+
t.end();
1742+
});

0 commit comments

Comments
 (0)