@Test
  public void resolveInlineBodyParameter() throws Exception {
    Swagger swagger = new Swagger();

    swagger.path(
        "/hello",
        new Path()
            .get(
                new Operation()
                    .parameter(
                        new BodyParameter()
                            .name("body")
                            .schema(
                                new ModelImpl()
                                    .property(
                                        "address",
                                        new ObjectProperty()
                                            .property("street", new StringProperty()))
                                    .property("name", new StringProperty())))));

    new InlineModelResolver().flatten(swagger);

    Operation operation = swagger.getPaths().get("/hello").getGet();
    BodyParameter bp = (BodyParameter) operation.getParameters().get(0);
    assertTrue(bp.getSchema() instanceof RefModel);

    Model body = swagger.getDefinitions().get("body");
    assertTrue(body instanceof ModelImpl);

    ModelImpl impl = (ModelImpl) body;
    assertNotNull(impl.getProperties().get("address"));
  }
  @Test
  public void resolveInlineArrayBodyParameter() throws Exception {
    Swagger swagger = new Swagger();

    swagger.path(
        "/hello",
        new Path()
            .get(
                new Operation()
                    .parameter(
                        new BodyParameter()
                            .name("body")
                            .schema(
                                new ArrayModel()
                                    .items(
                                        new ObjectProperty()
                                            .property(
                                                "address",
                                                new ObjectProperty()
                                                    .property("street", new StringProperty())))))));

    new InlineModelResolver().flatten(swagger);

    Parameter param = swagger.getPaths().get("/hello").getGet().getParameters().get(0);
    assertTrue(param instanceof BodyParameter);

    BodyParameter bp = (BodyParameter) param;
    Model schema = bp.getSchema();

    assertTrue(schema instanceof ArrayModel);

    ArrayModel am = (ArrayModel) schema;
    Property inner = am.getItems();

    ObjectProperty op = (ObjectProperty) inner;
    Property name = op.getProperties().get("address");
    assertTrue(name instanceof RefProperty);

    Model model = swagger.getDefinitions().get("hello_address");
    assertNotNull(model);
  }
  @Test
  public void notResolveNonModelBodyParameter() throws Exception {
    Swagger swagger = new Swagger();

    swagger.path(
        "/hello",
        new Path()
            .get(
                new Operation()
                    .parameter(
                        new BodyParameter()
                            .name("body")
                            .schema(new ModelImpl().type("string").format("binary")))));

    new InlineModelResolver().flatten(swagger);

    Operation operation = swagger.getPaths().get("/hello").getGet();
    BodyParameter bp = (BodyParameter) operation.getParameters().get(0);
    assertTrue(bp.getSchema() instanceof ModelImpl);
    ModelImpl m = (ModelImpl) bp.getSchema();
    assertEquals("string", m.getType());
    assertEquals("binary", m.getFormat());
  }
Exemple #4
0
  private Swagger removeBrokenReferenceDefinitions(Swagger swagger) {

    if (swagger.getDefinitions() == null || swagger.getDefinitions().isEmpty()) return swagger;

    Set<String> referencedDefinitions = new TreeSet<String>();

    if (swagger.getResponses() != null) {
      for (Response response : swagger.getResponses().values()) {
        String propertyRef = getPropertyRef(response.getSchema());
        if (propertyRef != null) {
          referencedDefinitions.add(propertyRef);
        }
      }
    }
    if (swagger.getParameters() != null) {
      for (Parameter p : swagger.getParameters().values()) {
        if (p instanceof BodyParameter) {
          BodyParameter bp = (BodyParameter) p;
          Set<String> modelRef = getModelRef(bp.getSchema());
          if (modelRef != null) {
            referencedDefinitions.addAll(modelRef);
          }
        }
      }
    }
    if (swagger.getPaths() != null) {
      for (Path path : swagger.getPaths().values()) {
        if (path.getParameters() != null) {
          for (Parameter p : path.getParameters()) {
            if (p instanceof BodyParameter) {
              BodyParameter bp = (BodyParameter) p;
              Set<String> modelRef = getModelRef(bp.getSchema());
              if (modelRef != null) {
                referencedDefinitions.addAll(modelRef);
              }
            }
          }
        }
        if (path.getOperations() != null) {
          for (Operation op : path.getOperations()) {
            if (op.getResponses() != null) {
              for (Response response : op.getResponses().values()) {
                String propertyRef = getPropertyRef(response.getSchema());
                if (propertyRef != null) {
                  referencedDefinitions.add(propertyRef);
                }
              }
            }
            if (op.getParameters() != null) {
              for (Parameter p : op.getParameters()) {
                if (p instanceof BodyParameter) {
                  BodyParameter bp = (BodyParameter) p;
                  Set<String> modelRef = getModelRef(bp.getSchema());
                  if (modelRef != null) {
                    referencedDefinitions.addAll(modelRef);
                  }
                }
              }
            }
          }
        }
      }
    }

    if (swagger.getDefinitions() != null) {
      Set<String> nestedReferencedDefinitions = new TreeSet<String>();
      for (String ref : referencedDefinitions) {
        locateReferencedDefinitions(ref, nestedReferencedDefinitions, swagger);
      }
      referencedDefinitions.addAll(nestedReferencedDefinitions);
      swagger.getDefinitions().keySet().retainAll(referencedDefinitions);
    }

    return swagger;
  }
  private void parse(
      Swagger swagger, RestDefinition rest, String camelContextId, ClassResolver classResolver) {
    List<VerbDefinition> verbs = new ArrayList<>(rest.getVerbs());
    // must sort the verbs by uri so we group them together when an uri has multiple operations
    Collections.sort(verbs, new VerbOrdering());

    // we need to group the operations within the same tag, so use the path as default if not
    // configured
    String pathAsTag =
        rest.getTag() != null ? rest.getTag() : FileUtil.stripLeadingSeparator(rest.getPath());
    String summary = rest.getDescriptionText();

    if (ObjectHelper.isNotEmpty(pathAsTag)) {
      // add rest as tag
      Tag tag = new Tag();
      tag.description(summary);
      tag.name(pathAsTag);
      swagger.addTag(tag);
    }

    // gather all types in use
    Set<String> types = new LinkedHashSet<>();
    for (VerbDefinition verb : verbs) {
      String type = verb.getType();
      if (ObjectHelper.isNotEmpty(type)) {
        if (type.endsWith("[]")) {
          type = type.substring(0, type.length() - 2);
        }
        types.add(type);
      }
      type = verb.getOutType();
      if (ObjectHelper.isNotEmpty(type)) {
        if (type.endsWith("[]")) {
          type = type.substring(0, type.length() - 2);
        }
        types.add(type);
      }
      // there can also be types in response messages
      if (verb.getResponseMsgs() != null) {
        for (RestOperationResponseMsgDefinition def : verb.getResponseMsgs()) {
          type = def.getResponseModel();
          if (ObjectHelper.isNotEmpty(type)) {
            if (type.endsWith("[]")) {
              type = type.substring(0, type.length() - 2);
            }
            types.add(type);
          }
        }
      }
    }

    // use annotation scanner to find models (annotated classes)
    for (String type : types) {
      Class<?> clazz = classResolver.resolveClass(type);
      appendModels(clazz, swagger);
    }

    // used during gathering of apis
    List<Path> paths = new ArrayList<>();

    String basePath = rest.getPath();

    for (VerbDefinition verb : verbs) {

      // the method must be in lower case
      String method = verb.asVerb().toLowerCase(Locale.US);
      // operation path is a key
      String opPath = SwaggerHelper.buildUrl(basePath, verb.getUri());

      Operation op = new Operation();
      if (ObjectHelper.isNotEmpty(pathAsTag)) {
        // group in the same tag
        op.addTag(pathAsTag);
      }

      // add id as vendor extensions
      op.getVendorExtensions().put("x-camelContextId", camelContextId);
      op.getVendorExtensions().put("x-routeId", verb.getRouteId());

      Path path = swagger.getPath(opPath);
      if (path == null) {
        path = new Path();
        paths.add(path);
      }
      path = path.set(method, op);

      String consumes = verb.getConsumes() != null ? verb.getConsumes() : rest.getConsumes();
      if (consumes != null) {
        String[] parts = consumes.split(",");
        for (String part : parts) {
          op.addConsumes(part);
        }
      }

      String produces = verb.getProduces() != null ? verb.getProduces() : rest.getProduces();
      if (produces != null) {
        String[] parts = produces.split(",");
        for (String part : parts) {
          op.addProduces(part);
        }
      }

      if (verb.getDescriptionText() != null) {
        op.summary(verb.getDescriptionText());
      }

      for (RestOperationParamDefinition param : verb.getParams()) {
        Parameter parameter = null;
        if (param.getType().equals(RestParamType.body)) {
          parameter = new BodyParameter();
        } else if (param.getType().equals(RestParamType.form)) {
          parameter = new FormParameter();
        } else if (param.getType().equals(RestParamType.header)) {
          parameter = new HeaderParameter();
        } else if (param.getType().equals(RestParamType.path)) {
          parameter = new PathParameter();
        } else if (param.getType().equals(RestParamType.query)) {
          parameter = new QueryParameter();
        }

        if (parameter != null) {
          parameter.setName(param.getName());
          parameter.setAccess(param.getAccess());
          parameter.setDescription(param.getDescription());
          parameter.setRequired(param.getRequired());

          // set type on parameter
          if (parameter instanceof SerializableParameter) {
            SerializableParameter sp = (SerializableParameter) parameter;

            if (param.getDataType() != null) {
              sp.setType(param.getDataType());
            }
            if (param.getAllowableValues() != null) {
              sp.setEnum(param.getAllowableValues());
            }
          }

          // set schema on body parameter
          if (parameter instanceof BodyParameter) {
            BodyParameter bp = (BodyParameter) parameter;

            if (verb.getType() != null) {
              String ref = modelTypeAsRef(verb.getType(), swagger);
              if (ref != null) {
                bp.setSchema(new RefModel(ref));
              }
            }
          }

          op.addParameter(parameter);
        }
      }

      // if we have an out type then set that as response message
      if (verb.getOutType() != null) {
        Response response = new Response();
        Property prop = modelTypeAsProperty(verb.getOutType(), swagger);
        response.setSchema(prop);
        response.setDescription("Output type");
        op.addResponse("200", response);
      }

      // enrich with configured response messages from the rest-dsl
      for (RestOperationResponseMsgDefinition msg : verb.getResponseMsgs()) {
        Response response = null;
        if (op.getResponses() != null) {
          response = op.getResponses().get(msg.getCode());
        }
        if (response == null) {
          response = new Response();
        }
        if (ObjectHelper.isNotEmpty(msg.getResponseModel())) {
          Property prop = modelTypeAsProperty(msg.getResponseModel(), swagger);
          response.setSchema(prop);
        }
        response.setDescription(msg.getMessage());
        op.addResponse(msg.getCode(), response);
      }

      // add path
      swagger.path(opPath, path);
    }
  }