Skip to content

Commit

Permalink
Replaced the StringBuilder for generating JSON (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakshi-75 authored Oct 3, 2023
1 parent 52d97d0 commit 3b0ecc9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.util.RawValue;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
Expand All @@ -11,12 +13,12 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static io.github.stavshamir.springwolf.configuration.properties.SpringwolfConfigConstants.SPRINGWOLF_SCHEMA_EXAMPLE_GENERATOR;

Expand Down Expand Up @@ -126,11 +128,8 @@ private static String getExampleValueFromSchemaAnnotation(Schema schema) {
}

private static String handleArraySchema(Schema schema, Map<String, Schema> definitions, Set<Schema> visited) {
StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append(buildSchemaInternal(schema.getItems(), definitions, visited));
sb.append("]");
return sb.toString();
return Arrays.asList(buildSchemaInternal(schema.getItems(), definitions, visited))
.toString();
}

private static String handleStringSchema(Schema schema) {
Expand Down Expand Up @@ -201,24 +200,14 @@ private static String handleObject(Schema schema, Map<String, Schema> definition

private static String handleObjectProperties(
Map<String, Schema> properties, Map<String, Schema> definitions, Set<Schema> visited) {
StringBuilder sb = new StringBuilder();
sb.append("{");

String data = properties.entrySet().stream()
.map(entry -> {
StringBuilder propertyStringBuilder = new StringBuilder();
propertyStringBuilder.append("\"");
propertyStringBuilder.append(entry.getKey());
propertyStringBuilder.append("\": ");
propertyStringBuilder.append(buildSchemaInternal(entry.getValue(), definitions, visited));
return propertyStringBuilder.toString();
})
.sorted()
.collect(Collectors.joining(","));
sb.append(data);

sb.append("}");

return sb.toString();
ObjectNode objectNode = objectMapper.createObjectNode();

properties.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> {
String propertyKey = entry.getKey();
RawValue propertyRawValue = new RawValue(buildSchemaInternal(entry.getValue(), definitions, visited));
objectNode.putRawValue(propertyKey, propertyRawValue);
});

return objectNode.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ void type_object_array() {

String actual = ExampleJsonGenerator.buildSchema(schema, emptyMap());

assertThat(actual).isEqualTo("[{\"b\": true,\"s\": \"string\"}]");
assertThat(actual).isEqualTo("[{\"b\":true,\"s\":\"string\"}]");
}

@Test
Expand All @@ -285,7 +285,7 @@ void composite_object_without_references() {

String actual = ExampleJsonGenerator.buildSchema(schema, emptyMap());

assertThat(actual).isEqualTo("{\"b\": true,\"s\": \"string\"}");
assertThat(actual).isEqualTo("{\"b\":true,\"s\":\"string\"}");
}

@Test
Expand All @@ -302,7 +302,7 @@ void composite_object_with_references() {
nestedSchema.addProperty("b", new BooleanSchema());
String actual = ExampleJsonGenerator.buildSchema(compositeSchema, Map.of("Nested", nestedSchema));

assertThat(actual).isEqualTo("{\"f\": {\"b\": true,\"s\": \"string\"},\"s\": \"string\"}");
assertThat(actual).isEqualTo("{\"f\":{\"b\":true,\"s\":\"string\"},\"s\":\"string\"}");
}

@Test
Expand All @@ -315,7 +315,7 @@ void object_with_anyOf() {

String actual = ExampleJsonGenerator.buildSchema(compositeSchema, Map.of("Nested", propertySchema));

assertThat(actual).isEqualTo("{\"anyOfField\": \"string\"}");
assertThat(actual).isEqualTo("{\"anyOfField\":\"string\"}");
}

@Test
Expand Down

0 comments on commit 3b0ecc9

Please sign in to comment.