serverless-openapi-documenter 0.0.121 → 0.0.123
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/definitionGenerator.js +49 -39
- package/src/schemaHandler.js +58 -36
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "serverless-openapi-documenter",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.123",
|
|
4
4
|
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -50,11 +50,11 @@
|
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"@apidevtools/json-schema-ref-parser": "^9.1.0",
|
|
52
52
|
"@redocly/openapi-core": "^1.2.0",
|
|
53
|
-
"@usebruno/converters": "^0.
|
|
53
|
+
"@usebruno/converters": "^0.16.0",
|
|
54
54
|
"chalk": "^4.1.2",
|
|
55
55
|
"js-yaml": "^4.1.1",
|
|
56
56
|
"json-schema-for-openapi": "^0.5.0",
|
|
57
|
-
"openapi-to-postmanv2": "^
|
|
57
|
+
"openapi-to-postmanv2": "^6.0.0",
|
|
58
58
|
"uuid": "^11.1.0"
|
|
59
59
|
},
|
|
60
60
|
"engines": {
|
|
@@ -167,11 +167,12 @@ class DefinitionGenerator {
|
|
|
167
167
|
|
|
168
168
|
if (documentation.contact) {
|
|
169
169
|
const contactObj = {};
|
|
170
|
-
|
|
170
|
+
|
|
171
|
+
if (documentation.contact.name) contactObj.name = documentation.contact.name;
|
|
171
172
|
|
|
172
173
|
if (documentation.contact.url) contactObj.url = documentation.contact.url;
|
|
173
174
|
|
|
174
|
-
contactObj.email = documentation.contact.email
|
|
175
|
+
if (documentation.contact.email) contactObj.email = documentation.contact.email;
|
|
175
176
|
|
|
176
177
|
const extendedSpec = this.extendSpecification(documentation.contact);
|
|
177
178
|
|
|
@@ -591,7 +592,9 @@ class DefinitionGenerator {
|
|
|
591
592
|
obj.headers = corsHeaders;
|
|
592
593
|
addHeaders(owaspHeaders);
|
|
593
594
|
} else {
|
|
594
|
-
|
|
595
|
+
if (Object.keys(owaspHeaders).length) {
|
|
596
|
+
obj.headers = owaspHeaders;
|
|
597
|
+
}
|
|
595
598
|
}
|
|
596
599
|
}
|
|
597
600
|
|
|
@@ -677,10 +680,13 @@ class DefinitionGenerator {
|
|
|
677
680
|
|
|
678
681
|
async createRequestBody(requestBodyDetails) {
|
|
679
682
|
const obj = {
|
|
680
|
-
description: requestBodyDetails.description,
|
|
681
683
|
required: requestBodyDetails.required || false,
|
|
682
684
|
};
|
|
683
685
|
|
|
686
|
+
if (requestBodyDetails.description) {
|
|
687
|
+
obj.description = requestBodyDetails.description;
|
|
688
|
+
}
|
|
689
|
+
|
|
684
690
|
obj.content = await this.createMediaTypeObject(
|
|
685
691
|
requestBodyDetails.models
|
|
686
692
|
).catch((err) => {
|
|
@@ -692,7 +698,6 @@ class DefinitionGenerator {
|
|
|
692
698
|
|
|
693
699
|
async createMediaTypeObject(models, type) {
|
|
694
700
|
const mediaTypeObj = {};
|
|
695
|
-
|
|
696
701
|
for (const mediaTypeDocumentation of this.schemaHandler.models) {
|
|
697
702
|
if (models === undefined || models === null) {
|
|
698
703
|
throw new Error(
|
|
@@ -700,48 +705,53 @@ class DefinitionGenerator {
|
|
|
700
705
|
);
|
|
701
706
|
}
|
|
702
707
|
|
|
703
|
-
|
|
704
|
-
let contentKey
|
|
705
|
-
|
|
706
|
-
|
|
708
|
+
for (const modelContentType in models) {
|
|
709
|
+
let contentKey
|
|
710
|
+
|
|
711
|
+
if (models[modelContentType] === mediaTypeDocumentation.name) {
|
|
712
|
+
contentKey = modelContentType;
|
|
707
713
|
}
|
|
708
|
-
const obj = {};
|
|
709
714
|
|
|
710
|
-
|
|
711
|
-
if (mediaTypeDocumentation?.content) {
|
|
712
|
-
if (mediaTypeDocumentation.content[contentKey]?.example)
|
|
713
|
-
obj.example = mediaTypeDocumentation.content[contentKey].example;
|
|
715
|
+
if (contentKey) {
|
|
714
716
|
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
)
|
|
717
|
+
const obj = {};
|
|
718
|
+
let schema;
|
|
719
|
+
if (mediaTypeDocumentation.content) {
|
|
720
|
+
if (mediaTypeDocumentation.content[contentKey]?.example) {
|
|
721
|
+
obj.example = mediaTypeDocumentation.content[contentKey]?.example;
|
|
722
|
+
}
|
|
719
723
|
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
if (mediaTypeDocumentation?.example)
|
|
726
|
-
obj.example = mediaTypeDocumentation.example;
|
|
724
|
+
if (mediaTypeDocumentation.content[contentKey]?.examples) {
|
|
725
|
+
obj.examples = this.createExamples(
|
|
726
|
+
mediaTypeDocumentation.content[contentKey].examples
|
|
727
|
+
);
|
|
728
|
+
}
|
|
727
729
|
|
|
728
|
-
|
|
729
|
-
|
|
730
|
+
schema = (mediaTypeDocumentation.schema) ? mediaTypeDocumentation.schema : mediaTypeDocumentation.schemas[contentKey];
|
|
731
|
+
} else if (mediaTypeDocumentation?.contentType && mediaTypeDocumentation.schema) {
|
|
732
|
+
if (mediaTypeDocumentation.example) {
|
|
733
|
+
obj.example = mediaTypeDocumentation.example;
|
|
734
|
+
}
|
|
730
735
|
|
|
731
|
-
|
|
732
|
-
|
|
736
|
+
if (mediaTypeDocumentation.examples) {
|
|
737
|
+
obj.example = mediaTypeDocumentation.examples;
|
|
738
|
+
}
|
|
733
739
|
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
.catch((err) => {
|
|
737
|
-
throw err;
|
|
738
|
-
});
|
|
740
|
+
schema = mediaTypeDocumentation.schema;
|
|
741
|
+
}
|
|
739
742
|
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
+
const schemaRef = await this.schemaHandler
|
|
744
|
+
.createSchema(mediaTypeDocumentation.name)
|
|
745
|
+
.catch((err) => {
|
|
746
|
+
throw err;
|
|
747
|
+
});
|
|
743
748
|
|
|
744
|
-
|
|
749
|
+
obj.schema = {
|
|
750
|
+
$ref: schemaRef,
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
Object.assign(mediaTypeObj, { [contentKey]: obj });
|
|
754
|
+
}
|
|
745
755
|
}
|
|
746
756
|
}
|
|
747
757
|
|
|
@@ -861,7 +871,7 @@ class DefinitionGenerator {
|
|
|
861
871
|
if (
|
|
862
872
|
this.openAPI.components[type][name] &&
|
|
863
873
|
isEqual(schemaObj[name], this.openAPI.components[type][name]) ===
|
|
864
|
-
|
|
874
|
+
false
|
|
865
875
|
) {
|
|
866
876
|
delete schemaObj[name];
|
|
867
877
|
newName = `${name}-${uuid()}`;
|
package/src/schemaHandler.js
CHANGED
|
@@ -42,9 +42,21 @@ class SchemaHandler {
|
|
|
42
42
|
return model;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
if (Object.keys(model.content).length === 1) {
|
|
46
|
+
const contentType = Object.keys(model.content)[0];
|
|
47
|
+
model.contentType = contentType;
|
|
48
|
+
model.contentTypes = [contentType];
|
|
49
|
+
model.schema = model.content[contentType].schema;
|
|
50
|
+
} else {
|
|
51
|
+
model.contentType = null;
|
|
52
|
+
model.contentTypes = Object.keys(model.content);
|
|
53
|
+
model.schema = null;
|
|
54
|
+
model.schemas = {};
|
|
55
|
+
for (const key in model.content) {
|
|
56
|
+
Object.assign(model.schemas, {[key]: {schema: model.content[key].schema}});
|
|
57
|
+
}
|
|
58
|
+
// model.schema = model.content[contentType].schema;
|
|
59
|
+
}
|
|
48
60
|
|
|
49
61
|
return model;
|
|
50
62
|
};
|
|
@@ -69,43 +81,53 @@ class SchemaHandler {
|
|
|
69
81
|
async addModelsToOpenAPI() {
|
|
70
82
|
for (const model of this.models) {
|
|
71
83
|
const modelName = model.name;
|
|
72
|
-
const
|
|
84
|
+
const schemas = []
|
|
85
|
+
if (model.schema){
|
|
86
|
+
// const modelSchema = model.schema;
|
|
87
|
+
schemas.push(model.schema)
|
|
88
|
+
} else {
|
|
89
|
+
for (const key in model.schemas) {
|
|
90
|
+
schemas.push(model.schemas[key].schema);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
73
93
|
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
94
|
+
for (const modelSchema of schemas) {
|
|
95
|
+
const convertedSchemas = await this.__dereferenceAndConvert(
|
|
96
|
+
modelSchema,
|
|
97
|
+
modelName,
|
|
98
|
+
model
|
|
99
|
+
).catch((err) => {
|
|
100
|
+
if (err instanceof Error) throw err;
|
|
101
|
+
else return err;
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
if (
|
|
105
|
+
typeof convertedSchemas.schemas === "object" &&
|
|
106
|
+
!Array.isArray(convertedSchemas.schemas) &&
|
|
107
|
+
convertedSchemas.schemas !== null
|
|
108
|
+
) {
|
|
109
|
+
for (const [schemaName, schemaValue] of Object.entries(
|
|
110
|
+
convertedSchemas.schemas
|
|
111
|
+
)) {
|
|
112
|
+
if (schemaName === modelName) {
|
|
113
|
+
this.modelReferences[
|
|
114
|
+
schemaName
|
|
115
|
+
] = `#/components/schemas/${modelName}`;
|
|
116
|
+
}
|
|
82
117
|
|
|
83
|
-
|
|
84
|
-
typeof convertedSchemas.schemas === "object" &&
|
|
85
|
-
!Array.isArray(convertedSchemas.schemas) &&
|
|
86
|
-
convertedSchemas.schemas !== null
|
|
87
|
-
) {
|
|
88
|
-
for (const [schemaName, schemaValue] of Object.entries(
|
|
89
|
-
convertedSchemas.schemas
|
|
90
|
-
)) {
|
|
91
|
-
if (schemaName === modelName) {
|
|
92
|
-
this.modelReferences[
|
|
93
|
-
schemaName
|
|
94
|
-
] = `#/components/schemas/${modelName}`;
|
|
118
|
+
this.__addToComponents("schemas", schemaValue, schemaName);
|
|
95
119
|
}
|
|
96
|
-
|
|
97
|
-
|
|
120
|
+
} else {
|
|
121
|
+
throw new Error(
|
|
122
|
+
`There was an error converting the ${
|
|
123
|
+
model.name
|
|
124
|
+
} schema. Model received looks like: \n\n${JSON.stringify(
|
|
125
|
+
model
|
|
126
|
+
)}. The convereted schema looks like \n\n${JSON.stringify(
|
|
127
|
+
convertedSchemas
|
|
128
|
+
)}`
|
|
129
|
+
);
|
|
98
130
|
}
|
|
99
|
-
} else {
|
|
100
|
-
throw new Error(
|
|
101
|
-
`There was an error converting the ${
|
|
102
|
-
model.name
|
|
103
|
-
} schema. Model received looks like: \n\n${JSON.stringify(
|
|
104
|
-
model
|
|
105
|
-
)}. The convereted schema looks like \n\n${JSON.stringify(
|
|
106
|
-
convertedSchemas
|
|
107
|
-
)}`
|
|
108
|
-
);
|
|
109
131
|
}
|
|
110
132
|
}
|
|
111
133
|
}
|