protected Property getErrorSchema() { return new ObjectProperty() .property("error", new StringProperty().description("Error message")) .property("details", new StringProperty().description("Detailed error description")); } }
protected Property getPropertyFromJavaType(String type) { if (type == null) { return new StringProperty(); } if (type.contains(ARRAY_SIGNATURE)) { String itemsType = type.replace(ARRAY_SIGNATURE, ""); return new ArrayProperty(getPropertyFromJavaType(itemsType)); } Property primitiveProperty = getPrimitiveProperty(type); if (primitiveProperty != null) { return primitiveProperty; } Property entityProperty = getObjectProperty(type); if (entityProperty != null) { return entityProperty; } return new StringProperty().description(type); }
protected Property getObjectProperty(String classFqn) { Class<?> clazz; try { clazz = ReflectionHelper.loadClass(classFqn); } catch (ClassNotFoundException e) { return null; } MetaClass metaClass = metadata.getClass(clazz); if (metaClass != null) { return new ObjectProperty() .description(metaClass.getName()); } if (Enum.class.isAssignableFrom(clazz)) { return new StringProperty().description(classFqn); } return null; }
/** * Manually register the Pippo Error class as Swagger model. * * @param swagger * @return a ref for the Error model */ protected RefProperty registerErrorModel(Swagger swagger) { String ref = Error.class.getSimpleName(); if (swagger.getDefinitions() != null && swagger.getDefinitions().containsKey(ref)) { // model already registered return new RefProperty(ref); } ModelImpl model = new ModelImpl(); swagger.addDefinition(ref, model); model.setDescription("an error message"); model.addProperty("statusCode", new IntegerProperty().readOnly().description("http status code")); model.addProperty("statusMessage", new StringProperty().readOnly().description("description of the http status code")); model.addProperty("requestMethod", new StringProperty().readOnly().description("http request method")); model.addProperty("requestUri", new StringProperty().readOnly().description("http request path")); model.addProperty("message", new StringProperty().readOnly().description("application message")); if (settings.isDev()) { // in DEV mode the stacktrace is returned in the error message model.addProperty("stacktrace", new StringProperty().readOnly().description("application stacktrace")); } return new RefProperty(ref); }
/** * Manually register the Pippo Error class as Swagger model. * * @param swagger * @return a ref for the Error model */ protected RefProperty registerErrorModel(Swagger swagger) { String ref = Error.class.getSimpleName(); if (swagger.getDefinitions() != null && swagger.getDefinitions().containsKey(ref)) { // model already registered return new RefProperty(ref); } ModelImpl model = new ModelImpl(); swagger.addDefinition(ref, model); model.setDescription("an error message"); model.addProperty("statusCode", new IntegerProperty().readOnly().description("http status code")); model.addProperty("statusMessage", new StringProperty().readOnly().description("description of the http status code")); model.addProperty("requestMethod", new StringProperty().readOnly().description("http request method")); model.addProperty("requestUri", new StringProperty().readOnly().description("http request path")); model.addProperty("message", new StringProperty().readOnly().description("application message")); if (settings.isDev()) { // in DEV mode the stacktrace is returned in the error message model.addProperty("stacktrace", new StringProperty().readOnly().description("application stacktrace")); } return new RefProperty(ref); }
private static Response constructResponse(int status, String error) { Property statusProp = new IntegerProperty() .description("The status code of the error (also provided in the HTTP header)"); statusProp.setExample(status); Property errorProp = new StringProperty() .description("The error message describing the error"); errorProp.setExample((Object)error); return new Response() .description(error) .schema(new ObjectProperty() .property("status", statusProp) .property("error", errorProp)); }
.required(annotation.required()) .example(annotation.example().isEmpty() ? null : annotation.example()) .description(annotation.value().isEmpty() ? null : annotation.value());
private static Map<String, Property> initProperties() { Map<String, Property> properties = new LinkedHashMap<>(); properties.put(PROPERTY_ONE_NAME, new StringProperty() .example(PROPERTY_ONE_EXAMPLE) .required(true) .description(PROPERTY_ONE_DESCRIPTION)); properties.put(PROPERTY_TWO_NAME, new StringProperty() .example(PROPERTY_TWO_EXAMPLE) .required(true) .description(PROPERTY_TWO_DESCRIPTION)); IntegerProperty integerProp = new IntegerProperty(); LongProperty longProp = new LongProperty(); BooleanProperty booleanProp = new BooleanProperty(); integerProp.setExample(""); longProp.setExample(""); booleanProp.setExample(""); properties.put(ARRAY_PROP_NAME, new ArrayProperty().example("").description("")); properties.put(DOUBLE_PROP_NAME, new DoubleProperty().example("").description("")); properties.put(FLOAT_PROP_NAME, new FloatProperty().example("").description("")); properties.put(INTEGER_PROP_NAME, integerProp.description("")); properties.put(LONG_PROP_NAME, longProp.description("")); properties.put(BOOLEAN_PROP_NAME, booleanProp.description("")); properties.put(OBJECT_PROP_NAME, new ObjectProperty().example("").description("")); return properties; } }
protected Operation generateEntitySearchOperation(ModelImpl entityModel, RequestMethod method) { Operation operation = new Operation() .tag(entityModel.getName()) .produces(APPLICATION_JSON_VALUE) .summary("Find entities by filter conditions: " + entityModel.getName()) .description("Finds entities by filter conditions. The filter is defined by JSON object " + "that is passed as in URL parameter.") .response(200, new Response() .description("Success. Entities that conforms filter conditions are returned in the response body.") .schema(new ArrayProperty(new RefProperty(ENTITY_DEFINITION_PREFIX + entityModel.getName())))) .response(400, getErrorResponse("Bad request. For example, the condition value cannot be parsed.")) .response(403, getErrorResponse("Forbidden. The user doesn't have permissions to read the entity.")) .response(404, getErrorResponse("Not found. MetaClass for the entity with the given name not found.")); if (RequestMethod.GET == method) { QueryParameter parameter = new QueryParameter() .name("filter") .required(true) .property(new StringProperty().description("JSON with filter definition")); operation.parameter(parameter); } else { BodyParameter parameter = new BodyParameter() .name("filter") .schema(new ModelImpl() .property("JSON with filter definition", new StringProperty())); parameter.setRequired(true); operation.parameter(parameter); } operation.getParameters().addAll(generateEntityOptionalParams(false)); return operation; }
protected Operation generateEntityUpdateOperation(ModelImpl entityModel) { BodyParameter entityParam = new BodyParameter() .name("entityJson") .description("JSON object with the entity") .schema(new RefModel(ENTITY_DEFINITION_PREFIX + entityModel.getName())); entityParam.setRequired(true); PathParameter entityIdParam = new PathParameter() .name("entityId") .description("Entity identifier") .required(true) .property(new StringProperty().description("Entity identifier")); return new Operation() .tag(entityModel.getName()) .produces(APPLICATION_JSON_VALUE) .summary("Updates the entity: " + entityModel.getName()) .description("Updates the entity. Only fields that are passed in the JSON object " + "(the request body) are updated.") .parameter(entityIdParam) .parameter(entityParam) .response(200, new Response() .description("Success. The updated entity is returned in the response body.") .schema(new RefProperty(ENTITY_DEFINITION_PREFIX + entityModel.getName()))) .response(403, getErrorResponse("Forbidden. The user doesn't have permissions to update the entity.")) .response(404, getErrorResponse("Not found. MetaClass for the entity with the given name not found.")); }