-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLOUDP-181547: transformation for nested hashmaps (#63)
- Loading branch information
Showing
7 changed files
with
83 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tools/transformer/src/transformations/additionalPropertiesObject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Remove all AdditonalProperties object section | ||
* That generates an map[string]map[string]string objects for SDK | ||
* but for our API much safer is to rely on the map[string]interface{} | ||
* This is due to the fact that dynamic field can be array which would | ||
* not be possible to represent in map[string]map[string]string structure. | ||
* @param {*} api OpenAPI JSON File | ||
* @param modelNames | ||
* @returns OpenAPI JSON File | ||
*/ | ||
function applyRemoveObjectAdditonalProperties(api) { | ||
const hasSchemas = api && api.components && api.components.schemas; | ||
if (!hasSchemas) { | ||
throw new Error("Missing schemas in openapi"); | ||
} | ||
// Recursive function to traverse the OpenAPI object | ||
function removeObjectAdditonalProperties(obj) { | ||
if (typeof obj !== "object" || obj === null) { | ||
return; | ||
} | ||
|
||
if (Array.isArray(obj)) { | ||
for (let i = 0; i < obj.length; i++) { | ||
removeObjectAdditonalProperties(obj[i]); | ||
} | ||
} else { | ||
// Remove enum field if present | ||
if (obj.hasOwnProperty("additionalProperties")) { | ||
if (obj.additionalProperties.type === "object") { | ||
delete obj.additionalProperties.type; | ||
} | ||
} | ||
|
||
// Traverse nested properties | ||
for (const prop in obj) { | ||
if (obj.hasOwnProperty(prop)) { | ||
removeObjectAdditonalProperties(obj[prop]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Start removing enum fields from the OpenAPI object | ||
removeObjectAdditonalProperties(api); | ||
} | ||
|
||
module.exports = { | ||
applyRemoveObjectAdditonalProperties, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters