Skip to content

Commit

Permalink
fix(core): resolve xml nodes from cache by schema name instead of xml…
Browse files Browse the repository at this point in the history
… element name (#645)

Co-authored-by: Timon Back <[email protected]>
  • Loading branch information
sam0r040 and timonback authored Mar 8, 2024
1 parent 26094bc commit 33f7353
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ private T getExampleValueFromSchemaAnnotation(Schema schema) {
}

// Return directly, when we have processed this before
T processedExample =
exampleValueGenerator.getExampleOrNull(exampleValueGenerator.lookupSchemaName(schema), exampleValue);
T processedExample = exampleValueGenerator.getExampleOrNull(schema, exampleValue);
if (processedExample != null) {
return processedExample;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ public interface ExampleValueGenerator<T, R> {

T createRaw(Object exampleValueString);

T getExampleOrNull(String name, Object example);
T getExampleOrNull(Schema schema, Object example);
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public JsonNode createRaw(Object exampleValue) {
}

@Override
public JsonNode getExampleOrNull(String name, Object example) {
public JsonNode getExampleOrNull(Schema schema, Object example) {
if (example instanceof JsonNode) {
return (JsonNode) example;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,27 +196,26 @@ public Node createArrayExample(String name, Node arrayItem) {

@Override
public String prepareForSerialization(Schema schema, Node exampleObject) {
final String name = lookupSchemaName(schema);

final Node objectToWrite;
if (exampleObject instanceof Element) {
objectToWrite = exampleObject;
} else {
final String name = lookupSchemaName(schema);
objectToWrite = wrapNode(name, exampleObject);
}
try {
document.appendChild(objectToWrite);

String xml = exampleXmlValueSerializer.writeDocumentAsXmlString(document);
exampleCache.putIfAbsent(name, exampleObject);
exampleCache.putIfAbsent(getCacheKey(schema), exampleObject);

// spec workaround to embedded xml examples as string https://github.com/asyncapi/spec/issues/1038
schema.setType(OVERRIDE_SCHEMA.getType());
schema.setTypes(OVERRIDE_SCHEMA.getTypes());

return xml;
} catch (TransformerException | DOMException e) {
log.error("Serialize {}", name, e);
log.error("Serialize {}", schema.getName(), e);
return null;
}
}
Expand All @@ -227,7 +226,9 @@ public Node createRaw(Object exampleValue) {
}

@Override
public Node getExampleOrNull(String name, Object example) {
public Node getExampleOrNull(Schema schema, Object example) {
String name = getCacheKey(schema);

if (example instanceof Node) {
return (Node) example;
}
Expand All @@ -244,6 +245,10 @@ public Node createEmptyObjectExample() {
return document.createTextNode("");
}

private String getCacheKey(Schema schema) {
return schema.getName();
}

private Document createDocument() throws ParserConfigurationException {
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
return documentBuilder.newDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public JsonNode createRaw(Object exampleValueString) {
}

@Override
public JsonNode getExampleOrNull(String name, Object example) {
return this.exampleJsonValueGenerator.getExampleOrNull(name, example);
public JsonNode getExampleOrNull(Schema schema, Object example) {
return this.exampleJsonValueGenerator.getExampleOrNull(schema, example);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.core.asyncapi.components.examples.walkers.xml;

import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.media.XML;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Node;

import static org.assertj.core.api.Assertions.assertThat;

class ExampleXmlValueGeneratorTest {

@Test
void cacheShouldResolveBySchemaName() {
// given
ExampleXmlValueGenerator generator = new ExampleXmlValueGenerator(new DefaultExampleXmlValueSerializer());

StringSchema schema1 = new StringSchema();
schema1.setName("full.a.schema1");
schema1.setXml(new XML().name("schema1"));

StringSchema schema2 = new StringSchema();
schema2.setName("full.b.schema1");
schema2.setXml(new XML().name("schema1"));

generator.initialize();
Node example1 = generator.createStringExample();
generator.prepareForSerialization(schema1, example1);

Node cachedExample1 = generator.getExampleOrNull(schema1, "does-not-matter-for-test-1");

// when
generator.initialize();
Node exampleFromCache = generator.getExampleOrNull(schema2, "does-not-matter-for-test-2");

// then
assertThat(exampleFromCache).isNotEqualTo(cachedExample1);
assertThat(exampleFromCache).isNull();
}

@Test
void cacheShouldStoreExampleBySchemaName() {

ExampleXmlValueGenerator generator = new ExampleXmlValueGenerator(new DefaultExampleXmlValueSerializer());

StringSchema schema1 = new StringSchema();
schema1.setName("full.a.schema1");
schema1.setXml(new XML().name("schema1"));

StringSchema schema2 = new StringSchema();
schema2.setName("schema1");

generator.initialize();
Node example1 = generator.createStringExample("example1");
generator.prepareForSerialization(schema1, example1);

generator.initialize();
Node exampleFromCache = generator.getExampleOrNull(schema2, "example-string");

assertThat(exampleFromCache).isNull();
}
}

0 comments on commit 33f7353

Please sign in to comment.