@@ -698,6 +698,8 @@ public void processOpts() {
698
698
699
699
super .processOpts ();
700
700
701
+ final String library = getLibrary ();
702
+
701
703
/*
702
704
* NOTE: When supporting boolean additionalProperties, you should read the value and write it back as a boolean.
703
705
* This avoids oddities where additionalProperties contains "false" rather than false, which will cause the
@@ -753,27 +755,37 @@ public void processOpts() {
753
755
setModelPackage ("Model" );
754
756
}
755
757
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 ();
770
781
} 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." );
772
784
}
773
785
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 <>();
777
789
778
790
if (inputFramework .contains (";" )) {
779
791
// multiple target framework
@@ -792,7 +804,7 @@ public void processOpts() {
792
804
strategyMatched = true ;
793
805
}
794
806
795
- if (frameworkStrategy != FrameworkStrategy .NETSTANDARD_2_0 && "restsharp" .equals (getLibrary () )) {
807
+ if (frameworkStrategy != FrameworkStrategy .NETSTANDARD_2_0 && RESTSHARP .equals (library )) {
796
808
LOGGER .warn ("If using built-in templates, RestSharp only supports netstandard 2.0 or later." );
797
809
}
798
810
}
@@ -876,39 +888,43 @@ public void processOpts() {
876
888
apiTestTemplateFiles .put ("api_test.mustache" , ".cs" );
877
889
}
878
890
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 ;
912
928
}
913
929
914
930
if (useDateOnly ()) {
@@ -998,7 +1014,9 @@ public CodegenOperation fromOperation(String path,
998
1014
999
1015
public void addSupportingFiles (final String clientPackageDir , final String packageFolder ,
1000
1016
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
1002
1020
if (useIntForTimeout ) { // option to fall back to int for Timeout using v7.9.0 template
1003
1021
supportingFiles .add (new SupportingFile ("ApiClient.v790.mustache" , clientPackageDir , "ApiClient.cs" ));
1004
1022
supportingFiles .add (new SupportingFile ("IReadableConfiguration.v790.mustache" , clientPackageDir , "IReadableConfiguration.cs" ));
@@ -1046,22 +1064,22 @@ public void addSupportingFiles(final String clientPackageDir, final String packa
1046
1064
supportingFiles .add (new SupportingFile ("git_push.sh.mustache" , "" , "git_push.sh" ));
1047
1065
supportingFiles .add (new SupportingFile ("gitignore.mustache" , "" , ".gitignore" ));
1048
1066
1049
- if (UNITY_WEB_REQUEST .equals (getLibrary () )) {
1067
+ if (UNITY_WEB_REQUEST .equals (library )) {
1050
1068
supportingFiles .add (new SupportingFile ("asmdef.mustache" , packageFolder , packageName + ".asmdef" ));
1051
1069
} else {
1052
1070
supportingFiles .add (new SupportingFile ("Solution.mustache" , "" , packageName + ".sln" ));
1053
1071
supportingFiles .add (new SupportingFile ("netcore_project.mustache" , packageFolder , packageName + ".csproj" ));
1054
1072
}
1055
1073
1056
1074
if (Boolean .FALSE .equals (excludeTests .get ())) {
1057
- if (UNITY_WEB_REQUEST .equals (getLibrary () )) {
1075
+ if (UNITY_WEB_REQUEST .equals (library )) {
1058
1076
supportingFiles .add (new SupportingFile ("asmdef_test.mustache" , testPackageFolder , testPackageName + ".asmdef" ));
1059
1077
} else {
1060
1078
supportingFiles .add (new SupportingFile ("netcore_testproject.mustache" , testPackageFolder , testPackageName + ".csproj" ));
1061
1079
}
1062
1080
}
1063
1081
1064
- if (!UNITY_WEB_REQUEST .equals (getLibrary () )) {
1082
+ if (!UNITY_WEB_REQUEST .equals (library )) {
1065
1083
supportingFiles .add (new SupportingFile ("appveyor.mustache" , "" , "appveyor.yml" ));
1066
1084
}
1067
1085
supportingFiles .add (new SupportingFile ("AbstractOpenAPISchema.mustache" , modelPackageDir , "AbstractOpenAPISchema.cs" ));
@@ -1622,24 +1640,23 @@ public ModelsMap postProcessModels(ModelsMap objs) {
1622
1640
for (ModelMap mo : objs .getModels ()) {
1623
1641
CodegenModel cm = mo .getModel ();
1624
1642
1625
- if (cm .oneOf != null && !cm .oneOf .isEmpty () && cm .oneOf .contains ("Null" )) {
1643
+ if (cm .oneOf != null && !cm .oneOf .isEmpty () && cm .oneOf .remove ("Null" )) {
1626
1644
// if oneOf contains "null" type
1627
1645
cm .isNullable = true ;
1628
- cm .oneOf .remove ("Null" );
1629
1646
}
1630
1647
1631
- if (cm .anyOf != null && !cm .anyOf .isEmpty () && cm .anyOf .contains ("Null" )) {
1648
+ if (cm .anyOf != null && !cm .anyOf .isEmpty () && cm .anyOf .remove ("Null" )) {
1632
1649
// if anyOf contains "null" type
1633
1650
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" ));
1639
1651
}
1640
1652
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
+ }
1643
1660
}
1644
1661
1645
1662
for (CodegenProperty cp : cm .readWriteVars ) {
@@ -1648,7 +1665,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
1648
1665
// see modules\openapi-generator\src\test\resources\3_0\allOf.yaml
1649
1666
// property boosterSeat will be in readWriteVars but not allVars
1650
1667
// 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 ))) {
1652
1669
LOGGER .debug ("Property " + cp .baseName + " was found in readWriteVars but not in allVars. Adding it back to allVars" );
1653
1670
cm .allVars .add (cp );
1654
1671
}
@@ -1658,6 +1675,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
1658
1675
return objs ;
1659
1676
}
1660
1677
1678
+
1661
1679
// https://github.com/OpenAPITools/openapi-generator/issues/15867
1662
1680
@ Override
1663
1681
protected void removePropertiesDeclaredInComposedTypes (Map <String , ModelsMap > objs , CodegenModel model , List <CodegenProperty > composedProperties ) {
@@ -1703,7 +1721,7 @@ protected void removePropertiesDeclaredInComposedTypes(Map<String, ModelsMap> ob
1703
1721
@ Override
1704
1722
protected boolean isValueType (CodegenProperty var ) {
1705
1723
// this is temporary until x-csharp-value-type is removed
1706
- return this .getLibrary ().equals ("generichost" )
1724
+ return this .getLibrary ().equals (GENERICHOST )
1707
1725
? super .isValueType (var )
1708
1726
: this .getValueTypes ().contains (var .dataType ) || var .isEnum ;
1709
1727
}
0 commit comments