Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0r040 committed May 24, 2024
1 parent 2136f10 commit 2fd4db5
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean canHandle(String contentType) {
public R fromSchema(Schema schema, Map<String, Schema> definitions) {
exampleValueGenerator.initialize();

String schemaName = exampleValueGenerator.lookupSchemaName(schema);
String schemaName = exampleValueGenerator.lookupSchemaName(schema).get();
try {
T generatedExample = buildExample(schemaName, schema, definitions, new HashSet<>())
.orElseThrow(() -> new ExampleGeneratingException("Something went wrong"));
Expand Down Expand Up @@ -154,13 +154,14 @@ private Optional<T> getExampleValueFromSchemaAnnotation(Schema schema) {
private Optional<T> buildArrayExample(Schema schema, Map<String, Schema> definitions, Set<Schema> visited) {
Schema arrayItemSchema =
resolveSchemaFromRef(schema.getItems(), definitions).orElse(schema.getItems());
String arrayItemName = exampleValueGenerator.lookupSchemaName(arrayItemSchema);

Optional<T> arrayItem = buildExample(arrayItemName, arrayItemSchema, definitions, visited);
Optional<String> arrayName = exampleValueGenerator.lookupSchemaName(schema);

String arrayName = exampleValueGenerator.lookupSchemaName(schema);

return arrayItem.map(array -> exampleValueGenerator.createArrayExample(arrayName, array));
return exampleValueGenerator
.lookupSchemaName(arrayItemSchema)
.or(() -> arrayName)
.flatMap(arrayItemName -> buildExample(arrayItemName, arrayItemSchema, definitions, visited))
.map(arrayItem -> exampleValueGenerator.createArrayExample(arrayName.get(), arrayItem));
}

private Optional<T> buildFromStringSchema(Schema schema) {
Expand Down Expand Up @@ -248,7 +249,12 @@ private List<PropertyExample<T>> buildPropertyExampleListFromSchema(
Map<String, Schema> properties, Map<String, Schema> definitions, Set<Schema> visited) {
return properties.entrySet().stream()
.map(propertySchema -> {
String propertyKey = exampleValueGenerator.lookupSchemaName(propertySchema.getValue());
// There can be instances where the schema has no name and only the property is named
// in this case we se the key as schema name
String propertyKey = exampleValueGenerator
.lookupSchemaName(propertySchema.getValue())
.orElse(propertySchema.getKey());

Optional<T> propertyValue =
buildExample(propertyKey, propertySchema.getValue(), definitions, visited);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface ExampleValueGenerator<T, R> {
*/
default void initialize() {}

String lookupSchemaName(Schema schema);
Optional<String> lookupSchemaName(Schema schema);

/**
* @return The serializable representation of the example (object for json and yaml, string for others)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public boolean canHandle(String contentType) {
}

@Override
public String lookupSchemaName(Schema schema) {
return schema.getName();
public Optional<String> lookupSchemaName(Schema schema) {
return Optional.ofNullable(schema.getName());
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public void initialize() {
}

@Override
public String lookupSchemaName(Schema schema) {
public Optional<String> lookupSchemaName(Schema schema) {
if (schema.getXml() != null && schema.getXml().getName() != null) {
return schema.getXml().getName();
return Optional.of(schema.getXml().getName());
}
return schema.getName();
return Optional.ofNullable(schema.getName());
}

@Override
Expand Down Expand Up @@ -158,15 +158,15 @@ public Node createArrayExample(String name, Node arrayItem) {

@Override
public String prepareForSerialization(Schema schema, Node exampleObject) {
final Node objectToWrite;
final Optional<Node> objectToWrite;
if (exampleObject instanceof Element) {
objectToWrite = exampleObject;
objectToWrite = Optional.of(exampleObject);
} else {
final String name = lookupSchemaName(schema);
objectToWrite = wrapNode(name, exampleObject);
objectToWrite = lookupSchemaName(schema).map(name -> wrapNode(name, exampleObject));
}

try {
document.appendChild(objectToWrite);
document.appendChild(objectToWrite.get());

String xml = exampleXmlValueSerializer.writeDocumentAsXmlString(document);
exampleCache.putIfAbsent(getCacheKey(schema), exampleObject);
Expand Down Expand Up @@ -229,7 +229,7 @@ private Node readXmlString(String xmlString) {
private Optional<Node> createNodeOrAddAttribute(String value, Schema schema) {
if (!nodeStack.isEmpty() && isAttribute(schema)) {
Element currentParent = nodeStack.peek();
currentParent.setAttribute(lookupSchemaName(schema), value);
lookupSchemaName(schema).ifPresent(name -> currentParent.setAttribute(name, value));
return Optional.empty();
} else {
return Optional.of(document.createTextNode(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public boolean canHandle(String contentType) {
}

@Override
public String lookupSchemaName(Schema schema) {
public Optional<String> lookupSchemaName(Schema schema) {
return exampleJsonValueGenerator.lookupSchemaName(schema);
}

Expand Down
Loading

0 comments on commit 2fd4db5

Please sign in to comment.