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

    swagger.addDefinition(
        "User",
        new ArrayModel()
            .items(
                new ObjectProperty()
                    .title("title")
                    ._default("default")
                    .access("access")
                    .readOnly(false)
                    .required(true)
                    .description("description")
                    .name("name")
                    .property("street", new StringProperty())
                    .property("city", new StringProperty())));

    new InlineModelResolver().flatten(swagger);

    Model model = swagger.getDefinitions().get("User");
    assertTrue(model instanceof ArrayModel);

    Model user = swagger.getDefinitions().get("User_inner");
    assertNotNull(user);
    assertEquals("description", user.getDescription());
  }
  @Test
  public void resolveInlineModelTest() throws Exception {
    Swagger swagger = new Swagger();

    swagger.addDefinition(
        "User",
        new ModelImpl()
            .name("user")
            .description("a common user")
            .property("name", new StringProperty())
            .property(
                "address",
                new ObjectProperty()
                    .title("title")
                    ._default("default")
                    .access("access")
                    .readOnly(false)
                    .required(true)
                    .description("description")
                    .name("name")
                    .property("street", new StringProperty())
                    .property("city", new StringProperty())));

    new InlineModelResolver().flatten(swagger);

    ModelImpl user = (ModelImpl) swagger.getDefinitions().get("User");

    assertNotNull(user);
    assertTrue(user.getProperties().get("address") instanceof RefProperty);

    ModelImpl address = (ModelImpl) swagger.getDefinitions().get("User_address");
    assertNotNull(address);
    assertNotNull(address.getProperties().get("city"));
    assertNotNull(address.getProperties().get("street"));
  }
Exemplo n.º 3
0
  void appendLinkModelDefinition() {
    swagger.addDefinition(
        "LinkRepr",
        new ModelImpl()
            .type("object")
            .property(
                "rel",
                stringProperty()
                    .description("the relationship of the resource to this referencing resource"))
            .property(
                "href",
                stringProperty().description("the hyperlink reference (URL) of the resource"))
            .property("title", stringProperty().description("title to render"))
            .property(
                "method",
                stringPropertyEnum("GET", "POST", "PUT", "DELETE")
                    .description("HTTP verb to access"))
            .property(
                "type",
                stringProperty()
                    .description(
                        "Content-Type recognized by the resource (for HTTP Accept header)"))
            .property(
                "arguments",
                new ObjectProperty()
                    .description("Any arguments, to send as query strings or in body"))
            .property(
                "value", stringProperty().description("the representation of the link if followed"))
            .required("rel")
            .required("href")
            .required("method"));

    swagger.addDefinition(
        "HrefRepr",
        new ModelImpl()
            .type("object")
            .description(
                "Abbreviated version of the Link resource, used primarily to reference non-value objects")
            .property(
                "href",
                stringProperty().description("the hyperlink reference (URL) of the resource"))
            .required("href"));
  }
Exemplo n.º 4
0
  ModelImpl appendObjectPathAndModelDefinitions(final ObjectSpecification objectSpec) {

    final String objectType = objectTypeFor(objectSpec);

    final Path path = new Path();
    swagger.path(String.format("/objects/%s/{objectId}", objectType), path);

    final String tag = tagFor(objectType, null);
    final Operation operation = new Operation();
    path.get(operation);
    operation
        .tag(tag)
        .description(Util.roSpec("14.1"))
        .parameter(new PathParameter().name("objectId").type("string"))
        .produces("application/json;profile=urn:org.apache.isis/v1")
        .produces("application/json;profile=urn:org.apache.isis/v1;suppress=true")
        .produces("application/json;profile=urn:org.restfulobjects:repr-types/object");

    // per https://github.com/swagger-api/swagger-spec/issues/146, swagger 2.0 doesn't support
    // multiple
    // modelled representations per path and response code;
    // in particular cannot associate representation/model with Accept header ('produces(...)
    // method)
    final String restfulObjectsModelDefinition = objectType + "RestfulObjectsRepr";
    if (false) {
      operation.response(
          200,
          newResponse(Caching.TRANSACTIONAL)
              .description(
                  "if Accept: application/json;profile=urn:org.restfulobjects:repr-types/object")
              .schema(newRefProperty(restfulObjectsModelDefinition)));

      final ModelImpl roSpecModel =
          newModel(Util.roSpec("14.4") + ": representation of " + objectType)
              .property("title", stringProperty())
              .property("domainType", stringProperty()._default(objectType))
              .property("instanceId", stringProperty())
              .property("members", new ObjectProperty());
      swagger.addDefinition(restfulObjectsModelDefinition, roSpecModel);
    }

    final String isisModelDefinition = objectType + "Repr";
    operation.response(
        200,
        newResponse(Caching.TRANSACTIONAL)
            .description(
                objectType + " , if Accept: application/json;profile=urn:org.apache.isis/v1")
            .schema(newRefProperty(isisModelDefinition)));

    final ModelImpl isisModel = new ModelImpl();
    addDefinition(isisModelDefinition, isisModel);

    // return so can be appended to
    return isisModel;
  }
  @Test
  public void testBasicInput() {
    Swagger swagger = new Swagger();

    ModelImpl user = new ModelImpl().property("name", new StringProperty());

    swagger.path(
        "/foo/baz",
        new Path()
            .post(
                new Operation()
                    .parameter(new BodyParameter().name("myBody").schema(new RefModel("User")))));

    swagger.addDefinition("User", user);

    new InlineModelResolver().flatten(swagger);

    Json.prettyPrint(swagger);
  }
Exemplo n.º 6
0
 private void addDefinition(final String key, final ModelImpl model) {
   addSwaggerDefinition(key);
   swagger.addDefinition(key, model);
 }
Exemplo n.º 7
0
 void appendDefinitionsForOrphanedReferences() {
   final Set<String> referencesWithoutDefinition = getReferencesWithoutDefinition();
   for (String reference : referencesWithoutDefinition) {
     swagger.addDefinition(reference, new ModelImpl());
   }
 }
Exemplo n.º 8
0
  void appendRestfulObjectsSupportingPathsAndDefinitions() {

    final String tag = "> restful objects supporting resources";

    swagger.path(
        "/",
        new Path()
            .get(
                new Operation()
                    .tag(tag)
                    .description(Util.roSpec("5.1"))
                    .produces("application/json")
                    .produces(
                        "application/json;profile=urn:org.restfulobjects:repr-types/home-page")
                    .response(
                        200,
                        newResponse(Caching.NON_EXPIRING)
                            .description("OK")
                            .schema(newRefProperty("RestfulObjectsSupportingHomePageRepr")))));
    addDefinition("RestfulObjectsSupportingHomePageRepr", newModel(Util.roSpec("5.2")));

    swagger.path(
        "/user",
        new Path()
            .get(
                new Operation()
                    .tag(tag)
                    .description(Util.roSpec("6.1"))
                    .produces("application/json")
                    .produces("application/json;profile=urn:org.restfulobjects:repr-types/user")
                    .response(
                        200,
                        newResponse(Caching.USER_INFO)
                            .description("OK")
                            .schema(newRefProperty("RestfulObjectsSupportingUserRepr")))));
    addDefinition(
        "RestfulObjectsSupportingUserRepr",
        newModel(Util.roSpec("6.2"))
            .property("userName", stringProperty())
            .property("roles", arrayOfStrings())
            .property("links", arrayOfLinks())
            .required("userName")
            .required("roles"));

    swagger.path(
        "/services",
        new Path()
            .get(
                new Operation()
                    .tag(tag)
                    .description(Util.roSpec("7.1"))
                    .produces("application/json")
                    .produces("application/json;profile=urn:org.restfulobjects:repr-types/services")
                    .response(
                        200,
                        newResponse(Caching.USER_INFO)
                            .description("OK")
                            .schema(newRefProperty("RestfulObjectsSupportingServicesRepr")))));
    addDefinition(
        "RestfulObjectsSupportingServicesRepr",
        newModel(Util.roSpec("7.2"))
            .property("value", arrayOfLinks())
            .required("userName")
            .required("roles"));

    swagger.path(
        "/version",
        new Path()
            .get(
                new Operation()
                    .tag(tag)
                    .description(Util.roSpec("8.1"))
                    .produces("application/json")
                    .produces(
                        "application/json;profile=urn:org.restfulobjects:repr-types/RestfulObjectsSupportingServicesRepr")
                    .response(
                        200,
                        newResponse(Caching.NON_EXPIRING)
                            .description("OK")
                            .schema(new ObjectProperty()))));
    swagger.addDefinition(
        "RestfulObjectsSupportingServicesRepr",
        newModel(Util.roSpec("8.2"))
            .property("specVersion", stringProperty())
            .property("implVersion", stringProperty())
            .property(
                "optionalCapabilities",
                new ObjectProperty()
                    .property("blobsClobs", stringProperty())
                    .property("deleteObjects", stringProperty())
                    .property("domainModel", stringProperty())
                    .property("validateOnly", stringProperty())
                    .property("protoPersistentObjects", stringProperty()))
            .required("userName")
            .required("roles"));
  }
  public String processRefToExternalDefinition(String $ref, RefFormat refFormat) {
    final Model model = cache.loadRef($ref, refFormat, Model.class);

    String newRef;

    Map<String, Model> definitions = swagger.getDefinitions();

    if (definitions == null) {
      definitions = new HashMap<>();
    }

    final String possiblyConflictingDefinitionName = computeDefinitionName($ref);

    Model existingModel = definitions.get(possiblyConflictingDefinitionName);

    if (existingModel != null) {
      LOGGER.debug("A model for " + existingModel + " already exists");
      if (existingModel instanceof RefModel) {
        // use the new model
        existingModel = null;
      }
    }
    newRef = possiblyConflictingDefinitionName;
    cache.putRenamedRef($ref, newRef);

    if (existingModel == null) {
      // don't overwrite existing model reference
      swagger.addDefinition(newRef, model);
      cache.addReferencedKey(newRef);

      String file = $ref.split("#/")[0];
      if (model instanceof RefModel) {
        RefModel refModel = (RefModel) model;
        if (isAnExternalRefFormat(refModel.getRefFormat())) {
          refModel.set$ref(
              processRefToExternalDefinition(refModel.get$ref(), refModel.getRefFormat()));
        } else {
          processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE);
        }
      }
      // Loop the properties and recursively call this method;
      Map<String, Property> subProps = model.getProperties();
      if (subProps != null) {
        for (Map.Entry<String, Property> prop : subProps.entrySet()) {
          if (prop.getValue() instanceof RefProperty) {
            processRefProperty((RefProperty) prop.getValue(), file);
          } else if (prop.getValue() instanceof ArrayProperty) {
            ArrayProperty arrayProp = (ArrayProperty) prop.getValue();
            if (arrayProp.getItems() instanceof RefProperty) {
              processRefProperty((RefProperty) arrayProp.getItems(), file);
            }
          } else if (prop.getValue() instanceof MapProperty) {
            MapProperty mapProp = (MapProperty) prop.getValue();
            if (mapProp.getAdditionalProperties() instanceof RefProperty) {
              processRefProperty((RefProperty) mapProp.getAdditionalProperties(), file);
            } else if (mapProp.getAdditionalProperties() instanceof ArrayProperty
                && ((ArrayProperty) mapProp.getAdditionalProperties()).getItems()
                    instanceof RefProperty) {
              processRefProperty(
                  (RefProperty) ((ArrayProperty) mapProp.getAdditionalProperties()).getItems(),
                  file);
            }
          }
        }
      }
      if (model instanceof ArrayModel && ((ArrayModel) model).getItems() instanceof RefProperty) {
        processRefProperty((RefProperty) ((ArrayModel) model).getItems(), file);
      }
    }

    return newRef;
  }