Skip to content

Commit

Permalink
fix(core): generate correct xml examples for array schemas springwolf…
Browse files Browse the repository at this point in the history
…GH-642

Co-authored-by: Timon Back <timonback@users.noreply.github.com>
  • Loading branch information
sam0r040 and timonback committed Mar 8, 2024
1 parent a8f18eb commit 0ab5718
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@ private T getExampleValueFromSchemaAnnotation(Schema schema) {
}

private T buildArrayExample(Schema schema, Map<String, Schema> definitions, Set<Schema> visited) {
String name = exampleValueGenerator.lookupSchemaName(schema);
T arrayItem = buildExample(name, schema.getItems(), definitions, visited);
Schema arrayItemSchema =
resolveSchemaFromRef(schema.getItems(), definitions).orElse(schema.getItems());
String arrayItemName = exampleValueGenerator.lookupSchemaName(arrayItemSchema);
T arrayItem = buildExample(arrayItemName, arrayItemSchema, definitions, visited);

return exampleValueGenerator.createArrayExample(arrayItem);
String arrayName = exampleValueGenerator.lookupSchemaName(schema);
return exampleValueGenerator.createArrayExample(arrayName, arrayItem);
}

private T buildFromStringSchema(Schema schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public interface ExampleValueGenerator<T, R> {

T createUnknownSchemaStringFormatExample(String schemaFormat);

T createArrayExample(T arrayItem);
T createArrayExample(String name, T arrayItem);

T createRaw(Object exampleValueString);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public JsonNode createUnknownSchemaStringFormatExample(String schemaFormat) {
}

@Override
public JsonNode createArrayExample(JsonNode arrayItem) {
public JsonNode createArrayExample(String name, JsonNode arrayItem) {
ArrayNode array = objectMapper.createArrayNode();
array.add(arrayItem);
return array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ public Node createUnknownSchemaStringFormatExample(String schemaFormat) {
}

@Override
public Node createArrayExample(Node arrayItem) {
return arrayItem;
public Node createArrayExample(String name, Node arrayItem) {
return wrapNode(name, arrayItem);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public JsonNode createUnknownSchemaStringFormatExample(String schemaFormat) {
}

@Override
public JsonNode createArrayExample(JsonNode arrayItem) {
return this.exampleJsonValueGenerator.createArrayExample(arrayItem);
public JsonNode createArrayExample(String name, JsonNode arrayItem) {
return this.exampleJsonValueGenerator.createArrayExample(name, arrayItem);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ void type_object_array() {

String actual = xmlSchemaWalker.fromSchema(schema, emptyMap()).trim();

// TODO FixeMe Expected
// :"<type_object_array><array_element><b>true</b><s>string</s></array_element></type_object_array>"
assertThat(actual).isEqualTo("<type_object_array><b>true</b><s>string</s></type_object_array>");
assertThat(actual)
.isEqualTo(
"<type_object_array><array_element><b>true</b><s>string</s></array_element></type_object_array>");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.core.asyncapi.oprtations;
package io.github.springwolf.core.asyncapi.operations;

import io.github.springwolf.asyncapi.v3.model.channel.ChannelReference;
import io.github.springwolf.asyncapi.v3.model.operation.Operation;
import io.github.springwolf.asyncapi.v3.model.operation.OperationAction;
import io.github.springwolf.core.asyncapi.operations.DefaultOperationsService;
import io.github.springwolf.core.asyncapi.scanners.OperationsScanner;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
}
},
"examples" : [ "<ArrayFoo><flist><b>true</b><s>string</s></flist></ArrayFoo>" ]
"examples" : [ "<ArrayFoo><flist><SimpleFoo><b>true</b><s>string</s></SimpleFoo></flist></ArrayFoo>" ]
},
"io.github.springwolf.core.asyncapi.components.DefaultXmlComponentsServiceTest$SimpleFoo" : {
"type" : "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"type" : "string"
}
},
"examples" : [ "<ComplexFoo><b>true</b><d>1.1</d><dt>2015-07-20T15:49:04-07:00</dt><f>1.1</f><i>0</i><n><nba>YmFzZTY0LWV4YW1wbGU=</nba><nc><cyclic/></nc><nli>0</nli><nmfm/><ns>string</ns><nsm><s>string</s></nsm><nu>3fa85f64-5717-4562-b3fc-2c963f66afa6</nu></n><s>string</s></ComplexFoo>" ]
"examples" : [ "<ComplexFoo><b>true</b><d>1.1</d><dt>2015-07-20T15:49:04-07:00</dt><f>1.1</f><i>0</i><n><nba>YmFzZTY0LWV4YW1wbGU=</nba><nc><cyclic/></nc><nli>0</nli><nmfm/><ns>string</ns><nsm><MyClass><s>string</s></MyClass></nsm><nu>3fa85f64-5717-4562-b3fc-2c963f66afa6</nu></n><s>string</s></ComplexFoo>" ]
},
"io.github.springwolf.core.asyncapi.components.DefaultXmlComponentsServiceTest$ComplexFoo$Nested" : {
"type" : "string",
Expand Down Expand Up @@ -71,7 +71,7 @@
"format" : "uuid"
}
},
"examples" : [ "<Nested><nba>YmFzZTY0LWV4YW1wbGU=</nba><nc><cyclic/></nc><nli>0</nli><nmfm/><ns>string</ns><nsm><s>string</s></nsm><nu>3fa85f64-5717-4562-b3fc-2c963f66afa6</nu></Nested>" ]
"examples" : [ "<Nested><nba>YmFzZTY0LWV4YW1wbGU=</nba><nc><cyclic/></nc><nli>0</nli><nmfm/><ns>string</ns><nsm><MyClass><s>string</s></MyClass></nsm><nu>3fa85f64-5717-4562-b3fc-2c963f66afa6</nu></Nested>" ]
},
"io.github.springwolf.core.asyncapi.components.DefaultXmlComponentsServiceTest$ComplexFoo$Nested$Cyclic" : {
"type" : "string",
Expand Down

0 comments on commit 0ab5718

Please sign in to comment.