Skip to content

Commit

Permalink
CLOUDP-181547: transformation for nested hashmaps (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki authored Jun 7, 2023
1 parent 5aac7c6 commit fa6a84c
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 33 deletions.
14 changes: 7 additions & 7 deletions admin/model_fts_mappings.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions admin/model_index_options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions openapi/atlas-api-transformed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15386,7 +15386,6 @@ components:
partialFilterExpression:
type: object
additionalProperties:
type: object
description: >-
Rules that limit the documents that the index references to a
filter expression. All MongoDB index types accept a
Expand Down Expand Up @@ -15436,7 +15435,6 @@ components:
storageEngine:
type: object
additionalProperties:
type: object
description: 'Storage engine set for the specific index. This value can be set
only at creation. This option uses the following format:
`"storageEngine" : { "<storage-engine-name>" : "<options>" }`
Expand Down Expand Up @@ -15475,7 +15473,6 @@ components:
weights:
type: object
additionalProperties:
type: object
description: Relative importance to place upon provided index parameters. This
object expresses this as key/value pairs of index parameter and
weight to apply to that parameter. You can specify weights for
Expand Down Expand Up @@ -28536,7 +28533,6 @@ components:
fields:
type: object
additionalProperties:
type: object
externalDocs:
description: Atlas Search Field Mappings
url: https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#define-field-mappings
Expand Down
1 change: 0 additions & 1 deletion openapi/atlas-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33902,7 +33902,6 @@ components:
partialFilterExpression:
type: object
additionalProperties:
type: object
description: |-
Rules that limit the documents that the index references to a filter expression. All MongoDB index types accept a **partialFilterExpression** option. **partialFilterExpression** can include following expressions:

Expand Down
2 changes: 2 additions & 0 deletions tools/transformer/src/atlasTransformations.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
applyArrayTransformations,
transformOneOfProperties,
applyRemoveEnumsTransformations,
applyRemoveObjectAdditonalProperties,
} = require("./transformations");

const removeUnusedSchemas = require("./engine/removeUnused");
Expand Down Expand Up @@ -77,6 +78,7 @@ module.exports = function runTransformations(openapi) {
]);

applyRemoveEnumsTransformations(openapi);
applyRemoveObjectAdditonalProperties(openapi);

// Required for RegionConfig
workaroundNestedTransformations(openapi);
Expand Down
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,
};
4 changes: 4 additions & 0 deletions tools/transformer/src/transformations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const {
const { applyDiscriminatorTransformations } = require("./discriminator");
const { applyArrayTransformations } = require("./swapArray");
const { applyRemoveEnumsTransformations } = require("./removeEnums");
const {
applyRemoveObjectAdditonalProperties,
} = require("./additionalPropertiesObject");

module.exports = {
applyModelNameTransformations,
Expand All @@ -19,4 +22,5 @@ module.exports = {
applyDiscriminatorTransformations,
applyArrayTransformations,
applyRemoveEnumsTransformations,
applyRemoveObjectAdditonalProperties,
};

0 comments on commit fa6a84c

Please sign in to comment.