Skip to content

Commit

Permalink
fix: merging discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio Davide committed Jan 30, 2023
1 parent c9d0803 commit fb6172a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 80 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Unreleased

### Fixed
- merging of baseProperties with customProperties causing inconsistent microservices' env Variables schema

### Added

- add optional metrics on request duration. The metrics collection is enabled by mean of the variable `ENABLE_HTTP_CLIENT_METRICS` (default: false)
Expand Down
38 changes: 31 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const addPostDecorator = require('./lib/postDecorator')
const ajvSetup = require('./lib/ajvSetup')
const HttpClient = require('./lib/httpClient')
const { extraHeadersKeys } = require('./lib/util')
const { mergeDeepWithKey, concat } = require('ramda')

const USERID_HEADER_KEY = 'USERID_HEADER_KEY'
const USER_PROPERTIES_HEADER_KEY = 'USER_PROPERTIES_HEADER_KEY'
Expand Down Expand Up @@ -95,16 +94,41 @@ const baseSchema = {
},
}

function mergeObjectOrArray(toBeMergedValue, alreadyMergedValues, isArray) {
return isArray ? [
...toBeMergedValue ?? [],
...alreadyMergedValues,
] : {
...toBeMergedValue ?? {},
...alreadyMergedValues,
}
}

function mergeJsonSchemas(schema, otherSchema) {
return mergeDeepWithKey(
(_, left, right) => (typeof left === 'object' && typeof right === 'object' ? concat(left, right) : right),
schema,
otherSchema
)
const mergedSchema = {
additionalProperties: false,
type: 'object',
}

Object.keys(schema).forEach(key => {
mergedSchema[key] = typeof schema[key] === 'object'
? mergeObjectOrArray(mergedSchema[key], schema[key], Array.isArray(schema[key]))
: schema[key]
})

Object.keys(otherSchema).forEach(key => {
mergedSchema[key] = typeof otherSchema[key] === 'object'
? mergeObjectOrArray(mergedSchema[key], otherSchema[key], Array.isArray(otherSchema[key]))
: otherSchema[key]
})

return mergedSchema
}

function getOverlappingKeys(properties, otherProperties) {
if (!otherProperties) { return [] }
if (!otherProperties) {
return []
}
const propertiesNames = Object.keys(properties)
const otherPropertiesNames = Object.keys(otherProperties)
const overlappingProperties = propertiesNames.filter(propertyName =>
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"nock": "^13.2.4",
"pre-commit": "^1.2.2",
"proxy": "^1.0.2",
"ramda": "^0.28.0",
"split2": "^4.1.0",
"swagger-parser": "^10.0.3",
"tap": "^16.1.0",
Expand Down
72 changes: 0 additions & 72 deletions test.json

This file was deleted.

0 comments on commit fb6172a

Please sign in to comment.