Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin): field names with dashes #104

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/components/EntityForm/EntityForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,19 @@ export default {

updateModel (parent, data) {
Object.keys(data).forEach(key => {
const modelKey = parent ? `${parent}-${key}` : key
const scheme = this.schema[modelKey]
let modelKey = parent ? `${parent}-${key}` : key
let scheme = this.schema[modelKey]
// If `scheme` is undefined, it is either because the key is not the deepest nested key of the schema object
// or the field name has dashes in it and its schema key was converted to underscores during initiation.
if (!scheme) {
const underscoredModelKey = parent ? `${parent}-${key.replace(/-/g, '_')}` : key.replace(/-/g, '_')
// Here we check if the underscored key exists in the schema and the `fieldNameHasDashes` flag is true
if (this.schema[underscoredModelKey]?.fieldNameHasDashes) {
modelKey = underscoredModelKey
scheme = this.schema[modelKey]
}
}

const value = data[key]
const type = typeof value

Expand Down Expand Up @@ -667,6 +678,14 @@ export default {
// to the dot notation that the Kong Admin API understands.
let fieldNameDotNotation = convertToDotNotation(fieldName)

// If the field name originally has dashes, they were converted to underscores during initiation.
// Now we need to convert them back to dashes because the Admin API expects dashes
if (fieldSchema.fieldNameHasDashes) {
const [keyToBeConverted, ...rest] = fieldNameDotNotation.split('.').reverse()

fieldNameDotNotation = [keyToBeConverted.replace(/_/g, '-'), ...rest].reverse().join('.')
}

if (fieldSchemaValueType === 'object-expand') {
let key

Expand Down
16 changes: 15 additions & 1 deletion src/pages/plugins/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,18 @@ export default {
schema = schema.reduce((acc, current) => {
const key = Object.keys(current)[0]

acc[key] = current[key]
// If the backend schema has dashes in the field name (e.g. config.response_headers.X-Cache-Status of the proxy-cache plugin),
// replace them with underscores because the shared form component treats dashes as separators for nested fields
if (key.match(/-/g)) {
acc[key.replace(/-/g, '_')] = {
...current[key],
// A flag to indicate the field name has dashes originally and they are replaced with underscores.
// When submitting the form, the underscores should be replaced with dashes again
fieldNameHasDashes: true,
}
} else {
acc[key] = current[key]
}

return acc
}, {})
Expand Down Expand Up @@ -736,6 +747,9 @@ export default {
}

output[field].valueType = valueType
if (scheme.fieldNameHasDashes) {
output[field].fieldNameHasDashes = true
}
})

return output
Expand Down
Loading