Skip to content

Commit 53f7e47

Browse files
authored
unity error message and code cleanup (#20595)
1 parent 055605b commit 53f7e47

File tree

1 file changed

+86
-68
lines changed

1 file changed

+86
-68
lines changed

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

Lines changed: 86 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,8 @@ public void processOpts() {
698698

699699
super.processOpts();
700700

701+
final String library = getLibrary();
702+
701703
/*
702704
* NOTE: When supporting boolean additionalProperties, you should read the value and write it back as a boolean.
703705
* This avoids oddities where additionalProperties contains "false" rather than false, which will cause the
@@ -753,27 +755,37 @@ public void processOpts() {
753755
setModelPackage("Model");
754756
}
755757

756-
if (GENERICHOST.equals(getLibrary())) {
757-
setLibrary(GENERICHOST);
758-
additionalProperties.put("useGenericHost", true);
759-
} else if (RESTSHARP.equals(getLibrary())) {
760-
additionalProperties.put("useRestSharp", true);
761-
needsCustomHttpMethod = true;
762-
} else if (HTTPCLIENT.equals(getLibrary())) {
763-
setLibrary(HTTPCLIENT);
764-
additionalProperties.put("useHttpClient", true);
765-
needsUriBuilder = true;
766-
} else if (UNITY_WEB_REQUEST.equals(getLibrary())) {
767-
setLibrary(UNITY_WEB_REQUEST);
768-
additionalProperties.put("useUnityWebRequest", true);
769-
needsUriBuilder = true;
758+
final Map<String, Runnable> libraryActions = Map.of(
759+
GENERICHOST, () -> {
760+
setLibrary(GENERICHOST);
761+
additionalProperties.put("useGenericHost", true);
762+
},
763+
RESTSHARP, () -> {
764+
additionalProperties.put("useRestSharp", true);
765+
needsCustomHttpMethod = true;
766+
},
767+
HTTPCLIENT, () -> {
768+
setLibrary(HTTPCLIENT);
769+
additionalProperties.put("useHttpClient", true);
770+
needsUriBuilder = true;
771+
},
772+
UNITY_WEB_REQUEST, () -> {
773+
setLibrary(UNITY_WEB_REQUEST);
774+
additionalProperties.put("useUnityWebRequest", true);
775+
needsUriBuilder = true;
776+
}
777+
);
778+
final Runnable action = libraryActions.get(library);
779+
if (action != null) {
780+
action.run();
770781
} else {
771-
throw new RuntimeException("Invalid HTTP library " + getLibrary() + ". Only restsharp, httpclient, and generichost are supported.");
782+
String supportedLibraries = String.join(", ", libraryActions.keySet());
783+
throw new RuntimeException("Invalid HTTP library " + library + ". Only " + supportedLibraries + " are supported.");
772784
}
773785

774-
String inputFramework = (String) additionalProperties.getOrDefault(CodegenConstants.DOTNET_FRAMEWORK, latestFramework.name);
775-
String[] frameworks;
776-
List<FrameworkStrategy> strategies = new ArrayList<>();
786+
final String inputFramework = (String) additionalProperties.getOrDefault(CodegenConstants.DOTNET_FRAMEWORK, latestFramework.name);
787+
final String[] frameworks;
788+
final List<FrameworkStrategy> strategies = new ArrayList<>();
777789

778790
if (inputFramework.contains(";")) {
779791
// multiple target framework
@@ -792,7 +804,7 @@ public void processOpts() {
792804
strategyMatched = true;
793805
}
794806

795-
if (frameworkStrategy != FrameworkStrategy.NETSTANDARD_2_0 && "restsharp".equals(getLibrary())) {
807+
if (frameworkStrategy != FrameworkStrategy.NETSTANDARD_2_0 && RESTSHARP.equals(library)) {
796808
LOGGER.warn("If using built-in templates, RestSharp only supports netstandard 2.0 or later.");
797809
}
798810
}
@@ -876,39 +888,43 @@ public void processOpts() {
876888
apiTestTemplateFiles.put("api_test.mustache", ".cs");
877889
}
878890

879-
if (HTTPCLIENT.equals(getLibrary())) {
880-
supportingFiles.add(new SupportingFile("FileParameter.mustache", clientPackageDir, "FileParameter.cs"));
881-
addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir);
882-
additionalProperties.put("apiDocPath", apiDocPath);
883-
additionalProperties.put("modelDocPath", modelDocPath);
884-
} else if (GENERICHOST.equals(getLibrary())) {
885-
addGenericHostSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir);
886-
additionalProperties.put("apiDocPath", apiDocPath + File.separatorChar + "apis");
887-
additionalProperties.put("modelDocPath", modelDocPath + File.separatorChar + "models");
888-
} else if (UNITY_WEB_REQUEST.equals(getLibrary())) {
889-
additionalProperties.put(CodegenConstants.VALIDATABLE, false);
890-
setValidatable(false);
891-
setSupportsRetry(false);
892-
setSupportsAsync(true);
893-
// Some consoles and tvOS do not support either Application.persistentDataPath or will refuse to
894-
// compile/link if you even reference GetTempPath as well.
895-
additionalProperties.put("supportsFileParameters", false);
896-
setSupportsFileParameters(false);
897-
898-
addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir);
899-
900-
supportingFiles.add(new SupportingFile("ConnectionException.mustache", clientPackageDir, "ConnectionException.cs"));
901-
supportingFiles.add(new SupportingFile("UnexpectedResponseException.mustache", clientPackageDir, "UnexpectedResponseException.cs"));
902-
} else { //restsharp
903-
addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir);
904-
additionalProperties.put("apiDocPath", apiDocPath);
905-
additionalProperties.put("modelDocPath", modelDocPath);
906-
907-
if (ProcessUtils.hasOAuthMethods(openAPI)) {
908-
supportingFiles.add(new SupportingFile("auth/OAuthAuthenticator.mustache", authPackageDir, "OAuthAuthenticator.cs"));
909-
supportingFiles.add(new SupportingFile("auth/TokenResponse.mustache", authPackageDir, "TokenResponse.cs"));
910-
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authPackageDir, "OAuthFlow.cs"));
911-
}
891+
switch (library) {
892+
case HTTPCLIENT:
893+
supportingFiles.add(new SupportingFile("FileParameter.mustache", clientPackageDir, "FileParameter.cs"));
894+
addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir);
895+
additionalProperties.put("apiDocPath", apiDocPath);
896+
additionalProperties.put("modelDocPath", modelDocPath);
897+
break;
898+
case GENERICHOST:
899+
addGenericHostSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir);
900+
additionalProperties.put("apiDocPath", apiDocPath + File.separatorChar + "apis");
901+
additionalProperties.put("modelDocPath", modelDocPath + File.separatorChar + "models");
902+
break;
903+
case UNITY_WEB_REQUEST:
904+
additionalProperties.put(CodegenConstants.VALIDATABLE, false);
905+
setValidatable(false);
906+
setSupportsRetry(false);
907+
setSupportsAsync(true);
908+
// Some consoles and tvOS do not support either Application.persistentDataPath or will refuse to
909+
// compile/link if you even reference GetTempPath as well.
910+
additionalProperties.put("supportsFileParameters", false);
911+
setSupportsFileParameters(false);
912+
913+
addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir);
914+
supportingFiles.add(new SupportingFile("ConnectionException.mustache", clientPackageDir, "ConnectionException.cs"));
915+
supportingFiles.add(new SupportingFile("UnexpectedResponseException.mustache", clientPackageDir, "UnexpectedResponseException.cs"));
916+
break;
917+
default: // restsharp
918+
addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir);
919+
additionalProperties.put("apiDocPath", apiDocPath);
920+
additionalProperties.put("modelDocPath", modelDocPath);
921+
922+
if (ProcessUtils.hasOAuthMethods(openAPI)) {
923+
supportingFiles.add(new SupportingFile("auth/OAuthAuthenticator.mustache", authPackageDir, "OAuthAuthenticator.cs"));
924+
supportingFiles.add(new SupportingFile("auth/TokenResponse.mustache", authPackageDir, "TokenResponse.cs"));
925+
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authPackageDir, "OAuthFlow.cs"));
926+
}
927+
break;
912928
}
913929

914930
if (useDateOnly()) {
@@ -998,7 +1014,9 @@ public CodegenOperation fromOperation(String path,
9981014

9991015
public void addSupportingFiles(final String clientPackageDir, final String packageFolder,
10001016
final AtomicReference<Boolean> excludeTests, final String testPackageFolder, final String testPackageName, final String modelPackageDir, final String authPackageDir) {
1001-
if (RESTSHARP.equals(getLibrary())) { // restsharp
1017+
final String library = getLibrary();
1018+
1019+
if (RESTSHARP.equals(library)) { // restsharp
10021020
if (useIntForTimeout) { // option to fall back to int for Timeout using v7.9.0 template
10031021
supportingFiles.add(new SupportingFile("ApiClient.v790.mustache", clientPackageDir, "ApiClient.cs"));
10041022
supportingFiles.add(new SupportingFile("IReadableConfiguration.v790.mustache", clientPackageDir, "IReadableConfiguration.cs"));
@@ -1046,22 +1064,22 @@ public void addSupportingFiles(final String clientPackageDir, final String packa
10461064
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
10471065
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
10481066

1049-
if (UNITY_WEB_REQUEST.equals(getLibrary())) {
1067+
if (UNITY_WEB_REQUEST.equals(library)) {
10501068
supportingFiles.add(new SupportingFile("asmdef.mustache", packageFolder, packageName + ".asmdef"));
10511069
} else {
10521070
supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln"));
10531071
supportingFiles.add(new SupportingFile("netcore_project.mustache", packageFolder, packageName + ".csproj"));
10541072
}
10551073

10561074
if (Boolean.FALSE.equals(excludeTests.get())) {
1057-
if (UNITY_WEB_REQUEST.equals(getLibrary())) {
1075+
if (UNITY_WEB_REQUEST.equals(library)) {
10581076
supportingFiles.add(new SupportingFile("asmdef_test.mustache", testPackageFolder, testPackageName + ".asmdef"));
10591077
} else {
10601078
supportingFiles.add(new SupportingFile("netcore_testproject.mustache", testPackageFolder, testPackageName + ".csproj"));
10611079
}
10621080
}
10631081

1064-
if (!UNITY_WEB_REQUEST.equals(getLibrary())) {
1082+
if (!UNITY_WEB_REQUEST.equals(library)) {
10651083
supportingFiles.add(new SupportingFile("appveyor.mustache", "", "appveyor.yml"));
10661084
}
10671085
supportingFiles.add(new SupportingFile("AbstractOpenAPISchema.mustache", modelPackageDir, "AbstractOpenAPISchema.cs"));
@@ -1622,24 +1640,23 @@ public ModelsMap postProcessModels(ModelsMap objs) {
16221640
for (ModelMap mo : objs.getModels()) {
16231641
CodegenModel cm = mo.getModel();
16241642

1625-
if (cm.oneOf != null && !cm.oneOf.isEmpty() && cm.oneOf.contains("Null")) {
1643+
if (cm.oneOf != null && !cm.oneOf.isEmpty() && cm.oneOf.remove("Null")) {
16261644
// if oneOf contains "null" type
16271645
cm.isNullable = true;
1628-
cm.oneOf.remove("Null");
16291646
}
16301647

1631-
if (cm.anyOf != null && !cm.anyOf.isEmpty() && cm.anyOf.contains("Null")) {
1648+
if (cm.anyOf != null && !cm.anyOf.isEmpty() && cm.anyOf.remove("Null")) {
16321649
// if anyOf contains "null" type
16331650
cm.isNullable = true;
1634-
cm.anyOf.remove("Null");
1635-
}
1636-
1637-
if (cm.getComposedSchemas() != null && cm.getComposedSchemas().getOneOf() != null && !cm.getComposedSchemas().getOneOf().isEmpty()) {
1638-
cm.getComposedSchemas().getOneOf().removeIf(o -> o.dataType.equals("Null"));
16391651
}
16401652

1641-
if (cm.getComposedSchemas() != null && cm.getComposedSchemas().getAnyOf() != null && !cm.getComposedSchemas().getAnyOf().isEmpty()) {
1642-
cm.getComposedSchemas().getAnyOf().removeIf(o -> o.dataType.equals("Null"));
1653+
if (cm.getComposedSchemas() != null) {
1654+
if (cm.getComposedSchemas().getOneOf() != null) {
1655+
cm.getComposedSchemas().getOneOf().removeIf(o -> "Null".equals(o.dataType));
1656+
}
1657+
if (cm.getComposedSchemas().getAnyOf() != null) {
1658+
cm.getComposedSchemas().getAnyOf().removeIf(o -> "Null".equals(o.dataType));
1659+
}
16431660
}
16441661

16451662
for (CodegenProperty cp : cm.readWriteVars) {
@@ -1648,7 +1665,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
16481665
// see modules\openapi-generator\src\test\resources\3_0\allOf.yaml
16491666
// property boosterSeat will be in readWriteVars but not allVars
16501667
// the property is present in the model but gets removed at CodegenModel#removeDuplicatedProperty
1651-
if (Boolean.FALSE.equals(cm.allVars.stream().anyMatch(v -> v.baseName.equals(cp.baseName)))) {
1668+
if (cm.allVars.stream().noneMatch(v -> v.baseName.equals(cp.baseName))) {
16521669
LOGGER.debug("Property " + cp.baseName + " was found in readWriteVars but not in allVars. Adding it back to allVars");
16531670
cm.allVars.add(cp);
16541671
}
@@ -1658,6 +1675,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
16581675
return objs;
16591676
}
16601677

1678+
16611679
// https://github.com/OpenAPITools/openapi-generator/issues/15867
16621680
@Override
16631681
protected void removePropertiesDeclaredInComposedTypes(Map<String, ModelsMap> objs, CodegenModel model, List<CodegenProperty> composedProperties) {
@@ -1703,7 +1721,7 @@ protected void removePropertiesDeclaredInComposedTypes(Map<String, ModelsMap> ob
17031721
@Override
17041722
protected boolean isValueType(CodegenProperty var) {
17051723
// this is temporary until x-csharp-value-type is removed
1706-
return this.getLibrary().equals("generichost")
1724+
return this.getLibrary().equals(GENERICHOST)
17071725
? super.isValueType(var)
17081726
: this.getValueTypes().contains(var.dataType) || var.isEnum;
17091727
}

0 commit comments

Comments
 (0)