Skip to content

Commit

Permalink
[PS][Experimental] Better common verb handling (OpenAPITools#5783)
Browse files Browse the repository at this point in the history
* better common verb handling

* better debugging

* add option to customize common verb
  • Loading branch information
wing328 authored and michaelpro1 committed May 7, 2020
1 parent c802dcf commit dbcb652
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bin/openapi3/powershell-experimental-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/powershell-experimental -i modules/openapi-generator/src/test/resources/3_0/powershell/petstore.yaml -g powershell-experimental -o samples/client/petstore/powershell-experimental --additional-properties powershellGalleryUrl=https://www.powershellgallery.com/packages/PSPetstore,packageGuid=a27b908d-2a20-467f-bc32-af6f3a654ac5,packageName=PSPetstore,apiNamePrefix=PS,packageVersion=0.1.2 -c ./bin/powershell-config.json $@"
ags="generate -t modules/openapi-generator/src/main/resources/powershell-experimental -i modules/openapi-generator/src/test/resources/3_0/powershell/petstore.yaml -g powershell-experimental -o samples/client/petstore/powershell-experimental --additional-properties powershellGalleryUrl=https://www.powershellgallery.com/packages/PSPetstore,packageGuid=a27b908d-2a20-467f-bc32-af6f3a654ac5,packageName=PSPetstore,apiNamePrefix=PS,packageVersion=0.1.2,commonVerbs=Delete=Remove:Patch=Update $@"

java ${JAVA_OPTS} -jar ${executable} ${ags}
1 change: 1 addition & 0 deletions docs/generators/powershell-experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ sidebar_label: powershell-experimental
| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|apiNamePrefix|Prefix that will be appended to all PS objects. Default: empty string. e.g. Pet => PSPet.| |null|
|commonVerbs|PS common verb mappings. e.g. Delete=Remove:Patch=Update to map Delete with Remove and Patch with Update accordingly.| |null|
|packageGuid|GUID for PowerShell module (e.g. a27b908d-2a20-467f-bc32-af6f3a654ac5). A random GUID will be generated by default.| |null|
|packageName|Client package name (e.g. PSTwitter).| |PSOpenAPITools|
|packageVersion|Package version (e.g. 0.1.2).| |0.1.2|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class PowerShellExperimentalClientCodegen extends DefaultCodegen implemen
protected String powershellGalleryUrl;
protected HashSet powershellVerbs;
protected Map<String, String> commonVerbs; // verbs not in the official ps verb list but can be mapped to one of the verbs
protected HashSet methodNames; // store a list of method names to detect duplicates

/**
* Constructs an instance of `PowerShellExperimentalClientCodegen`.
Expand Down Expand Up @@ -123,9 +124,137 @@ public PowerShellExperimentalClientCodegen() {
));

commonVerbs = new HashMap<String, String>();
commonVerbs.put("Create", "New");

Map<String, List<String>> verbMappings = new HashMap<String, List<String>>();

// common
verbMappings.put("Add", Arrays.asList("Append", "Attach", "Concatenate", "Insert"));
verbMappings.put("Clear", Arrays.asList("Flush", "Erase", "Release", "Unmark", "Unset", "Nullify"));
verbMappings.put("Close", Arrays.asList());
verbMappings.put("Copy", Arrays.asList("Duplicate", "Clone", "Replicate", "Sync"));
verbMappings.put("Enter", Arrays.asList("PushInto"));
verbMappings.put("Exit", Arrays.asList("PopOut"));
verbMappings.put("Find", Arrays.asList());
verbMappings.put("Format", Arrays.asList());
verbMappings.put("Get", Arrays.asList("Read", "Open", "Cat", "Type", "Dir", "Obtain", "Dump", "Acquire", "Examine", "Find", "Search"));
verbMappings.put("Hide", Arrays.asList("Block"));
verbMappings.put("Join", Arrays.asList("Combine", "Unite", "Connect", "Associate"));
verbMappings.put("Lock", Arrays.asList("RestrictSecure"));
verbMappings.put("Move", Arrays.asList("Transfer", "Name", "Migrate"));
verbMappings.put("New", Arrays.asList("Create", "Generate", "Build", "Make", "Allocate"));
verbMappings.put("Open", Arrays.asList());
verbMappings.put("Optimize", Arrays.asList());
verbMappings.put("Pop", Arrays.asList());
verbMappings.put("Push", Arrays.asList());
verbMappings.put("Redo", Arrays.asList());
verbMappings.put("Remove", Arrays.asList("Clear", "Cut", "Dispose", "Discard", "Erase"));
verbMappings.put("Rename", Arrays.asList("Change"));
verbMappings.put("Reset", Arrays.asList());
verbMappings.put("Search", Arrays.asList("FindLocate"));
verbMappings.put("Select", Arrays.asList("FindLocate"));
verbMappings.put("Set", Arrays.asList("Write", "Reset", "Assign", "Configure"));
verbMappings.put("Show", Arrays.asList("DisplayProduce"));
verbMappings.put("Skip", Arrays.asList("BypassJump"));
verbMappings.put("Split", Arrays.asList("parate"));
verbMappings.put("Step", Arrays.asList());
verbMappings.put("Switch", Arrays.asList());
verbMappings.put("Undo", Arrays.asList());
verbMappings.put("Unlock", Arrays.asList("Release", "Unrestrict", "Unsecure"));
verbMappings.put("Watch", Arrays.asList());

// communication
verbMappings.put("Connect", Arrays.asList("JoinTelnet"));
verbMappings.put("Disconnect", Arrays.asList("BreakLogoff"));
verbMappings.put("Read", Arrays.asList("Acquire", "Prompt", "Get"));
verbMappings.put("Receive", Arrays.asList("Read", "Accept", "Peek"));
verbMappings.put("Send", Arrays.asList("Put", "Broadcast", "Mail", "Fax"));
verbMappings.put("Write", Arrays.asList("PutPrint"));

// data
verbMappings.put("Backup", Arrays.asList(" Save", " Burn", " Replicate", "Sync"));
verbMappings.put("Checkpoint", Arrays.asList(" Diff"));
verbMappings.put("Compare", Arrays.asList(" Diff"));
verbMappings.put("Compress", Arrays.asList(" Compact"));
verbMappings.put("Convert", Arrays.asList(" Change", " Resize", "Resample"));
verbMappings.put("ConvertFrom", Arrays.asList(" Export", " Output", "Out"));
verbMappings.put("ConvertTo", Arrays.asList(" Import", " Input", "In"));
verbMappings.put("Dismount", Arrays.asList(" UnmountUnlink"));
verbMappings.put("Edit", Arrays.asList(" Change", " Update", "Modify"));
verbMappings.put("Expand", Arrays.asList(" ExplodeUncompress"));
verbMappings.put("Export", Arrays.asList(" ExtractBackup"));
verbMappings.put("Group", Arrays.asList(" Aggregate", " Arrange", " Associate", "Correlate"));
verbMappings.put("Import", Arrays.asList(" BulkLoadLoad"));
verbMappings.put("Initialize", Arrays.asList(" Erase", " Init", " Renew", " Rebuild", " Reinitialize", "Setup"));
verbMappings.put("Limit", Arrays.asList(" Quota"));
verbMappings.put("Merge", Arrays.asList(" CombineJoin"));
verbMappings.put("Mount", Arrays.asList(" Connect"));
verbMappings.put("Out", Arrays.asList());
verbMappings.put("Publish", Arrays.asList(" Deploy", " Release", "Install"));
verbMappings.put("Restore", Arrays.asList(" Repair", " Return", " Undo", "Fix"));
verbMappings.put("Save", Arrays.asList());
verbMappings.put("Sync", Arrays.asList(" Replicate", " Coerce", "Match"));
verbMappings.put("Unpublish", Arrays.asList(" Uninstall", " Revert", "Hide"));
verbMappings.put("Update", Arrays.asList(" Refresh", " Renew", " Recalculate", "Re-index"));

// diagnostic
verbMappings.put("Debug", Arrays.asList("Diagnose"));
verbMappings.put("Measure", Arrays.asList("Calculate", "Determine", "Analyze"));
verbMappings.put("Ping", Arrays.asList());
verbMappings.put("Repair", Arrays.asList("FixRestore"));
verbMappings.put("Resolve", Arrays.asList("ExpandDetermine"));
verbMappings.put("Test", Arrays.asList("Diagnose", "Analyze", "Salvage", "Verify"));
verbMappings.put("Trace", Arrays.asList("Track", "Follow", "Inspect", "Dig"));

// lifecycle
verbMappings.put("Approve", Arrays.asList());
verbMappings.put("Assert", Arrays.asList("Certify"));
verbMappings.put("Build", Arrays.asList());
verbMappings.put("Complete", Arrays.asList());
verbMappings.put("Confirm", Arrays.asList("Acknowledge", "Agree", "Certify", "Validate", "Verify"));
verbMappings.put("Deny", Arrays.asList("Block", "Object", "Refuse", "Reject"));
verbMappings.put("Deploy", Arrays.asList());
verbMappings.put("Disable", Arrays.asList("HaltHide"));
verbMappings.put("Enable", Arrays.asList("StartBegin"));
verbMappings.put("Install", Arrays.asList("Setup"));
verbMappings.put("Invoke", Arrays.asList("RunStart"));
verbMappings.put("Register", Arrays.asList());
verbMappings.put("Request", Arrays.asList());
verbMappings.put("Restart", Arrays.asList("Recycle"));
verbMappings.put("Resume", Arrays.asList());
verbMappings.put("Start", Arrays.asList("Launch", "Initiate", "Boot"));
verbMappings.put("Stop", Arrays.asList("End", "Kill", "Terminate", "Cancel"));
verbMappings.put("Submit", Arrays.asList("Post"));
verbMappings.put("Suspend", Arrays.asList("Pause"));
verbMappings.put("Uninstall", Arrays.asList());
verbMappings.put("Unregister", Arrays.asList("Remove"));
verbMappings.put("Wait", Arrays.asList("SleepPause"));

// security
verbMappings.put("Block", Arrays.asList("Prevent", "Limit", "Deny"));
verbMappings.put("Grant", Arrays.asList("AllowEnable"));
verbMappings.put("Protect", Arrays.asList("Encrypt", "Safeguard", "Seal"));
verbMappings.put("Revoke", Arrays.asList("RemoveDisable"));
verbMappings.put("Unblock", Arrays.asList("ClearAllow"));
verbMappings.put("Unprotect", Arrays.asList("DecryptUnseal"));

// other
verbMappings.put("Use", Arrays.asList());

for (Map.Entry<String, List<String>> entry : verbMappings.entrySet()) {
// loop through each verb in the list
for (String verb : entry.getValue()) {
if (verbMappings.containsKey(verb)) {
// the verb to be mapped is also a common verb, do nothing
LOGGER.debug("verbmapping: skipped {}", verb);
} else {
commonVerbs.put(verb, entry.getKey());
LOGGER.debug("verbmapping: adding {} => {}", verb, entry.getKey());
}
}
}

// additional common verbs mapping
commonVerbs.put("Delete", "Remove");
commonVerbs.put("Update", "Set");

powershellVerbs = new HashSet<String>(Arrays.asList(
"Add",
Expand Down Expand Up @@ -229,6 +358,7 @@ public PowerShellExperimentalClientCodegen() {
"Use"
));

methodNames = new HashSet<String>();

nullablePrimitives = new HashSet<String>(Arrays.asList(
"System.Nullable[Byte]",
Expand Down Expand Up @@ -280,7 +410,6 @@ public PowerShellExperimentalClientCodegen() {
"Where"
));


defaultIncludes = new HashSet<String>(Arrays.asList(
"Byte",
"SByte",
Expand Down Expand Up @@ -333,6 +462,7 @@ public PowerShellExperimentalClientCodegen() {
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "Package version (e.g. 0.1.2).").defaultValue(this.packageVersion));
cliOptions.add(new CliOption(CodegenConstants.OPTIONAL_PROJECT_GUID, "GUID for PowerShell module (e.g. a27b908d-2a20-467f-bc32-af6f3a654ac5). A random GUID will be generated by default."));
cliOptions.add(new CliOption(CodegenConstants.API_NAME_PREFIX, "Prefix that will be appended to all PS objects. Default: empty string. e.g. Pet => PSPet."));
cliOptions.add(new CliOption("commonVerbs", "PS common verb mappings. e.g. Delete=Remove:Patch=Update to map Delete with Remove and Patch with Update accordingly."));

}

Expand Down Expand Up @@ -404,6 +534,19 @@ public void processOpts() {
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
}

if (additionalProperties.containsKey("commonVerbs")) {
String[] entries = ((String)additionalProperties.get("commonVerbs")).split(":");
for (String entry : entries) {
String[] pair = entry.split("=");
if (pair.length == 2) {
commonVerbs.put(pair[0], pair[1]);
LOGGER.debug("Add commonVerbs: {} => {}", pair[0], pair[1]);
} else {
LOGGER.error("Failed to parse commonVerbs: {}", entry);
}
}
}

if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
LOGGER.warn(CodegenConstants.MODEL_PACKAGE + " with " + this.getName() + " generator is ignored. Setting this value independently of " + CodegenConstants.PACKAGE_NAME + " is not currently supported.");
}
Expand Down Expand Up @@ -648,6 +791,13 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
} else {
op.vendorExtensions.put("x-powershell-method-name-lowercase", ((String) op.vendorExtensions.get("x-powershell-method-name")).toLowerCase(Locale.ROOT));
}

// detect duplicated method name
if (methodNames.contains(op.vendorExtensions.get("x-powershell-method-name"))) {
LOGGER.error("Duplicated method name found: {}", op.vendorExtensions.get("x-powershell-method-name"));
} else {
methodNames.add(op.vendorExtensions.get("x-powershell-method-name"));
}
}

processedModelMaps.clear();
Expand Down

0 comments on commit dbcb652

Please sign in to comment.