unity error message and code cleanup (#20595)

This commit is contained in:
Blake Lassiter 2025-02-09 05:56:00 -08:00 committed by GitHub
parent 055605b075
commit 53f7e471c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -698,6 +698,8 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
super.processOpts(); super.processOpts();
final String library = getLibrary();
/* /*
* NOTE: When supporting boolean additionalProperties, you should read the value and write it back as a boolean. * NOTE: When supporting boolean additionalProperties, you should read the value and write it back as a boolean.
* This avoids oddities where additionalProperties contains "false" rather than false, which will cause the * This avoids oddities where additionalProperties contains "false" rather than false, which will cause the
@ -753,27 +755,37 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
setModelPackage("Model"); setModelPackage("Model");
} }
if (GENERICHOST.equals(getLibrary())) { final Map<String, Runnable> libraryActions = Map.of(
setLibrary(GENERICHOST); GENERICHOST, () -> {
additionalProperties.put("useGenericHost", true); setLibrary(GENERICHOST);
} else if (RESTSHARP.equals(getLibrary())) { additionalProperties.put("useGenericHost", true);
additionalProperties.put("useRestSharp", true); },
needsCustomHttpMethod = true; RESTSHARP, () -> {
} else if (HTTPCLIENT.equals(getLibrary())) { additionalProperties.put("useRestSharp", true);
setLibrary(HTTPCLIENT); needsCustomHttpMethod = true;
additionalProperties.put("useHttpClient", true); },
needsUriBuilder = true; HTTPCLIENT, () -> {
} else if (UNITY_WEB_REQUEST.equals(getLibrary())) { setLibrary(HTTPCLIENT);
setLibrary(UNITY_WEB_REQUEST); additionalProperties.put("useHttpClient", true);
additionalProperties.put("useUnityWebRequest", true); needsUriBuilder = true;
needsUriBuilder = true; },
UNITY_WEB_REQUEST, () -> {
setLibrary(UNITY_WEB_REQUEST);
additionalProperties.put("useUnityWebRequest", true);
needsUriBuilder = true;
}
);
final Runnable action = libraryActions.get(library);
if (action != null) {
action.run();
} else { } else {
throw new RuntimeException("Invalid HTTP library " + getLibrary() + ". Only restsharp, httpclient, and generichost are supported."); String supportedLibraries = String.join(", ", libraryActions.keySet());
throw new RuntimeException("Invalid HTTP library " + library + ". Only " + supportedLibraries + " are supported.");
} }
String inputFramework = (String) additionalProperties.getOrDefault(CodegenConstants.DOTNET_FRAMEWORK, latestFramework.name); final String inputFramework = (String) additionalProperties.getOrDefault(CodegenConstants.DOTNET_FRAMEWORK, latestFramework.name);
String[] frameworks; final String[] frameworks;
List<FrameworkStrategy> strategies = new ArrayList<>(); final List<FrameworkStrategy> strategies = new ArrayList<>();
if (inputFramework.contains(";")) { if (inputFramework.contains(";")) {
// multiple target framework // multiple target framework
@ -792,7 +804,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
strategyMatched = true; strategyMatched = true;
} }
if (frameworkStrategy != FrameworkStrategy.NETSTANDARD_2_0 && "restsharp".equals(getLibrary())) { if (frameworkStrategy != FrameworkStrategy.NETSTANDARD_2_0 && RESTSHARP.equals(library)) {
LOGGER.warn("If using built-in templates, RestSharp only supports netstandard 2.0 or later."); LOGGER.warn("If using built-in templates, RestSharp only supports netstandard 2.0 or later.");
} }
} }
@ -876,39 +888,43 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
apiTestTemplateFiles.put("api_test.mustache", ".cs"); apiTestTemplateFiles.put("api_test.mustache", ".cs");
} }
if (HTTPCLIENT.equals(getLibrary())) { switch (library) {
supportingFiles.add(new SupportingFile("FileParameter.mustache", clientPackageDir, "FileParameter.cs")); case HTTPCLIENT:
addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir); supportingFiles.add(new SupportingFile("FileParameter.mustache", clientPackageDir, "FileParameter.cs"));
additionalProperties.put("apiDocPath", apiDocPath); addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir);
additionalProperties.put("modelDocPath", modelDocPath); additionalProperties.put("apiDocPath", apiDocPath);
} else if (GENERICHOST.equals(getLibrary())) { additionalProperties.put("modelDocPath", modelDocPath);
addGenericHostSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir); break;
additionalProperties.put("apiDocPath", apiDocPath + File.separatorChar + "apis"); case GENERICHOST:
additionalProperties.put("modelDocPath", modelDocPath + File.separatorChar + "models"); addGenericHostSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir);
} else if (UNITY_WEB_REQUEST.equals(getLibrary())) { additionalProperties.put("apiDocPath", apiDocPath + File.separatorChar + "apis");
additionalProperties.put(CodegenConstants.VALIDATABLE, false); additionalProperties.put("modelDocPath", modelDocPath + File.separatorChar + "models");
setValidatable(false); break;
setSupportsRetry(false); case UNITY_WEB_REQUEST:
setSupportsAsync(true); additionalProperties.put(CodegenConstants.VALIDATABLE, false);
// Some consoles and tvOS do not support either Application.persistentDataPath or will refuse to setValidatable(false);
// compile/link if you even reference GetTempPath as well. setSupportsRetry(false);
additionalProperties.put("supportsFileParameters", false); setSupportsAsync(true);
setSupportsFileParameters(false); // Some consoles and tvOS do not support either Application.persistentDataPath or will refuse to
// compile/link if you even reference GetTempPath as well.
additionalProperties.put("supportsFileParameters", false);
setSupportsFileParameters(false);
addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir); addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir);
supportingFiles.add(new SupportingFile("ConnectionException.mustache", clientPackageDir, "ConnectionException.cs"));
supportingFiles.add(new SupportingFile("UnexpectedResponseException.mustache", clientPackageDir, "UnexpectedResponseException.cs"));
break;
default: // restsharp
addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir);
additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath);
supportingFiles.add(new SupportingFile("ConnectionException.mustache", clientPackageDir, "ConnectionException.cs")); if (ProcessUtils.hasOAuthMethods(openAPI)) {
supportingFiles.add(new SupportingFile("UnexpectedResponseException.mustache", clientPackageDir, "UnexpectedResponseException.cs")); supportingFiles.add(new SupportingFile("auth/OAuthAuthenticator.mustache", authPackageDir, "OAuthAuthenticator.cs"));
} else { //restsharp supportingFiles.add(new SupportingFile("auth/TokenResponse.mustache", authPackageDir, "TokenResponse.cs"));
addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir); supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authPackageDir, "OAuthFlow.cs"));
additionalProperties.put("apiDocPath", apiDocPath); }
additionalProperties.put("modelDocPath", modelDocPath); break;
if (ProcessUtils.hasOAuthMethods(openAPI)) {
supportingFiles.add(new SupportingFile("auth/OAuthAuthenticator.mustache", authPackageDir, "OAuthAuthenticator.cs"));
supportingFiles.add(new SupportingFile("auth/TokenResponse.mustache", authPackageDir, "TokenResponse.cs"));
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authPackageDir, "OAuthFlow.cs"));
}
} }
if (useDateOnly()) { if (useDateOnly()) {
@ -998,7 +1014,9 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
public void addSupportingFiles(final String clientPackageDir, final String packageFolder, public void addSupportingFiles(final String clientPackageDir, final String packageFolder,
final AtomicReference<Boolean> excludeTests, final String testPackageFolder, final String testPackageName, final String modelPackageDir, final String authPackageDir) { final AtomicReference<Boolean> excludeTests, final String testPackageFolder, final String testPackageName, final String modelPackageDir, final String authPackageDir) {
if (RESTSHARP.equals(getLibrary())) { // restsharp final String library = getLibrary();
if (RESTSHARP.equals(library)) { // restsharp
if (useIntForTimeout) { // option to fall back to int for Timeout using v7.9.0 template if (useIntForTimeout) { // option to fall back to int for Timeout using v7.9.0 template
supportingFiles.add(new SupportingFile("ApiClient.v790.mustache", clientPackageDir, "ApiClient.cs")); supportingFiles.add(new SupportingFile("ApiClient.v790.mustache", clientPackageDir, "ApiClient.cs"));
supportingFiles.add(new SupportingFile("IReadableConfiguration.v790.mustache", clientPackageDir, "IReadableConfiguration.cs")); supportingFiles.add(new SupportingFile("IReadableConfiguration.v790.mustache", clientPackageDir, "IReadableConfiguration.cs"));
@ -1046,7 +1064,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore")); supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
if (UNITY_WEB_REQUEST.equals(getLibrary())) { if (UNITY_WEB_REQUEST.equals(library)) {
supportingFiles.add(new SupportingFile("asmdef.mustache", packageFolder, packageName + ".asmdef")); supportingFiles.add(new SupportingFile("asmdef.mustache", packageFolder, packageName + ".asmdef"));
} else { } else {
supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln")); supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln"));
@ -1054,14 +1072,14 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
} }
if (Boolean.FALSE.equals(excludeTests.get())) { if (Boolean.FALSE.equals(excludeTests.get())) {
if (UNITY_WEB_REQUEST.equals(getLibrary())) { if (UNITY_WEB_REQUEST.equals(library)) {
supportingFiles.add(new SupportingFile("asmdef_test.mustache", testPackageFolder, testPackageName + ".asmdef")); supportingFiles.add(new SupportingFile("asmdef_test.mustache", testPackageFolder, testPackageName + ".asmdef"));
} else { } else {
supportingFiles.add(new SupportingFile("netcore_testproject.mustache", testPackageFolder, testPackageName + ".csproj")); supportingFiles.add(new SupportingFile("netcore_testproject.mustache", testPackageFolder, testPackageName + ".csproj"));
} }
} }
if (!UNITY_WEB_REQUEST.equals(getLibrary())) { if (!UNITY_WEB_REQUEST.equals(library)) {
supportingFiles.add(new SupportingFile("appveyor.mustache", "", "appveyor.yml")); supportingFiles.add(new SupportingFile("appveyor.mustache", "", "appveyor.yml"));
} }
supportingFiles.add(new SupportingFile("AbstractOpenAPISchema.mustache", modelPackageDir, "AbstractOpenAPISchema.cs")); supportingFiles.add(new SupportingFile("AbstractOpenAPISchema.mustache", modelPackageDir, "AbstractOpenAPISchema.cs"));
@ -1622,24 +1640,23 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
for (ModelMap mo : objs.getModels()) { for (ModelMap mo : objs.getModels()) {
CodegenModel cm = mo.getModel(); CodegenModel cm = mo.getModel();
if (cm.oneOf != null && !cm.oneOf.isEmpty() && cm.oneOf.contains("Null")) { if (cm.oneOf != null && !cm.oneOf.isEmpty() && cm.oneOf.remove("Null")) {
// if oneOf contains "null" type // if oneOf contains "null" type
cm.isNullable = true; cm.isNullable = true;
cm.oneOf.remove("Null");
} }
if (cm.anyOf != null && !cm.anyOf.isEmpty() && cm.anyOf.contains("Null")) { if (cm.anyOf != null && !cm.anyOf.isEmpty() && cm.anyOf.remove("Null")) {
// if anyOf contains "null" type // if anyOf contains "null" type
cm.isNullable = true; cm.isNullable = true;
cm.anyOf.remove("Null");
} }
if (cm.getComposedSchemas() != null && cm.getComposedSchemas().getOneOf() != null && !cm.getComposedSchemas().getOneOf().isEmpty()) { if (cm.getComposedSchemas() != null) {
cm.getComposedSchemas().getOneOf().removeIf(o -> o.dataType.equals("Null")); if (cm.getComposedSchemas().getOneOf() != null) {
} cm.getComposedSchemas().getOneOf().removeIf(o -> "Null".equals(o.dataType));
}
if (cm.getComposedSchemas() != null && cm.getComposedSchemas().getAnyOf() != null && !cm.getComposedSchemas().getAnyOf().isEmpty()) { if (cm.getComposedSchemas().getAnyOf() != null) {
cm.getComposedSchemas().getAnyOf().removeIf(o -> o.dataType.equals("Null")); cm.getComposedSchemas().getAnyOf().removeIf(o -> "Null".equals(o.dataType));
}
} }
for (CodegenProperty cp : cm.readWriteVars) { for (CodegenProperty cp : cm.readWriteVars) {
@ -1648,7 +1665,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
// see modules\openapi-generator\src\test\resources\3_0\allOf.yaml // see modules\openapi-generator\src\test\resources\3_0\allOf.yaml
// property boosterSeat will be in readWriteVars but not allVars // property boosterSeat will be in readWriteVars but not allVars
// the property is present in the model but gets removed at CodegenModel#removeDuplicatedProperty // the property is present in the model but gets removed at CodegenModel#removeDuplicatedProperty
if (Boolean.FALSE.equals(cm.allVars.stream().anyMatch(v -> v.baseName.equals(cp.baseName)))) { if (cm.allVars.stream().noneMatch(v -> v.baseName.equals(cp.baseName))) {
LOGGER.debug("Property " + cp.baseName + " was found in readWriteVars but not in allVars. Adding it back to allVars"); LOGGER.debug("Property " + cp.baseName + " was found in readWriteVars but not in allVars. Adding it back to allVars");
cm.allVars.add(cp); cm.allVars.add(cp);
} }
@ -1658,6 +1675,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
return objs; return objs;
} }
// https://github.com/OpenAPITools/openapi-generator/issues/15867 // https://github.com/OpenAPITools/openapi-generator/issues/15867
@Override @Override
protected void removePropertiesDeclaredInComposedTypes(Map<String, ModelsMap> objs, CodegenModel model, List<CodegenProperty> composedProperties) { protected void removePropertiesDeclaredInComposedTypes(Map<String, ModelsMap> objs, CodegenModel model, List<CodegenProperty> composedProperties) {
@ -1703,7 +1721,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
@Override @Override
protected boolean isValueType(CodegenProperty var) { protected boolean isValueType(CodegenProperty var) {
// this is temporary until x-csharp-value-type is removed // this is temporary until x-csharp-value-type is removed
return this.getLibrary().equals("generichost") return this.getLibrary().equals(GENERICHOST)
? super.isValueType(var) ? super.isValueType(var)
: this.getValueTypes().contains(var.dataType) || var.isEnum; : this.getValueTypes().contains(var.dataType) || var.isEnum;
} }