Esempio n. 1
0
 private Set<String> getModelRef(Model model) {
   if (model instanceof ArrayModel && ((ArrayModel) model).getItems() != null) {
     return new HashSet<String>(Arrays.asList(getPropertyRef(((ArrayModel) model).getItems())));
   } else if (model instanceof ComposedModel && ((ComposedModel) model).getAllOf() != null) {
     Set<String> refs = new LinkedHashSet<String>();
     ComposedModel cModel = (ComposedModel) model;
     for (Model ref : cModel.getAllOf()) {
       if (ref instanceof RefModel) {
         refs.add(((RefModel) ref).getSimpleRef());
       }
     }
     return refs;
   } else if (model instanceof RefModel) {
     return new HashSet<String>(Arrays.asList(((RefModel) model).getSimpleRef()));
   }
   return null;
 }
  public Model allOfModel(ObjectNode node, String location, ParseResult result) {
    JsonNode sub = node.get("$ref");
    JsonNode allOf = node.get("allOf");

    if (sub != null) {
      if (sub.getNodeType().equals(JsonNodeType.OBJECT)) {
        return refModel((ObjectNode) sub, location, result);
      } else {
        result.invalidType(location, "$ref", "object", sub);
        return null;
      }
    } else if (allOf != null) {
      ComposedModel model = null;
      // we only support one parent, no multiple inheritance or composition
      if (allOf.getNodeType().equals(JsonNodeType.ARRAY)) {
        model = new ComposedModel();
        int pos = 0;
        for (JsonNode part : allOf) {
          if (part.getNodeType().equals(JsonNodeType.OBJECT)) {
            Model segment = definition((ObjectNode) part, location, result);
            if (segment != null) {
              model.getAllOf().add(segment);
            }
          } else {
            result.invalidType(location, "allOf[" + pos + "]", "object", part);
          }
          pos++;
        }

        List<Model> allComponents = model.getAllOf();
        if (allComponents.size() >= 1) {
          model.setParent(allComponents.get(0));
          if (allComponents.size() >= 2) {
            model.setChild(allComponents.get(allComponents.size() - 1));
            List<RefModel> interfaces = new ArrayList<RefModel>();
            int size = allComponents.size();
            for (Model m : allComponents.subList(1, size - 1)) {
              if (m instanceof RefModel) {
                RefModel ref = (RefModel) m;
                interfaces.add(ref);
              }
            }
            model.setInterfaces(interfaces);
          } else {
            model.setChild(new ModelImpl());
          }
        }
        return model;
      } else {
        result.invalidType(location, "allOf", "array", allOf);
      }

      return model;
    }
    return null;
  }