Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced the StringBuilder for generating JSON #385

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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 @@ -10,12 +12,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 @@ -125,11 +127,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 @@ -200,24 +199,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 @@ -273,7 +273,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 @@ -284,7 +284,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 @@ -301,7 +301,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 @@ -314,7 +314,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