|
1 | 1 | package com.sap.cloud.sdk.datamodel.openapi.generator; |
2 | 2 |
|
3 | | -import static com.sap.cloud.sdk.datamodel.openapi.generator.GeneratorCustomProperties.FIX_REDUNDANT_IS_BOOLEAN_PREFIX; |
4 | | -import static com.sap.cloud.sdk.datamodel.openapi.generator.GeneratorCustomProperties.USE_FLOAT_ARRAYS; |
5 | | -import static com.sap.cloud.sdk.datamodel.openapi.generator.GeneratorCustomProperties.USE_ONE_OF_CREATORS; |
6 | | - |
7 | 3 | import java.nio.file.Path; |
8 | 4 | import java.nio.file.Paths; |
9 | 5 | import java.time.Year; |
10 | 6 | import java.util.HashMap; |
11 | | -import java.util.HashSet; |
12 | 7 | import java.util.List; |
13 | 8 | import java.util.Map; |
14 | | -import java.util.Set; |
15 | | -import java.util.regex.Pattern; |
16 | 9 |
|
17 | 10 | import javax.annotation.Nonnull; |
18 | | -import javax.annotation.Nullable; |
19 | 11 |
|
20 | 12 | import org.openapitools.codegen.ClientOptInput; |
21 | 13 | import org.openapitools.codegen.CodegenConstants; |
22 | | -import org.openapitools.codegen.CodegenModel; |
23 | | -import org.openapitools.codegen.CodegenOperation; |
24 | | -import org.openapitools.codegen.CodegenProperty; |
25 | 14 | import org.openapitools.codegen.config.GlobalSettings; |
26 | 15 | import org.openapitools.codegen.languages.JavaClientCodegen; |
27 | | -import org.openapitools.codegen.model.ModelMap; |
28 | | -import org.openapitools.codegen.model.OperationsMap; |
29 | 16 |
|
30 | 17 | import com.google.common.base.Strings; |
31 | 18 | import com.sap.cloud.sdk.datamodel.openapi.generator.model.ApiMaturity; |
32 | 19 | import com.sap.cloud.sdk.datamodel.openapi.generator.model.GenerationConfiguration; |
33 | 20 |
|
34 | 21 | import io.swagger.parser.OpenAPIParser; |
35 | 22 | import io.swagger.v3.oas.models.OpenAPI; |
36 | | -import io.swagger.v3.oas.models.media.Schema; |
37 | 23 | import io.swagger.v3.parser.core.models.AuthorizationValue; |
38 | 24 | import io.swagger.v3.parser.core.models.ParseOptions; |
39 | 25 | import lombok.extern.slf4j.Slf4j; |
@@ -82,131 +68,7 @@ static ClientOptInput convertGenerationConfiguration( |
82 | 68 |
|
83 | 69 | private static JavaClientCodegen createCodegenConfig( @Nonnull final GenerationConfiguration config ) |
84 | 70 | { |
85 | | - final var primitives = Set.of("String", "Integer", "Long", "Double", "Float", "Byte"); |
86 | | - final var doubleIs = Pattern.compile("^isIs[A-Z]").asPredicate(); |
87 | | - return new JavaClientCodegen() |
88 | | - { |
89 | | - @SuppressWarnings( { "rawtypes", "RedundantSuppression" } ) |
90 | | - @Override |
91 | | - protected void updatePropertyForArray( |
92 | | - @Nonnull final CodegenProperty property, |
93 | | - @Nonnull final CodegenProperty innerProperty ) |
94 | | - { |
95 | | - super.updatePropertyForArray(property, innerProperty); |
96 | | - |
97 | | - if( USE_FLOAT_ARRAYS.isEnabled(config) && innerProperty.isNumber && property.isArray ) { |
98 | | - property.datatypeWithEnum = "float[]"; |
99 | | - property.vendorExtensions.put("isPrimitiveArray", true); |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - @SuppressWarnings( { "rawtypes", "RedundantSuppression" } ) |
104 | | - @Override |
105 | | - @Nullable |
106 | | - public String toDefaultValue( @Nonnull final CodegenProperty cp, @Nonnull final Schema schema ) |
107 | | - { |
108 | | - if( USE_FLOAT_ARRAYS.isEnabled(config) && "float[]".equals(cp.datatypeWithEnum) ) { |
109 | | - return null; |
110 | | - } |
111 | | - return super.toDefaultValue(cp, schema); |
112 | | - } |
113 | | - |
114 | | - @Override |
115 | | - @Nullable |
116 | | - public String toBooleanGetter( @Nullable final String name ) |
117 | | - { |
118 | | - final String result = super.toBooleanGetter(name); |
119 | | - if( FIX_REDUNDANT_IS_BOOLEAN_PREFIX.isEnabled(config) && result != null && doubleIs.test(result) ) { |
120 | | - return "is" + result.substring(4); |
121 | | - } |
122 | | - return result; |
123 | | - } |
124 | | - |
125 | | - // Custom processor to inject "x-return-nullable" extension |
126 | | - @Override |
127 | | - @Nonnull |
128 | | - public OperationsMap postProcessOperationsWithModels( |
129 | | - @Nonnull final OperationsMap ops, |
130 | | - @Nonnull final List<ModelMap> allModels ) |
131 | | - { |
132 | | - for( final CodegenOperation op : ops.getOperations().getOperation() ) { |
133 | | - final var noContent = |
134 | | - op.isResponseOptional |
135 | | - || op.responses == null |
136 | | - || op.responses.stream().anyMatch(r -> "204".equals(r.code)); |
137 | | - op.vendorExtensions.put("x-return-nullable", op.returnType != null && noContent); |
138 | | - } |
139 | | - return super.postProcessOperationsWithModels(ops, allModels); |
140 | | - } |
141 | | - |
142 | | - @SuppressWarnings( { "rawtypes", "RedundantSuppression" } ) |
143 | | - @Override |
144 | | - protected void updateModelForComposedSchema( |
145 | | - @Nonnull final CodegenModel m, |
146 | | - @Nonnull final Schema schema, |
147 | | - @Nonnull final Map<String, Schema> allDefinitions ) |
148 | | - { |
149 | | - super.updateModelForComposedSchema(m, schema, allDefinitions); |
150 | | - |
151 | | - if( USE_ONE_OF_CREATORS.isEnabled(config) ) { |
152 | | - useCreatorsForInterfaceSubtypes(m); |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - /** |
157 | | - * Use JsonCreator for interface sub-types in case there are any primitives. |
158 | | - * |
159 | | - * @param m |
160 | | - * The model to update. |
161 | | - */ |
162 | | - private void useCreatorsForInterfaceSubtypes( @Nonnull final CodegenModel m ) |
163 | | - { |
164 | | - if( m.discriminator != null ) { |
165 | | - return; |
166 | | - } |
167 | | - boolean useCreators = false; |
168 | | - for( final Set<String> candidates : List.of(m.anyOf, m.oneOf) ) { |
169 | | - int nonPrimitives = 0; |
170 | | - final var candidatesSingle = new HashSet<String>(); |
171 | | - final var candidatesMultiple = new HashSet<String>(); |
172 | | - |
173 | | - for( final String candidate : candidates ) { |
174 | | - if( candidate.startsWith("List<") ) { |
175 | | - final var c1 = candidate.substring(5, candidate.length() - 1); |
176 | | - candidatesMultiple.add(c1); |
177 | | - useCreators = true; |
178 | | - } else { |
179 | | - candidatesSingle.add(candidate); |
180 | | - useCreators |= primitives.contains(candidate); |
181 | | - if( !primitives.contains(candidate) ) { |
182 | | - nonPrimitives++; |
183 | | - } |
184 | | - } |
185 | | - } |
186 | | - if( useCreators ) { |
187 | | - if( nonPrimitives > 1 ) { |
188 | | - final var msg = |
189 | | - "Generating interface with mixed multiple non-primitive and primitive sub-types: {}. Deserialization may not work."; |
190 | | - log.warn(msg, m.name); |
191 | | - } |
192 | | - candidates.clear(); |
193 | | - final var monads = Map.of("single", candidatesSingle, "multiple", candidatesMultiple); |
194 | | - m.vendorExtensions.put("x-monads", monads); |
195 | | - m.vendorExtensions.put("x-is-one-of-interface", true); // enforce template usage |
196 | | - } |
197 | | - } |
198 | | - } |
199 | | - |
200 | | - @SuppressWarnings( { "rawtypes", "RedundantSuppression" } ) |
201 | | - @Override |
202 | | - protected void updateModelForObject( @Nonnull final CodegenModel m, @Nonnull final Schema schema ) |
203 | | - { |
204 | | - // Disable additional attributes to prevent model classes from extending "HashMap" |
205 | | - // SAP Cloud SDK offers custom field APIs to handle additional attributes already |
206 | | - schema.setAdditionalProperties(Boolean.FALSE); |
207 | | - super.updateModelForObject(m, schema); |
208 | | - } |
209 | | - }; |
| 71 | + return new CustomJavaClientCodegen(config); |
210 | 72 | } |
211 | 73 |
|
212 | 74 | private static void setGlobalSettings( @Nonnull final GenerationConfiguration configuration ) |
|
0 commit comments