|
1 | 1 | package com.wordnik.swagger.codegen;
|
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collection; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Iterator; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.regex.Matcher; |
| 15 | +import java.util.regex.Pattern; |
| 16 | + |
| 17 | +import javax.annotation.Nullable; |
| 18 | + |
| 19 | +import org.apache.commons.lang.StringUtils; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
3 | 23 | import com.google.common.base.Function;
|
4 | 24 | import com.google.common.collect.Lists;
|
5 |
| - |
6 | 25 | import com.wordnik.swagger.codegen.examples.ExampleGenerator;
|
7 | 26 | import com.wordnik.swagger.models.ArrayModel;
|
| 27 | +import com.wordnik.swagger.models.ComposedModel; |
8 | 28 | import com.wordnik.swagger.models.Model;
|
9 | 29 | import com.wordnik.swagger.models.ModelImpl;
|
10 | 30 | import com.wordnik.swagger.models.Operation;
|
|
40 | 60 | import com.wordnik.swagger.models.properties.StringProperty;
|
41 | 61 | import com.wordnik.swagger.util.Json;
|
42 | 62 |
|
43 |
| -import javax.annotation.Nullable; |
44 |
| -import java.io.File; |
45 |
| -import java.util.ArrayList; |
46 |
| -import java.util.Arrays; |
47 |
| -import java.util.HashMap; |
48 |
| -import java.util.HashSet; |
49 |
| -import java.util.Iterator; |
50 |
| -import java.util.List; |
51 |
| -import java.util.Map; |
52 |
| -import java.util.Set; |
53 |
| -import java.util.regex.Matcher; |
54 |
| -import java.util.regex.Pattern; |
55 |
| - |
56 |
| -import org.apache.commons.lang.StringUtils; |
57 |
| - |
58 |
| -import org.slf4j.Logger; |
59 |
| -import org.slf4j.LoggerFactory; |
60 |
| - |
61 | 63 |
|
62 | 64 | public class DefaultCodegen {
|
63 |
| - Logger LOGGER = LoggerFactory.getLogger(DefaultCodegen.class); |
| 65 | + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultCodegen.class); |
64 | 66 |
|
65 | 67 | protected String outputFolder = "";
|
66 | 68 | protected Set<String> defaultIncludes = new HashSet<String>();
|
@@ -473,100 +475,28 @@ public CodegenModel fromModel(String name, Model model) {
|
473 | 475 | m.classVarName = toVarName(name);
|
474 | 476 | m.modelJson = Json.pretty(model);
|
475 | 477 | m.externalDocs = model.getExternalDocs();
|
476 |
| - int count = 0; |
477 | 478 | if(model instanceof ArrayModel) {
|
478 | 479 | ArrayModel am = (ArrayModel) model;
|
479 | 480 | ArrayProperty arrayProperty = new ArrayProperty(am.getItems());
|
480 |
| - CodegenProperty cp = fromProperty(name, arrayProperty); |
481 |
| - if(cp.complexType != null && !defaultIncludes.contains(cp.complexType)) |
482 |
| - m.imports.add(cp.complexType); |
483 |
| - m.parent = toInstantiationType(arrayProperty); |
484 |
| - String containerType = cp.containerType; |
485 |
| - if(instantiationTypes.containsKey(containerType)) |
486 |
| - m.imports.add(instantiationTypes.get(containerType)); |
487 |
| - if(typeMapping.containsKey(containerType)) { |
488 |
| - containerType = typeMapping.get(containerType); |
489 |
| - cp.containerType = containerType; |
490 |
| - m.imports.add(containerType); |
491 |
| - } |
| 481 | + addParentContainer(m, name, arrayProperty); |
492 | 482 | }
|
493 | 483 | else if (model instanceof RefModel) {
|
494 | 484 | // TODO
|
495 |
| - } |
496 |
| - else { |
| 485 | + } else if (model instanceof ComposedModel) { |
| 486 | + final ComposedModel composed = (ComposedModel) model; |
| 487 | + final RefModel parent = (RefModel) composed.getParent(); |
| 488 | + final String parentModel = toModelName(parent.getSimpleRef()); |
| 489 | + m.parent = parentModel; |
| 490 | + addImport(m, parentModel); |
| 491 | + final ModelImpl child = (ModelImpl) composed.getChild(); |
| 492 | + addVars(m, child.getProperties(), child.getRequired()); |
| 493 | + } else { |
497 | 494 | ModelImpl impl = (ModelImpl) model;
|
498 | 495 | if(impl.getAdditionalProperties() != null) {
|
499 | 496 | MapProperty mapProperty = new MapProperty(impl.getAdditionalProperties());
|
500 |
| - CodegenProperty cp = fromProperty(name, mapProperty); |
501 |
| - if(cp.complexType != null && !defaultIncludes.contains(cp.complexType)) |
502 |
| - m.imports.add(cp.complexType); |
503 |
| - m.parent = toInstantiationType(mapProperty); |
504 |
| - String containerType = cp.containerType; |
505 |
| - if(instantiationTypes.containsKey(containerType)) |
506 |
| - m.imports.add(instantiationTypes.get(containerType)); |
507 |
| - if(typeMapping.containsKey(containerType)) { |
508 |
| - containerType = typeMapping.get(containerType); |
509 |
| - cp.containerType = containerType; |
510 |
| - m.imports.add(containerType); |
511 |
| - } |
512 |
| - } |
513 |
| - if(impl.getProperties() != null && impl.getProperties().size() > 0) { |
514 |
| - m.hasVars = true; |
515 |
| - m.hasEnums = false; |
516 |
| - for(String key: impl.getProperties().keySet()) { |
517 |
| - Property prop = impl.getProperties().get(key); |
518 |
| - |
519 |
| - if(prop == null) { |
520 |
| - LOGGER.warn("null property for " + key); |
521 |
| - } |
522 |
| - else { |
523 |
| - CodegenProperty cp; |
524 |
| - try{ |
525 |
| - cp = fromProperty(key, prop); |
526 |
| - } |
527 |
| - catch(Exception e) { |
528 |
| - System.out.println("failed to process model " + name); |
529 |
| - throw new RuntimeException(e); |
530 |
| - } |
531 |
| - cp.required = null; |
532 |
| - if(impl.getRequired() != null) { |
533 |
| - for(String req : impl.getRequired()) { |
534 |
| - if(key.equals(req)) |
535 |
| - cp.required = true; |
536 |
| - } |
537 |
| - } |
538 |
| - if(cp.complexType != null && !defaultIncludes.contains(cp.complexType)) { |
539 |
| - m.imports.add(cp.complexType); |
540 |
| - } |
541 |
| - m.vars.add(cp); |
542 |
| - count += 1; |
543 |
| - if (cp.isEnum) |
544 |
| - m.hasEnums = true; |
545 |
| - if(count != impl.getProperties().keySet().size()) |
546 |
| - cp.hasMore = new Boolean(true); |
547 |
| - if(cp.isContainer != null) { |
548 |
| - String arrayImport = typeMapping.get("array"); |
549 |
| - if(arrayImport != null && |
550 |
| - !languageSpecificPrimitives.contains(arrayImport) && |
551 |
| - !defaultIncludes.contains(arrayImport)) |
552 |
| - m.imports.add(arrayImport); |
553 |
| - } |
554 |
| - |
555 |
| - if(cp.complexType != null && |
556 |
| - !languageSpecificPrimitives.contains(cp.complexType) && |
557 |
| - !defaultIncludes.contains(cp.complexType)) |
558 |
| - m.imports.add(cp.complexType); |
559 |
| - |
560 |
| - if(cp.baseType != null && |
561 |
| - !languageSpecificPrimitives.contains(cp.baseType) && |
562 |
| - !defaultIncludes.contains(cp.baseType)) |
563 |
| - m.imports.add(cp.baseType); |
564 |
| - } |
565 |
| - } |
566 |
| - } |
567 |
| - else { |
568 |
| - m.emptyVars = true; |
| 497 | + addParentContainer(m, name, mapProperty); |
569 | 498 | }
|
| 499 | + addVars(m, impl.getProperties(), impl.getRequired()); |
570 | 500 | }
|
571 | 501 | return m;
|
572 | 502 | }
|
@@ -1149,6 +1079,62 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
|
1149 | 1079 | co.baseName = tag;
|
1150 | 1080 | }
|
1151 | 1081 |
|
| 1082 | + private void addParentContainer(CodegenModel m, String name, Property property) { |
| 1083 | + final CodegenProperty tmp = fromProperty(name, property); |
| 1084 | + addImport(m, tmp.complexType); |
| 1085 | + m.parent = toInstantiationType(property); |
| 1086 | + final String containerType = tmp.containerType; |
| 1087 | + final String instantiationType = instantiationTypes.get(containerType); |
| 1088 | + if (instantiationType != null) { |
| 1089 | + addImport(m, instantiationType); |
| 1090 | + } |
| 1091 | + final String mappedType = typeMapping.get(containerType); |
| 1092 | + if (mappedType != null) { |
| 1093 | + addImport(m, mappedType); |
| 1094 | + } |
| 1095 | + } |
| 1096 | + |
| 1097 | + private void addImport(CodegenModel m, String type) { |
| 1098 | + if (type != null && !languageSpecificPrimitives.contains(type) && !defaultIncludes.contains(type)) { |
| 1099 | + m.imports.add(type); |
| 1100 | + } |
| 1101 | + } |
| 1102 | + |
| 1103 | + private void addVars(CodegenModel m, Map<String, Property> properties, Collection<String> required) { |
| 1104 | + if (properties != null && properties.size() > 0) { |
| 1105 | + m.hasVars = true; |
| 1106 | + m.hasEnums = false; |
| 1107 | + final int totalCount = properties.size(); |
| 1108 | + final Set<String> mandatory = required == null ? Collections.<String> emptySet() : new HashSet<String>(required); |
| 1109 | + int count = 0; |
| 1110 | + for (Map.Entry<String, Property> entry : properties.entrySet()) { |
| 1111 | + final String key = entry.getKey(); |
| 1112 | + final Property prop = entry.getValue(); |
| 1113 | + |
| 1114 | + if (prop == null) { |
| 1115 | + LOGGER.warn("null property for " + key); |
| 1116 | + } else { |
| 1117 | + final CodegenProperty cp = fromProperty(key, prop); |
| 1118 | + cp.required = mandatory.contains(key) ? true : null; |
| 1119 | + if (cp.isEnum) { |
| 1120 | + m.hasEnums = true; |
| 1121 | + } |
| 1122 | + count += 1; |
| 1123 | + if (count != totalCount) |
| 1124 | + cp.hasMore = true; |
| 1125 | + if (cp.isContainer != null) { |
| 1126 | + addImport(m, typeMapping.get("array")); |
| 1127 | + } |
| 1128 | + addImport(m, cp.baseType); |
| 1129 | + addImport(m, cp.complexType); |
| 1130 | + m.vars.add(cp); |
| 1131 | + } |
| 1132 | + } |
| 1133 | + } else { |
| 1134 | + m.emptyVars = true; |
| 1135 | + } |
| 1136 | + } |
| 1137 | + |
1152 | 1138 | /* underscore and camelize are copied from Twitter elephant bird
|
1153 | 1139 | * https://github.com/twitter/elephant-bird/blob/master/core/src/main/java/com/twitter/elephantbird/util/Strings.java
|
1154 | 1140 | */
|
|
0 commit comments