public static String determineSchemaReference(final Property schema) { if (schema instanceof RefProperty) { return ((RefProperty) schema).get$ref(); } else if (schema instanceof ArrayProperty) { final Property property = ((ArrayProperty) schema).getItems(); return determineSchemaReference(property); } throw new IllegalArgumentException("Only references to schemas are supported"); }
private static String determineTitleOf(final String name, final Property schema) { final String title = schema.getTitle(); if (title != null) { return title; } final String description = schema.getDescription(); if (description != null) { return description; } final String reference = JsonSchemaHelper.determineSchemaReference(schema); if (reference != null) { return reference.replaceAll("^.*/", ""); } return name; }
private static ObjectNode createSchemaFromProperty(final ObjectNode json, final String name, final Property schema) { if (schema instanceof MapProperty || schema instanceof ObjectProperty) { try { final String schemaString = Json.writer().writeValueAsString(schema); return JsonSchemaHelper.parseJsonSchema(schemaString); } catch (final JsonProcessingException e) { throw new IllegalStateException("Unable to serialize/read given JSON specification in response schema: " + schema, e); } } else if (schema instanceof StringProperty) { final ObjectNode jsonSchema = JsonNodeFactory.instance.objectNode(); final String format = schema.getFormat(); if (format != null) { jsonSchema.put("format", format); } final String type = schema.getType(); if (type != null) { jsonSchema.put("type", type); } return jsonSchema; } final String reference = JsonSchemaHelper.determineSchemaReference(schema); final String title = determineTitleOf(name, schema); return JsonSchemaHelper.resolveSchemaForReference(json, title, reference); }