|
52 | 52 | import org.openapijsonschematools.codegen.generators.openapimodels.CodegenServer;
|
53 | 53 | import org.openapijsonschematools.codegen.generators.openapimodels.EnumInfo;
|
54 | 54 | import org.openapijsonschematools.codegen.generators.openapimodels.EnumValue;
|
| 55 | +import org.openapijsonschematools.codegen.generators.openapimodels.JsonPathPieceMethod; |
55 | 56 | import org.openapijsonschematools.codegen.generators.openapimodels.MapBuilder;
|
56 | 57 | import org.openapijsonschematools.codegen.templating.HandlebarsEngineAdapter;
|
57 | 58 | import org.openapijsonschematools.codegen.templating.SupportingFile;
|
@@ -2413,16 +2414,16 @@ private void addDoubleEnum(LinkedHashMap<String, LinkedHashMap<EnumValue, String
|
2413 | 2414 | }
|
2414 | 2415 | }
|
2415 | 2416 |
|
2416 |
| - protected List<MapBuilder<?>> getOperationBuilders(String jsonSchema, CodegenRequestBody requestBody, CodegenParametersInfo parametersInfo, CodegenList<CodegenServer> servers, CodegenList<CodegenSecurityRequirementObject> security) { |
| 2417 | + protected List<MapBuilder<?>> getOperationBuilders(String jsonPath, CodegenRequestBody requestBody, CodegenParametersInfo parametersInfo, CodegenList<CodegenServer> servers, CodegenList<CodegenSecurityRequirementObject> security) { |
2417 | 2418 | if (requestBody == null && parametersInfo == null && servers == null && security == null) {
|
2418 | 2419 | return null;
|
2419 | 2420 | }
|
2420 | 2421 | int qtyBuilders = 1;
|
2421 | 2422 | int reqPropsSize = 0;
|
2422 | 2423 | boolean requestBodyExists = requestBody != null;
|
2423 | 2424 | boolean parametersExist = parametersInfo != null;
|
2424 |
| - List<Object> requiredProperties = new ArrayList<>(); |
2425 |
| - List<Object> optionalProperties = new ArrayList<>(); |
| 2425 | + List<JsonPathPieceMethod> requiredProperties = new ArrayList<>(); |
| 2426 | + List<JsonPathPieceMethod> optionalProperties = new ArrayList<>(); |
2426 | 2427 | if (requestBodyExists) {
|
2427 | 2428 | if (Boolean.TRUE.equals(requestBody.getSelfOrDeepestRef().required)) {
|
2428 | 2429 | qtyBuilders += 1;
|
@@ -2476,8 +2477,60 @@ protected List<MapBuilder<?>> getOperationBuilders(String jsonSchema, CodegenReq
|
2476 | 2477 | if (security != null) {
|
2477 | 2478 | optionalProperties.add(security);
|
2478 | 2479 | }
|
2479 |
| - // todo pass in and use path/root servers + security here if they are used |
2480 |
| - return List.of(); |
| 2480 | + |
| 2481 | + Map<String, MapBuilder<Object>> bitStrToBuilder = new HashMap<>(); |
| 2482 | + MapBuilder<Object> lastBuilder = null; |
| 2483 | + // builders are built last to first, last builder has build method |
| 2484 | + String[] pathPieces = jsonPath.split("/"); |
| 2485 | + CodegenKey operationKey = getKey(pathPieces[pathPieces.length-1], "misc", jsonPath); |
| 2486 | + String builderName = operationKey.pascalCase; |
| 2487 | + List<MapBuilder<?>> builders = new ArrayList<>(); |
| 2488 | + for (int i=0; i < qtyBuilders; i++) { |
| 2489 | + String bitStr = ""; |
| 2490 | + if (reqPropsSize != 0) { |
| 2491 | + bitStr = String.format("%"+reqPropsSize+"s", Integer.toBinaryString(i)).replace(' ', '0'); |
| 2492 | + } |
| 2493 | + CodegenKey builderClassName; |
| 2494 | + if (i == qtyBuilders - 1) { |
| 2495 | + // first invoked builder has the simplest name with no bitStr |
| 2496 | + builderClassName = getKey(builderName + "RequestBuilder", "schemas", jsonPath); |
| 2497 | + } else { |
| 2498 | + builderClassName = getKey(builderName + bitStr + "RequestBuilder", "schemas", jsonPath); |
| 2499 | + } |
| 2500 | + MapBuilder<Object> builder; |
| 2501 | + if (i == 0) { |
| 2502 | + builder = new MapBuilder<>(builderClassName, new LinkedHashMap<>()); |
| 2503 | + lastBuilder = builder; |
| 2504 | + } else { |
| 2505 | + LinkedHashMap<CodegenKey, MapBuilder.BuilderPropertyPair<Object>> keyToBuilder = new LinkedHashMap<>(); |
| 2506 | + for (int c=0; c < reqPropsSize; c++) { |
| 2507 | + if (bitStr.charAt(c) == '1') { |
| 2508 | + StringBuilder nextBuilderBitStr = new StringBuilder(bitStr); |
| 2509 | + nextBuilderBitStr.setCharAt(c, '0'); |
| 2510 | + CodegenKey key = requiredProperties.get(c).jsonPathPiece(); |
| 2511 | + if (key == null) { |
| 2512 | + throw new RuntimeException("key must exist at c="+c); |
| 2513 | + } |
| 2514 | + MapBuilder<Object> nextBuilder = bitStrToBuilder.get(nextBuilderBitStr.toString()); |
| 2515 | + if (nextBuilder == null) { |
| 2516 | + throw new RuntimeException("Next builder must exist for bitStr="+ nextBuilderBitStr); |
| 2517 | + } |
| 2518 | + var pair = new MapBuilder.BuilderPropertyPair<>(nextBuilder, requiredProperties.get(c)); |
| 2519 | + keyToBuilder.put(key, pair); |
| 2520 | + } |
| 2521 | + } |
| 2522 | + builder = new MapBuilder<>(builderClassName, keyToBuilder); |
| 2523 | + } |
| 2524 | + bitStrToBuilder.put(bitStr, builder); |
| 2525 | + builders.add(builder); |
| 2526 | + } |
| 2527 | + if (!optionalProperties.isEmpty()) { |
| 2528 | + for (JsonPathPieceMethod property: optionalProperties) { |
| 2529 | + var pair = new MapBuilder.BuilderPropertyPair<>(lastBuilder, property); |
| 2530 | + lastBuilder.keyToBuilder.put(property.jsonPathPiece(), pair); |
| 2531 | + } |
| 2532 | + } |
| 2533 | + return builders; |
2481 | 2534 | }
|
2482 | 2535 |
|
2483 | 2536 | protected List<MapBuilder<CodegenSchema>> getMapBuilders(CodegenSchema schema, String currentJsonPath, String sourceJsonPath) {
|
|
0 commit comments