Refine search
public BodyParameter createBodyParameter(String paramName) { String modelType = ParamUtils.generateBodyParameterName(operationGenerator.getProviderMethod()); RefModel model = toRefModel(modelType); setName(paramName); setSchema(model); try { return Json.mapper().readValue(Json.mapper().writeValueAsString(this), BodyParameter.class); } catch (IOException e) { throw new IllegalStateException("failed to createBodyParameter.", e); } }
/** * Parse a JSON array * * @param raw Object containing a JSON string * @return JsonNode[contentType, example] * @throws RuntimeException when the given JSON string cannot be parsed */ private JsonNode parseExample(Object raw) throws RuntimeException { try { JsonFactory factory = new JsonFactory(); ObjectMapper mapper = new ObjectMapper(factory); return mapper.readTree(Json.pretty(raw)); } catch (Exception ex) { throw new RuntimeException("Failed to read example", ex); } }
if (model.getExample() != null) { try { String str = Json.mapper().writeValueAsString(model.getExample()); output = Json.mapper().readValue(str, ObjectExample.class); } catch (IOException e) { return null; if (impl.getXml() != null) { Xml xml = impl.getXml(); name = xml.getName(); namespace = xml.getNamespace(); if(impl.getProperties() != null) { for(String key : impl.getProperties().keySet()) { Property property = impl.getProperties().get(key);
@Override public Map<String,Path> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonFactory factory = new JsonFactory(); ObjectMapper mapper = new ObjectMapper(factory); JsonNode paths = mapper.readTree(jp); if (messageBuilder.toString().isEmpty()) { Map<String,ObjectNode> extraFields = new HashMap<>(); ApiDeclaration output = Json.mapper().convertValue(transformed, ApiDeclaration.class); String fieldName = i.next(); JsonNode field = extraField.get(fieldName); if (field.isValueNode()) { path.setVendorExtension("x-"+fieldName, field.asText()); paths.toString(), result.getMessages(), messageBuilder.toString()));
/** * Convert given API definition in JSON format. * @param api The API model to serialize * @param pretty Whether to pretty format the output * @return The API definition as JSON */ static String asJson(Swagger api, boolean pretty) { try { return pretty ? Json.pretty(api) : Json.mapper().writeValueAsString(api); } catch (Exception e) { throw new ApiConfigurationException(e); } }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final String pathInfo = request.getRequestURI(); if (pathInfo.endsWith("/swagger.json")) { response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().println(Json.mapper().writeValueAsString(request.getServletContext().getAttribute("swagger"))); } else { response.setStatus(404); } }
public static boolean validate(Object o, String schema, Direction direction) { try { JsonNode schemaObject = Json.mapper().readTree(schema); JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); JsonNode content = Json.mapper().convertValue(o, JsonNode.class); com.github.fge.jsonschema.main.JsonSchema jsonSchema = factory.getJsonSchema(schemaObject); ProcessingReport report = jsonSchema.validate(content); if(!report.isSuccess()) { if(direction.equals(Direction.INPUT)) { LOGGER.warn("input: " + content.toString() + "\n" + "does not match schema: \n" + schema); } else { LOGGER.warn("response: " + content.toString() + "\n" + "does not match schema: \n" + schema); } } return report.isSuccess(); } catch (Exception e) { LOGGER.error("can't validate model against schema", e); } return true; }
if (input.startsWith("http")) { String json = RemoteUrl.urlToString(input, auths); jsonNode = Json.mapper().readTree(json); } else { final String fileScheme = "file:"; jsonNode = Json.mapper().readTree(json); if (jsonNode == null || jsonNode.get("swaggerVersion") == null) { return null; output = Json.mapper().convertValue(transformed, ResourceListing.class); } catch (java.lang.IllegalArgumentException e) { return null;
/** * Creates a Swagger schema validator based on the given JSON-based Swagger spec. * * @param swaggerSpec the Swagger spec (in JSON format) * @return a validator for that spec * @throws IOException if the Swagger spec is not a valid JSON object */ public static SwaggerValidator forJsonSchema(Reader swaggerSpec) throws IOException { return new SwaggerValidator(Json.mapper().readTree(swaggerSpec)); }
JsonNode ref = obj.get("$ref"); if(ref != null) { if(ref.getNodeType().equals(JsonNodeType.STRING)) { return refParameter((TextNode) ref, location, result); JsonNode ln = obj.get("name"); if(ln != null) { l = ln.asText(); List<String> _enum = new ArrayList<String>(); for(JsonNode n : an) { if(n.isValueNode()) { _enum.add(n.asText()); Map<String, String> examples = Json.mapper().convertValue(examplesNode, Json.mapper().getTypeFactory().constructMapType(Map.class, String.class, Object.class)); bp.setExamples(examples);
Model m = new ModelImpl(); if("array".equals(type)) { ArrayModel am = new ArrayModel(); ModelImpl impl = new ModelImpl(); impl.setType(type); if(ap != null && ap.getNodeType().equals(JsonNodeType.OBJECT)) { impl.setAdditionalProperties(Json.mapper().convertValue(ap, Property.class)); if(arrayNode != null) { for(JsonNode n : arrayNode) { if(n.isValueNode()) { impl._enum(n.asText()); impl.setXml(Json.mapper().convertValue(xml, Xml.class)); Object example = Json.mapper().convertValue(exampleNode, Object.class); model.setExample(example);
if (input.startsWith("http")) { String json = RemoteUrl.urlToString(input, auths); jsonNode = Json.mapper().readTree(json); } else { final String fileScheme = "file:"; jsonNode = Json.mapper().readTree(json); if (jsonNode.isObject()) { ((ObjectNode) jsonNode).remove("authorizations"); output = Json.mapper().convertValue(transformed, ApiDeclaration.class); } catch (java.lang.IllegalArgumentException e) { return null;
while (itr.hasNext()) { JsonNode rm = itr.next(); JsonNode code = rm.get("code"); if (code != null) { if ("200".equals(code.toString())) { type = rm; if (type.get("type") == null) { on.put("type", "void"); } else { on.put("parameters", Json.mapper().createArrayNode());
Config conf = req.require(Config.class); Map<String, Object> hash = conf.getConfig("raml").root().unwrapped(); Raml base = Json.mapper().convertValue(hash, Raml.class); Raml raml = Raml.build(base, provider.apply(req.require(M))); if (configurer != null) {
private void setupSchemaDefinitionRefs(final JsonNode schemaObject) throws IOException { final ObjectNode rootNode = (ObjectNode) schemaObject; rootNode.put(SCHEMA_REF_FIELD, OAI_V2_METASCHEMA_URI); if (additionalPropertiesValidationEnabled()) { injectAdditionalPropertiesDirectiveIntoTree(rootNode); } if (api != null) { if (definitions == null) { definitions = (api.getComponents() == null || api.getComponents().getSchemas() == null) ? Json.mapper().createObjectNode() : Json.mapper().readTree(Json.pretty(api.getComponents().getSchemas())); definitions.forEach(n -> { if (additionalPropertiesValidationEnabled()) { // Explicitly disable additionalProperties // Calling code can choose what level to emit this failure at using // validation.schema.additionalProperties definitionsContainAllOf = injectAdditionalPropertiesDirectiveIntoTree(n); } }); } rootNode.putObject(COMPONENTS_FIELD).set(SCHEMAS_FIELD, definitions); } }
JsonNode ref = node.get("$ref"); if(ref != null) { if(ref.getNodeType().equals(JsonNodeType.STRING)) { JsonNode schemaRef = schema.get("$ref"); if (schemaRef != null) { if (schemaRef.getNodeType().equals(JsonNodeType.STRING)) { Model schemaProp = new RefModel(schemaRef.textValue()); output.responseSchema(schemaProp); } else { output.responseSchema(Json.mapper().convertValue(schema, Model.class)); if(headersNode != null) { Map<String, Property> headers = Json.mapper().convertValue(headersNode, Json.mapper().getTypeFactory().constructMapType(Map.class, String.class, Property.class)); output.headers(headers); Map<String, Object> examples = Json.mapper().convertValue(examplesNode, Json.mapper().getTypeFactory().constructMapType(Map.class, String.class, Object.class)); output.setExamples(examples);
private Status doValidate(final Object value, final Object schema, SchemaValidatorsConfig config) { requireNonNull(schema, "A schema is required"); Status status = null; Set<ValidationMessage> processingReport = null; try { final JsonNode schemaObject = Json.mapper().readTree(Json.pretty(schema)); if (api != null) { if (this.definitions == null) { this.definitions = Json.mapper().readTree(Json.pretty(api.getDefinitions())); } ((ObjectNode)schemaObject).set(DEFINITIONS_FIELD, this.definitions); } JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schemaObject, config); final JsonNode content = Json.mapper().valueToTree(value); processingReport = jsonSchema.validate(content); } catch (JsonParseException e) { return new Status(VALIDATOR_SCHEMA_INVALID_JSON, e.getMessage()); } catch (Exception e) { e.printStackTrace(); } if(processingReport != null && processingReport.size() > 0) { ValidationMessage vm = processingReport.iterator().next(); status = new Status(VALIDATOR_SCHEMA, vm.getMessage()); } return status; }
Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields(); String example = Json.pretty(rootNode); String example = Json.pretty(field.getValue()); example = stripExampleQuotes(StringEscapeUtils.unescapeJson(example)); String example = stripExampleQuotes(field.getValue().toString()); example = StringEscapeUtils.unescapeJava(example); String example = Json.pretty(entry.getValue()); markupDocBuilder.listingBlock(example, "json"); break; // No need to print the same example multiple times String example = Json.pretty(entryByType.getValue()); example = stripExampleQuotes(StringEscapeUtils.unescapeJson(example)); String example = Json.pretty(entry.getValue()); markupDocBuilder.listingBlock(example, "json"); break; // No need to print the same example multiple times markupDocBuilder.listingBlock(Json.pretty(value), "json");
private static ObjectMapper findMapper() { ObjectMapper mapper = Json.mapper(); mapper.registerModule(new EnumModuleExt()); return mapper; }