@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 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());
  }
  @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);
  }
Beispiel #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;
  }