-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): moves methods and add class comments
- Loading branch information
Showing
13 changed files
with
124 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,40 +13,21 @@ | |
import io.swagger.v3.core.util.Json; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Slf4j | ||
public class ExampleJsonValueGenerator implements ExampleValueGenerator<JsonNode, JsonNode> { | ||
|
||
private static final BooleanNode DEFAULT_BOOLEAN_EXAMPLE = BooleanNode.TRUE; | ||
|
||
private static final Set<String> SUPPORTED_CONTENT_TYPES = Set.of("application/json"); | ||
private static final BooleanNode DEFAULT_BOOLEAN_EXAMPLE = | ||
BooleanNode.valueOf(ExampleValueGenerator.DEFAULT_BOOLEAN_EXAMPLE); | ||
private static final ObjectMapper objectMapper = Json.mapper(); | ||
private static final Double DEFAULT_NUMBER_EXAMPLE = 1.1; | ||
private static final Integer DEFAULT_INTEGER_EXAMPLE = 0; | ||
|
||
private static final String DEFAULT_DATE_EXAMPLE = "2015-07-20"; | ||
private static final String DEFAULT_DATE_TIME_EXAMPLE = "2015-07-20T15:49:04-07:00"; | ||
private static final String DEFAULT_PASSWORD_EXAMPLE = "string-password"; | ||
private static final String DEFAULT_BYTE_EXAMPLE = "YmFzZTY0LWV4YW1wbGU="; | ||
private static final String DEFAULT_BINARY_EXAMPLE = | ||
"0111010001100101011100110111010000101101011000100110100101101110011000010110010001111001"; | ||
private static final String DEFAULT_STRING_EXAMPLE = "string"; | ||
private static final String DEFAULT_EMAIL_EXAMPLE = "[email protected]"; | ||
private static final String DEFAULT_UUID_EXAMPLE = "3fa85f64-5717-4562-b3fc-2c963f66afa6"; | ||
|
||
private static String DEFAULT_UNKNOWN_SCHEMA_EXAMPLE(String type) { | ||
return "unknown schema type: " + type; | ||
} | ||
|
||
private static String DEFAULT_UNKNOWN_SCHEMA_STRING_EXAMPLE(String format) { | ||
return "unknown string schema format: " + format; | ||
} | ||
|
||
@Override | ||
public boolean canHandle(String contentType) { | ||
return (StringUtils.equals(contentType, "application/json")); | ||
return SUPPORTED_CONTENT_TYPES.contains(contentType); | ||
} | ||
|
||
@Override | ||
|
@@ -138,12 +119,12 @@ public JsonNode generateUuidExample() { | |
|
||
@Override | ||
public JsonNode generateUnknownSchemaStringTypeExample(String type) { | ||
return JsonNodeFactory.instance.textNode(DEFAULT_UNKNOWN_SCHEMA_EXAMPLE(type)); | ||
return JsonNodeFactory.instance.textNode("unknown schema type: " + type); | ||
} | ||
|
||
@Override | ||
public JsonNode generateUnknownSchemaFormatExample(String schemaFormat) { | ||
return JsonNodeFactory.instance.textNode(DEFAULT_UNKNOWN_SCHEMA_STRING_EXAMPLE(schemaFormat)); | ||
return JsonNodeFactory.instance.textNode("unknown string schema format: " + schemaFormat); | ||
} | ||
|
||
@Override | ||
|
@@ -154,7 +135,7 @@ public JsonNode generateArrayExample(JsonNode arrayItem) { | |
} | ||
|
||
@Override | ||
public JsonNode serializeIfNeeded(String name, JsonNode exampleObject) { | ||
public JsonNode prepareForSerialization(String name, JsonNode exampleObject) { | ||
return exampleObject; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,42 @@ | |
|
||
import java.util.List; | ||
|
||
/** | ||
* Provides the building blocks to generate an example | ||
* | ||
* @param <T> The internal representation of a node like object to compose the example | ||
* @param <R> The serializable representation of the example | ||
*/ | ||
interface ExampleValueGenerator<T, R> { | ||
|
||
Boolean DEFAULT_BOOLEAN_EXAMPLE = true; | ||
|
||
String DEFAULT_STRING_EXAMPLE = "string"; | ||
Integer DEFAULT_INTEGER_EXAMPLE = 0; | ||
Double DEFAULT_NUMBER_EXAMPLE = 1.1; | ||
|
||
String DEFAULT_DATE_EXAMPLE = "2015-07-20"; | ||
String DEFAULT_DATE_TIME_EXAMPLE = "2015-07-20T15:49:04-07:00"; | ||
String DEFAULT_PASSWORD_EXAMPLE = "string-password"; | ||
String DEFAULT_BYTE_EXAMPLE = "YmFzZTY0LWV4YW1wbGU="; | ||
String DEFAULT_BINARY_EXAMPLE = | ||
"0111010001100101011100110111010000101101011000100110100101101110011000010110010001111001"; | ||
|
||
String DEFAULT_EMAIL_EXAMPLE = "[email protected]"; | ||
String DEFAULT_UUID_EXAMPLE = "3fa85f64-5717-4562-b3fc-2c963f66afa6"; | ||
|
||
boolean canHandle(String contentType); | ||
|
||
/** | ||
* Some internal representation need to be initialized per Schema | ||
*/ | ||
void initialize(); | ||
|
||
/** | ||
* @return The serializable representation of the example (object for json & yaml, string for others) | ||
*/ | ||
R prepareForSerialization(String name, T exampleObject); | ||
|
||
T createIntegerExample(Integer value); | ||
|
||
T createDoubleExample(Double value); | ||
|
@@ -51,8 +81,6 @@ interface ExampleValueGenerator<T, R> { | |
|
||
T generateArrayExample(T arrayItem); | ||
|
||
R serializeIfNeeded(String name, T exampleObject); | ||
|
||
T createRaw(Object exampleValueString); | ||
|
||
T exampleOrNull(String name, Object example); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
package io.github.stavshamir.springwolf.schemas.example; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.w3c.dom.DOMException; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
|
@@ -19,51 +18,27 @@ | |
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
@Slf4j | ||
public class ExampleXmlValueGenerator implements ExampleValueGenerator<Node, String> { | ||
|
||
private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | ||
private final Set<String> SUPPORTED_CONTENT_TYPES = Set.of("text/xml", "application/xml"); | ||
|
||
private final ExampleXmlValueSerializer exampleXmlValueSerializer; | ||
|
||
private Document document; | ||
|
||
private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | ||
private final Map<String, Node> exampleCache = new HashMap<>(); | ||
|
||
private static final Boolean DEFAULT_BOOLEAN_EXAMPLE = true; | ||
|
||
private static final String DEFAULT_STRING_EXAMPLE = "string"; | ||
|
||
private static final Integer DEFAULT_INTEGER_EXAMPLE = 0; | ||
|
||
private static final Double DEFAULT_NUMBER_EXAMPLE = 1.1; | ||
|
||
private static final String DEFAULT_DATE_EXAMPLE = "2015-07-20"; | ||
private static final String DEFAULT_DATE_TIME_EXAMPLE = "2015-07-20T15:49:04-07:00"; | ||
private static final String DEFAULT_PASSWORD_EXAMPLE = "string-password"; | ||
private static final String DEFAULT_BYTE_EXAMPLE = "YmFzZTY0LWV4YW1wbGU="; | ||
private static final String DEFAULT_BINARY_EXAMPLE = | ||
"0111010001100101011100110111010000101101011000100110100101101110011000010110010001111001"; | ||
|
||
private static final String DEFAULT_EMAIL_EXAMPLE = "[email protected]"; | ||
private static final String DEFAULT_UUID_EXAMPLE = "3fa85f64-5717-4562-b3fc-2c963f66afa6"; | ||
|
||
public ExampleXmlValueGenerator(ExampleXmlValueSerializer exampleXmlValueSerializer) { | ||
this.exampleXmlValueSerializer = exampleXmlValueSerializer; | ||
} | ||
|
||
private static String DEFAULT_UNKNOWN_SCHEMA_EXAMPLE(String type) { | ||
return "unknown schema type: " + type; | ||
} | ||
|
||
private static String DEFAULT_UNKNOWN_SCHEMA_STRING_EXAMPLE(String format) { | ||
return "unknown string schema format: " + format; | ||
} | ||
|
||
@Override | ||
public boolean canHandle(String contentType) { | ||
return (StringUtils.equals(contentType, "text/xml") || StringUtils.equals(contentType, "application/xml")); | ||
return SUPPORTED_CONTENT_TYPES.contains(contentType); | ||
} | ||
|
||
@Override | ||
|
@@ -103,7 +78,7 @@ public Node createIntegerExample() { | |
@Override | ||
public Node createObjectExample(String name, List<PropertyExample<Node>> properties) { | ||
if (name == null) { | ||
throw new IllegalArgumentException("Object Name must not be empty"); | ||
throw new IllegalArgumentException("Object name must not be empty"); | ||
} | ||
try { | ||
Element rootElement = document.createElement(name); | ||
|
@@ -193,12 +168,12 @@ public Node generateEnumExample(String anEnumValue) { | |
|
||
@Override | ||
public Node generateUnknownSchemaStringTypeExample(String schemaType) { | ||
return document.createTextNode(DEFAULT_UNKNOWN_SCHEMA_EXAMPLE(schemaType)); | ||
return document.createTextNode("unknown schema type: " + schemaType); | ||
} | ||
|
||
@Override | ||
public Node generateUnknownSchemaFormatExample(String schemaFormat) { | ||
return document.createTextNode(DEFAULT_UNKNOWN_SCHEMA_STRING_EXAMPLE(schemaFormat)); | ||
return document.createTextNode("unknown string schema format: " + schemaFormat); | ||
} | ||
|
||
@Override | ||
|
@@ -207,7 +182,7 @@ public Node generateArrayExample(Node arrayItem) { | |
} | ||
|
||
@Override | ||
public String serializeIfNeeded(String name, Node exampleObject) { | ||
public String prepareForSerialization(String name, Node exampleObject) { | ||
final Node objectToWrite; | ||
if (exampleObject instanceof Element) { | ||
objectToWrite = exampleObject; | ||
|
@@ -217,7 +192,7 @@ public String serializeIfNeeded(String name, Node exampleObject) { | |
try { | ||
document.appendChild(objectToWrite); | ||
String xml = exampleXmlValueSerializer.writeDocumentAsXmlString(document); | ||
log.info("name {} -> xml: {}", name, xml); | ||
log.debug("name {} -> xml: {}", name, xml); | ||
|
||
exampleCache.putIfAbsent(name, exampleObject); | ||
return xml; | ||
|
Oops, something went wrong.