Skip to content

Commit 4623ec8

Browse files
authored
[PS][Experimental] Better common verb handling (#5783)
* better common verb handling * better debugging * add option to customize common verb
1 parent 12440ca commit 4623ec8

File tree

3 files changed

+155
-4
lines changed

3 files changed

+155
-4
lines changed

bin/openapi3/powershell-experimental-petstore.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ fi
2727

2828
# if you've executed sbt assembly previously it will use that instead.
2929
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
30-
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 $@"
30+
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 $@"
3131

3232
java ${JAVA_OPTS} -jar ${executable} ${ags}

docs/generators/powershell-experimental.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ sidebar_label: powershell-experimental
66
| Option | Description | Values | Default |
77
| ------ | ----------- | ------ | ------- |
88
|apiNamePrefix|Prefix that will be appended to all PS objects. Default: empty string. e.g. Pet => PSPet.| |null|
9+
|commonVerbs|PS common verb mappings. e.g. Delete=Remove:Patch=Update to map Delete with Remove and Patch with Update accordingly.| |null|
910
|packageGuid|GUID for PowerShell module (e.g. a27b908d-2a20-467f-bc32-af6f3a654ac5). A random GUID will be generated by default.| |null|
1011
|packageName|Client package name (e.g. PSTwitter).| |PSOpenAPITools|
1112
|packageVersion|Package version (e.g. 0.1.2).| |0.1.2|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellExperimentalClientCodegen.java

Lines changed: 153 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class PowerShellExperimentalClientCodegen extends DefaultCodegen implemen
5050
protected String powershellGalleryUrl;
5151
protected HashSet powershellVerbs;
5252
protected Map<String, String> commonVerbs; // verbs not in the official ps verb list but can be mapped to one of the verbs
53+
protected HashSet methodNames; // store a list of method names to detect duplicates
5354

5455
/**
5556
* Constructs an instance of `PowerShellExperimentalClientCodegen`.
@@ -123,9 +124,137 @@ public PowerShellExperimentalClientCodegen() {
123124
));
124125

125126
commonVerbs = new HashMap<String, String>();
126-
commonVerbs.put("Create", "New");
127+
128+
Map<String, List<String>> verbMappings = new HashMap<String, List<String>>();
129+
130+
// common
131+
verbMappings.put("Add", Arrays.asList("Append", "Attach", "Concatenate", "Insert"));
132+
verbMappings.put("Clear", Arrays.asList("Flush", "Erase", "Release", "Unmark", "Unset", "Nullify"));
133+
verbMappings.put("Close", Arrays.asList());
134+
verbMappings.put("Copy", Arrays.asList("Duplicate", "Clone", "Replicate", "Sync"));
135+
verbMappings.put("Enter", Arrays.asList("PushInto"));
136+
verbMappings.put("Exit", Arrays.asList("PopOut"));
137+
verbMappings.put("Find", Arrays.asList());
138+
verbMappings.put("Format", Arrays.asList());
139+
verbMappings.put("Get", Arrays.asList("Read", "Open", "Cat", "Type", "Dir", "Obtain", "Dump", "Acquire", "Examine", "Find", "Search"));
140+
verbMappings.put("Hide", Arrays.asList("Block"));
141+
verbMappings.put("Join", Arrays.asList("Combine", "Unite", "Connect", "Associate"));
142+
verbMappings.put("Lock", Arrays.asList("RestrictSecure"));
143+
verbMappings.put("Move", Arrays.asList("Transfer", "Name", "Migrate"));
144+
verbMappings.put("New", Arrays.asList("Create", "Generate", "Build", "Make", "Allocate"));
145+
verbMappings.put("Open", Arrays.asList());
146+
verbMappings.put("Optimize", Arrays.asList());
147+
verbMappings.put("Pop", Arrays.asList());
148+
verbMappings.put("Push", Arrays.asList());
149+
verbMappings.put("Redo", Arrays.asList());
150+
verbMappings.put("Remove", Arrays.asList("Clear", "Cut", "Dispose", "Discard", "Erase"));
151+
verbMappings.put("Rename", Arrays.asList("Change"));
152+
verbMappings.put("Reset", Arrays.asList());
153+
verbMappings.put("Search", Arrays.asList("FindLocate"));
154+
verbMappings.put("Select", Arrays.asList("FindLocate"));
155+
verbMappings.put("Set", Arrays.asList("Write", "Reset", "Assign", "Configure"));
156+
verbMappings.put("Show", Arrays.asList("DisplayProduce"));
157+
verbMappings.put("Skip", Arrays.asList("BypassJump"));
158+
verbMappings.put("Split", Arrays.asList("parate"));
159+
verbMappings.put("Step", Arrays.asList());
160+
verbMappings.put("Switch", Arrays.asList());
161+
verbMappings.put("Undo", Arrays.asList());
162+
verbMappings.put("Unlock", Arrays.asList("Release", "Unrestrict", "Unsecure"));
163+
verbMappings.put("Watch", Arrays.asList());
164+
165+
// communication
166+
verbMappings.put("Connect", Arrays.asList("JoinTelnet"));
167+
verbMappings.put("Disconnect", Arrays.asList("BreakLogoff"));
168+
verbMappings.put("Read", Arrays.asList("Acquire", "Prompt", "Get"));
169+
verbMappings.put("Receive", Arrays.asList("Read", "Accept", "Peek"));
170+
verbMappings.put("Send", Arrays.asList("Put", "Broadcast", "Mail", "Fax"));
171+
verbMappings.put("Write", Arrays.asList("PutPrint"));
172+
173+
// data
174+
verbMappings.put("Backup", Arrays.asList(" Save", " Burn", " Replicate", "Sync"));
175+
verbMappings.put("Checkpoint", Arrays.asList(" Diff"));
176+
verbMappings.put("Compare", Arrays.asList(" Diff"));
177+
verbMappings.put("Compress", Arrays.asList(" Compact"));
178+
verbMappings.put("Convert", Arrays.asList(" Change", " Resize", "Resample"));
179+
verbMappings.put("ConvertFrom", Arrays.asList(" Export", " Output", "Out"));
180+
verbMappings.put("ConvertTo", Arrays.asList(" Import", " Input", "In"));
181+
verbMappings.put("Dismount", Arrays.asList(" UnmountUnlink"));
182+
verbMappings.put("Edit", Arrays.asList(" Change", " Update", "Modify"));
183+
verbMappings.put("Expand", Arrays.asList(" ExplodeUncompress"));
184+
verbMappings.put("Export", Arrays.asList(" ExtractBackup"));
185+
verbMappings.put("Group", Arrays.asList(" Aggregate", " Arrange", " Associate", "Correlate"));
186+
verbMappings.put("Import", Arrays.asList(" BulkLoadLoad"));
187+
verbMappings.put("Initialize", Arrays.asList(" Erase", " Init", " Renew", " Rebuild", " Reinitialize", "Setup"));
188+
verbMappings.put("Limit", Arrays.asList(" Quota"));
189+
verbMappings.put("Merge", Arrays.asList(" CombineJoin"));
190+
verbMappings.put("Mount", Arrays.asList(" Connect"));
191+
verbMappings.put("Out", Arrays.asList());
192+
verbMappings.put("Publish", Arrays.asList(" Deploy", " Release", "Install"));
193+
verbMappings.put("Restore", Arrays.asList(" Repair", " Return", " Undo", "Fix"));
194+
verbMappings.put("Save", Arrays.asList());
195+
verbMappings.put("Sync", Arrays.asList(" Replicate", " Coerce", "Match"));
196+
verbMappings.put("Unpublish", Arrays.asList(" Uninstall", " Revert", "Hide"));
197+
verbMappings.put("Update", Arrays.asList(" Refresh", " Renew", " Recalculate", "Re-index"));
198+
199+
// diagnostic
200+
verbMappings.put("Debug", Arrays.asList("Diagnose"));
201+
verbMappings.put("Measure", Arrays.asList("Calculate", "Determine", "Analyze"));
202+
verbMappings.put("Ping", Arrays.asList());
203+
verbMappings.put("Repair", Arrays.asList("FixRestore"));
204+
verbMappings.put("Resolve", Arrays.asList("ExpandDetermine"));
205+
verbMappings.put("Test", Arrays.asList("Diagnose", "Analyze", "Salvage", "Verify"));
206+
verbMappings.put("Trace", Arrays.asList("Track", "Follow", "Inspect", "Dig"));
207+
208+
// lifecycle
209+
verbMappings.put("Approve", Arrays.asList());
210+
verbMappings.put("Assert", Arrays.asList("Certify"));
211+
verbMappings.put("Build", Arrays.asList());
212+
verbMappings.put("Complete", Arrays.asList());
213+
verbMappings.put("Confirm", Arrays.asList("Acknowledge", "Agree", "Certify", "Validate", "Verify"));
214+
verbMappings.put("Deny", Arrays.asList("Block", "Object", "Refuse", "Reject"));
215+
verbMappings.put("Deploy", Arrays.asList());
216+
verbMappings.put("Disable", Arrays.asList("HaltHide"));
217+
verbMappings.put("Enable", Arrays.asList("StartBegin"));
218+
verbMappings.put("Install", Arrays.asList("Setup"));
219+
verbMappings.put("Invoke", Arrays.asList("RunStart"));
220+
verbMappings.put("Register", Arrays.asList());
221+
verbMappings.put("Request", Arrays.asList());
222+
verbMappings.put("Restart", Arrays.asList("Recycle"));
223+
verbMappings.put("Resume", Arrays.asList());
224+
verbMappings.put("Start", Arrays.asList("Launch", "Initiate", "Boot"));
225+
verbMappings.put("Stop", Arrays.asList("End", "Kill", "Terminate", "Cancel"));
226+
verbMappings.put("Submit", Arrays.asList("Post"));
227+
verbMappings.put("Suspend", Arrays.asList("Pause"));
228+
verbMappings.put("Uninstall", Arrays.asList());
229+
verbMappings.put("Unregister", Arrays.asList("Remove"));
230+
verbMappings.put("Wait", Arrays.asList("SleepPause"));
231+
232+
// security
233+
verbMappings.put("Block", Arrays.asList("Prevent", "Limit", "Deny"));
234+
verbMappings.put("Grant", Arrays.asList("AllowEnable"));
235+
verbMappings.put("Protect", Arrays.asList("Encrypt", "Safeguard", "Seal"));
236+
verbMappings.put("Revoke", Arrays.asList("RemoveDisable"));
237+
verbMappings.put("Unblock", Arrays.asList("ClearAllow"));
238+
verbMappings.put("Unprotect", Arrays.asList("DecryptUnseal"));
239+
240+
// other
241+
verbMappings.put("Use", Arrays.asList());
242+
243+
for (Map.Entry<String, List<String>> entry : verbMappings.entrySet()) {
244+
// loop through each verb in the list
245+
for (String verb : entry.getValue()) {
246+
if (verbMappings.containsKey(verb)) {
247+
// the verb to be mapped is also a common verb, do nothing
248+
LOGGER.debug("verbmapping: skipped {}", verb);
249+
} else {
250+
commonVerbs.put(verb, entry.getKey());
251+
LOGGER.debug("verbmapping: adding {} => {}", verb, entry.getKey());
252+
}
253+
}
254+
}
255+
256+
// additional common verbs mapping
127257
commonVerbs.put("Delete", "Remove");
128-
commonVerbs.put("Update", "Set");
129258

130259
powershellVerbs = new HashSet<String>(Arrays.asList(
131260
"Add",
@@ -229,6 +358,7 @@ public PowerShellExperimentalClientCodegen() {
229358
"Use"
230359
));
231360

361+
methodNames = new HashSet<String>();
232362

233363
nullablePrimitives = new HashSet<String>(Arrays.asList(
234364
"System.Nullable[Byte]",
@@ -280,7 +410,6 @@ public PowerShellExperimentalClientCodegen() {
280410
"Where"
281411
));
282412

283-
284413
defaultIncludes = new HashSet<String>(Arrays.asList(
285414
"Byte",
286415
"SByte",
@@ -333,6 +462,7 @@ public PowerShellExperimentalClientCodegen() {
333462
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "Package version (e.g. 0.1.2).").defaultValue(this.packageVersion));
334463
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."));
335464
cliOptions.add(new CliOption(CodegenConstants.API_NAME_PREFIX, "Prefix that will be appended to all PS objects. Default: empty string. e.g. Pet => PSPet."));
465+
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."));
336466

337467
}
338468

@@ -404,6 +534,19 @@ public void processOpts() {
404534
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
405535
}
406536

537+
if (additionalProperties.containsKey("commonVerbs")) {
538+
String[] entries = ((String)additionalProperties.get("commonVerbs")).split(":");
539+
for (String entry : entries) {
540+
String[] pair = entry.split("=");
541+
if (pair.length == 2) {
542+
commonVerbs.put(pair[0], pair[1]);
543+
LOGGER.debug("Add commonVerbs: {} => {}", pair[0], pair[1]);
544+
} else {
545+
LOGGER.error("Failed to parse commonVerbs: {}", entry);
546+
}
547+
}
548+
}
549+
407550
if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
408551
LOGGER.warn(CodegenConstants.MODEL_PACKAGE + " with " + this.getName() + " generator is ignored. Setting this value independently of " + CodegenConstants.PACKAGE_NAME + " is not currently supported.");
409552
}
@@ -648,6 +791,13 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
648791
} else {
649792
op.vendorExtensions.put("x-powershell-method-name-lowercase", ((String) op.vendorExtensions.get("x-powershell-method-name")).toLowerCase(Locale.ROOT));
650793
}
794+
795+
// detect duplicated method name
796+
if (methodNames.contains(op.vendorExtensions.get("x-powershell-method-name"))) {
797+
LOGGER.error("Duplicated method name found: {}", op.vendorExtensions.get("x-powershell-method-name"));
798+
} else {
799+
methodNames.add(op.vendorExtensions.get("x-powershell-method-name"));
800+
}
651801
}
652802

653803
processedModelMaps.clear();

0 commit comments

Comments
 (0)