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

Remove Hardcoded Schema References #240

Merged
merged 15 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
11 changes: 2 additions & 9 deletions src/actions/InstallationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as fs from 'fs';
import { ConfigurationStore } from '../storage/ConfigurationStore';
import { InstallationArgs } from '../types/stateInterfaces';
import { FALLBACK_SCHEMA, deepMerge } from '../renderer/components/common/Utils';

import { updateSchemaReferences } from '../services/ResolveRef';

//AJV did not like the regex in our current schema
const zoweDatasetMemberRegexFixed = {
Expand Down Expand Up @@ -157,14 +157,7 @@ class Installation {
let yamlSchema = JSON.parse(readPaxYamlAndSchema.details.yamlSchema);
const serverCommon = JSON.parse(readPaxYamlAndSchema.details.serverCommon);
if(yamlSchema && serverCommon){
yamlSchema.additionalProperties = true;
yamlSchema.properties.zowe.properties.setup.properties.dataset.properties.parmlibMembers.properties.zis = zoweDatasetMemberRegexFixed;
yamlSchema.properties.zowe.properties.setup.properties.certificate.properties.pkcs12.properties.directory = serverCommon.$defs.path;
if(yamlSchema.$defs?.networkSettings?.properties?.server?.properties?.listenAddresses?.items){
delete yamlSchema.$defs?.networkSettings?.properties?.server?.properties?.listenAddresses?.items?.ref;
yamlSchema.$defs.networkSettings.properties.server.properties.listenAddresses.items = serverCommon.$defs.ipv4
}
// console.log('Setting schema from runtime dir:', JSON.stringify(yamlSchema));
updateSchemaReferences(yamlSchema, serverCommon);
ConfigurationStore.setSchema(yamlSchema);
parsedSchema = true;
ProgressStore.set('downloadUnpax.getSchemas', true);
Expand Down
29 changes: 29 additions & 0 deletions src/services/ResolveRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const updateSchemaReferences = (schema: any, serverCommon: any): void => {

const traverseAndUpdate = (node: any) => {
if (node !== null && typeof node === "object") {
for (const key in node) {
if (key === "$ref" && typeof node[key] === "string") {
try {
const refValue = resolveRef(node[key]);
Object.assign(node, refValue);
delete node['$ref'];
} catch(error){
console.error("Error resolving reference:", error.message);
}
} else {
traverseAndUpdate(node[key]);
}
}
}
}

const resolveRef = (ref: string) => {
const refPath = ref.split('#')[1];
let result = serverCommon.$defs;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we are resolving only server-common references? I do not know how likely we will have references to other schemas in future but we'd better handle it at least through log / warnings if some refs were not resolved.
Also how are internal refs handled now, like "$ref": "#/$defs/pkcs12-certificate"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it would be better to do
const refParts = ref.split('#');

const refFile = refParts[0];
const refAnchor = refParts[1];

if (!refFile) { refFile = zowe-yaml-schema.json }
else { // gather the $id values of each schema file and make a map so you can identify which schema file is being referenced }

const refObject = Object.values(result).find((obj:any) => obj.$anchor === refPath);
return refObject;
}

traverseAndUpdate(schema);
}
Loading