List of usage examples for com.fasterxml.jackson.module.jsonSchema.factories SchemaFactoryWrapper finalSchema
public JsonSchema finalSchema()
From source file:com.phoenixnap.oss.ramlapisync.parser.SpringMvcResourceParser.java
@Override protected Pair<String, RamlMimeType> extractRequestBody(Method method, Map<String, String> parameterComments, String comment, List<ApiParameterMetadata> apiParameters) { RamlMimeType mimeType = RamlModelFactoryOfFactories.createRamlModelFactory().createRamlMimeType(); String type;/*from w w w . j a v a 2s . c o m*/ //Handle empty body if (apiParameters != null && apiParameters.size() == 0) { // do nothing here return null; } else if (apiParameters != null && apiParameters.size() == 1 && String.class.equals(apiParameters.get(0).getType()) // Handle Plain Text parameters && apiParameters.get(0).isAnnotationPresent(RequestBody.class)) { ApiParameterMetadata apiParameterMetadata = apiParameters.get(0); type = "text/plain"; if (StringUtils.hasText(apiParameterMetadata.getExample())) { mimeType.setExample(apiParameterMetadata.getExample()); } ObjectMapper m = new ObjectMapper(); SchemaFactoryWrapper visitor = new SchemaFactoryWrapper(); try { m.acceptJsonFormatVisitor(m.constructType(String.class), visitor); JsonSchema jsonSchema = visitor.finalSchema(); String description = parameterComments.get(apiParameterMetadata.getJavaName()); if (description == null) { description = apiParameterMetadata.getName(); } jsonSchema.setDescription(description); jsonSchema.setRequired(!apiParameterMetadata.isNullable()); mimeType.setSchema(m.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema)); } catch (JsonProcessingException e) { throw new IllegalStateException(e); } return new Pair<>(type, mimeType); } else if (apiParameters != null && (apiParameters.size() > 1 || (!apiParameters.get(0).isAnnotationPresent(RequestBody.class) && String.class.equals(apiParameters.get(0).getType())))) { type = "application/x-www-form-urlencoded"; for (ApiParameterMetadata param : apiParameters) { RamlFormParameter formParameter = RamlModelFactoryOfFactories.createRamlModelFactory() .createRamlFormParameter(); formParameter.setDisplayName(param.getName()); formParameter.setExample(param.getExample()); RamlParamType simpleType = SchemaHelper.mapSimpleType(param.getType()); formParameter.setType(simpleType == null ? RamlParamType.STRING : simpleType); String description = parameterComments.get(param.getJavaName()); if (description == null) { description = param.getName(); } formParameter.setDescription(description); formParameter.setRequired(!param.isNullable()); Map<String, List<RamlFormParameter>> paramMap; if (mimeType.getFormParameters() == null) { paramMap = new TreeMap<>(); mimeType.setFormParameters(paramMap); } else { paramMap = mimeType.getFormParameters(); } mimeType.addFormParameters(param.getName(), Collections.singletonList(formParameter)); } return new Pair<>(type, mimeType); } else { return super.extractRequestBody(method, parameterComments, comment, apiParameters); } }
From source file:org.datacleaner.monitor.server.controllers.ComponentControllerV1.java
static void setPropertyType(ComponentDescriptor<?> descriptor, ConfiguredPropertyDescriptor propertyDescriptor, ComponentList.PropertyInfo propInfo) { // TODO: avoid instanceof by extending the basic ComponentDescriptor // interface (maybe add getter for property "Type" in addition to // "Class" ? ) SchemaFactoryWrapper visitor = new SchemaFactoryWrapper(); if (propertyDescriptor instanceof AbstractPropertyDescriptor) { Field f = ((AbstractPropertyDescriptor) propertyDescriptor).getField(); Type t = f.getGenericType(); if (t instanceof Class) { propInfo.setClassDetails(((Class<?>) t).getCanonicalName()); } else {/*from w ww. ja va 2s . c o m*/ propInfo.setClassDetails(f.getGenericType().toString()); } if (!propertyDescriptor.isInputColumn()) { try { ComponentHandler.mapper.acceptJsonFormatVisitor( ComponentHandler.mapper.constructType(f.getGenericType()), visitor); } catch (JsonMappingException e) { throw new RuntimeException(e); } } } else { propInfo.setClassDetails(propertyDescriptor.getType().getCanonicalName()); if (!propertyDescriptor.isInputColumn()) { try { ComponentHandler.mapper.acceptJsonFormatVisitor( ComponentHandler.mapper.constructType(propertyDescriptor.getType()), visitor); } catch (JsonMappingException e) { throw new RuntimeException(e); } } } propInfo.setClassName(propertyDescriptor.getType().getName()); if (!propertyDescriptor.isInputColumn()) { propInfo.setSchema(visitor.finalSchema()); } }
From source file:org.emonocot.portal.controller.SearchController.java
@RequestMapping(method = RequestMethod.OPTIONS, produces = "application/json") public ResponseEntity<RestDoc> optionsResource() throws JsonMappingException { RestDoc restDoc = new RestDoc(); HashMap<String, Schema> schemas = new HashMap<String, Schema>(); Schema pagerSchema = new Schema(); SchemaFactoryWrapper pageVisitor = new SchemaFactoryWrapper(); objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(Page.class), pageVisitor); pagerSchema.setSchema(pageVisitor.finalSchema()); schemas.put("http://e-monocot.org#page", pagerSchema); restDoc.setSchemas(schemas);/* w ww.j a va2 s . c o m*/ GlobalHeader headers = new GlobalHeader(); headers.request("Content-Type", "Must be set to application/json", true); headers.request("Authorization", "Supports HTTP Basic. Users may also use their api key", false); restDoc.setHeaders(headers); ParamValidation integerParam = new ParamValidation(); integerParam.setType("match"); integerParam.setPattern("\\d+"); ParamValidation apikeyParam = new ParamValidation(); apikeyParam.setType("match"); apikeyParam.setPattern("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"); ParamValidation stringParam = new ParamValidation(); stringParam.setType("match"); stringParam.setPattern("[0-9a-f]+"); ParamValidation doubleParam = new ParamValidation(); doubleParam.setType("match"); doubleParam.setPattern("[0-9]+\\.[0.9]+"); Set<RestResource> resources = new HashSet<RestResource>(); RestResource searchForObjects = new RestResource(); searchForObjects.setId("Search"); searchForObjects.setPath("/search{?query,x1,y1,x2,y2,facet,limit,start,sort,callback,apikey,fetch}"); searchForObjects.param("limit", "The maximum number of resources to return", integerParam); searchForObjects.param("start", "The number of pages (of size _limit_) offset from the beginning of the recordset", integerParam); searchForObjects.param("apikey", "The apikey of the user account making the request", apikeyParam); searchForObjects.param("callback", "The name of the callback function used to wrap the JSON response", stringParam); searchForObjects.param("x1", "The southerly extent of the bounding box (uses WGS84 Coordinate reference system). Only documents with distributions within the bounding box will be returned", doubleParam); searchForObjects.param("y1", "The westerly extent of the bounding box (uses WGS84 Coordinate reference system). Only documents with distributions within the bounding box will be returned", doubleParam); searchForObjects.param("x2", "The northerly extent of the bounding box (uses WGS84 Coordinate reference system). Only documents with distributions within the bounding box will be returned", doubleParam); searchForObjects.param("y2", "The easterly extent of the bounding box (uses WGS84 Coordinate reference system). Only documents with distributions within the bounding box will be returned", doubleParam); searchForObjects.param("query", "A free-text query string. Only documents matching the query string will be returned", stringParam); searchForObjects.param("facet", "Only return documents which match a particular filter, in the form {fieldName}:{fieldValue} where fieldName is from the controlled vocabulary defined by org.emonocot.pager.FacetName.", stringParam); searchForObjects.param("sort", "Sort the result set according to the supplied criteria, in the form {fieldName}_(asc|desc) where fieldName is from the controlled vocabulary defined by org.emonocot.pager.FacetName.", stringParam); searchForObjects.param("fetch", "The name of a valid 'fetch-profile' which will load some or all related objects prior to serialization. Try 'object-page' to return most related objects", stringParam); MethodDefinition searchObjects = new MethodDefinition(); searchObjects.description("Search for resources"); ResponseDefinition searchObjectsResponseDefinition = new ResponseDefinition(); searchObjectsResponseDefinition.type("application/json", "http://e-monocot.org#page"); searchObjectsResponseDefinition.type("application/javascript", "http://e-monocot.org#page"); searchObjects.response(searchObjectsResponseDefinition); searchObjects.statusCode("200", "Successfully searched for resources"); searchForObjects.method("GET", searchObjects); resources.add(searchForObjects); restDoc.setResources(resources); return new ResponseEntity<RestDoc>(restDoc, HttpStatus.OK); }
From source file:org.versly.rest.wsdoc.AnnotationProcessor.java
String jsonSchemaFromTypeMirror(TypeMirror type) { String serializedSchema = null; if (type.getKind().isPrimitive() || type.getKind() == TypeKind.VOID) { return null; }/* w w w . j av a 2 s .c o m*/ // we need the dto class to generate schema using jackson json-schema module // note: Types.erasure() provides canonical names whereas Class.forName() wants a "regular" name, // so forName will fail for nested and inner classes as "regular" names use $ between parent and child. Class dtoClass = null; StringBuffer erasure = new StringBuffer(_typeUtils.erasure(type).toString()); for (boolean done = false; !done;) { try { dtoClass = Class.forName(erasure.toString()); done = true; } catch (ClassNotFoundException e) { if (erasure.lastIndexOf(".") != -1) { erasure.setCharAt(erasure.lastIndexOf("."), '$'); } else { done = true; } } } // if we were able to figure out the dto class, use jackson json-schema module to serialize it Exception e = null; if (dtoClass != null) { try { ObjectMapper m = new ObjectMapper(); m.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); m.registerModule(new JodaModule()); SchemaFactoryWrapper visitor = new SchemaFactoryWrapper(); m.acceptJsonFormatVisitor(m.constructType(dtoClass), visitor); serializedSchema = m.writeValueAsString(visitor.finalSchema()); } catch (Exception ex) { e = ex; } } // report warning if we were not able to generate schema for non-primitive type if (serializedSchema == null) { this.processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "cannot generate json-schema for class " + type.toString() + " (erasure " + erasure + "), " + ((e != null) ? ("exception: " + e.getMessage()) : "class not found")); } return serializedSchema; }