Skip to content

Commit

Permalink
Port to Java 11
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Dubinsky committed Feb 2, 2022
1 parent 33cd268 commit 11948a8
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 106 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.AccessLevel;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;

// Class that manages creating new definitions or getting $refs to existing definitions
@RequiredArgsConstructor
class DefinitionsHandler {

record DefinitionInfo(String ref, JsonObjectFormatVisitor jsonObjectFormatVisitor) {}
record WorkInProgress(JavaType typeInProgress, ObjectNode nodeInProgress) {}
// can be records
@Data @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) @Accessors(fluent = true)
static class DefinitionInfo { String ref; JsonObjectFormatVisitor jsonObjectFormatVisitor; }
@Data @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) @Accessors(fluent = true)
static class WorkInProgress { JavaType typeInProgress; ObjectNode nodeInProgress; }

final JsonSchemaConfig config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.validation.constraints.*;
import lombok.AccessLevel;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand Down Expand Up @@ -357,7 +361,8 @@ class MapVisitor extends AbstractJsonFormatVisitorWithSerializerProvider impleme
return new MapVisitor();
}

record PolymorphismInfo(String typePropertyName, String subTypeName) {}
@Data @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) @Accessors(fluent = true)
static class PolymorphismInfo { String typePropertyName; String subTypeName; }

private PolymorphismInfo extractPolymorphismInfo(JavaType type) throws JsonMappingException {

Expand Down Expand Up @@ -642,7 +647,9 @@ void handleProperty(String propertyName, JavaType propertyType, BeanProperty pro
}

// Need to check for Optional/Optional-special-case before we know what node to use here.
record PropertyNode(ObjectNode main, ObjectNode meta) {}
// record PropertyNode(ObjectNode main, ObjectNode meta) {}
@Data @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) @Accessors(fluent = true)
class PropertyNode { final ObjectNode main; final ObjectNode meta; }

// Check if we should set this property as required. Primitive types MUST have a value, as does anything
// with a @JsonProperty that has "required" set to true. Lastly, various javax.validation annotations also
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/kjetland/jackson/jsonSchema/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public static void merge(JsonNode mainNode, JsonNode updateNode) {
merge(jsonNode, updateNode.get(fieldName));
}
else {
if (mainNode instanceof ObjectNode node) {
if (mainNode instanceof ObjectNode) {
// Overwrite field
var value = updateNode.get(fieldName);
node.set(fieldName, value);
((ObjectNode)mainNode).set(fieldName, value);
}
}
}
Expand Down Expand Up @@ -165,8 +165,8 @@ public static List<Class<?>> extractGroupsFromAnnotation(Annotation annotation)
return List.of();
}
}
else if (annotation instanceof JsonSchemaInject _annotation)
return List.of(_annotation.javaxValidationGroups());
else if (annotation instanceof JsonSchemaInject)
return List.of(((JsonSchemaInject)annotation).javaxValidationGroups());
else
return List.of();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1180,12 +1180,12 @@ void checkInjected(JsonNode schema, String propertyName, boolean included) {
out.println("--------------------------------------------");
out.println(schemaJson);

var fasit = """
{
"everything" : "should be replaced"
}""".stripIndent();
var fasit =
"{\n" +
" \"everything\" : \"should be replaced\"\n" +
"}".stripIndent();

assertTrue ( schemaJson .equals (fasit) );
assertEquals(fasit, schemaJson);
}

@Test void preventingPolymorphismWithClassTypeRemapping_classWithProperty() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
package com.kjetland.jackson.jsonSchema.testData;

import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.*;
import javax.validation.groups.Default;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.experimental.FieldDefaults;
import lombok.extern.jackson.Jacksonized;

public record ClassUsingValidation
(
@FieldDefaults(level = AccessLevel.PUBLIC, makeFinal = true) @Jacksonized @Builder @EqualsAndHashCode // Can be Java 17 record
public class ClassUsingValidation
{
@NotNull
String stringUsingNotNull,
String stringUsingNotNull;

@NotBlank
String stringUsingNotBlank,
String stringUsingNotBlank;

@NotNull
@NotBlank
String stringUsingNotBlankAndNotNull,
String stringUsingNotBlankAndNotNull;

@NotEmpty
String stringUsingNotEmpty,
String stringUsingNotEmpty;

@NotEmpty
List<String> notEmptyStringArray, // Per PojoArraysWithScala, we use always use Lists in Scala, and never raw arrays.
List<String> notEmptyStringArray; // Per PojoArraysWithScala, we use always use Lists in Scala, and never raw arrays.

@NotEmpty
Map<String, String> notEmptyMap,
Map<String, String> notEmptyMap;

@Size(min=1, max=20)
String stringUsingSize,
String stringUsingSize;

@Size(min=1)
String stringUsingSizeOnlyMin,
String stringUsingSizeOnlyMin;

@Size(max=30)
String stringUsingSizeOnlyMax,
String stringUsingSizeOnlyMax;

@Pattern(regexp = "_stringUsingPatternA|_stringUsingPatternB")
String stringUsingPattern,
String stringUsingPattern;

@Pattern.List({
@Pattern(regexp = "^_stringUsing.*"),
@Pattern(regexp = ".*PatternList$")
})
String stringUsingPatternList,
String stringUsingPatternList;

@Min(1)
int intMin,
int intMin;
@Max(10)
int intMax,
int intMax;
@Min(1)
double doubleMin,
double doubleMin;
@Max(10)
double doubleMax,
double doubleMax;
@DecimalMin("1.5")
double decimalMin,
double decimalMin;
@DecimalMax("2.5")
double decimalMax,
double decimalMax;

@Email
String email
)
{

String email;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,36 @@
import com.kjetland.jackson.jsonSchema.testData.ClassUsingValidationWithGroups.ValidationGroup1;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.experimental.FieldDefaults;
import lombok.extern.jackson.Jacksonized;

@JsonSchemaInject(json = "{\"injected\":true}", javaxValidationGroups = { ValidationGroup1.class })
public record ClassUsingValidationWithGroups
(
@EqualsAndHashCode @FieldDefaults(level = AccessLevel.PUBLIC, makeFinal = true) @Jacksonized @Builder // Can be Java 17 record
public class ClassUsingValidationWithGroups
{
@NotNull
@JsonSchemaInject(json = "{\"injected\":true}")
String noGroup,
String noGroup;

@NotNull(groups = { Default.class })
@JsonSchemaInject(json = "{\"injected\":true}", javaxValidationGroups = { Default.class })
String defaultGroup,
String defaultGroup;

@NotNull(groups = { ValidationGroup1.class })
@JsonSchemaInject(json = "{\"injected\":true}", javaxValidationGroups = { ValidationGroup1.class })
String group1,
String group1;

@NotNull(groups = { ValidationGroup2.class })
@JsonSchemaInject(json = "{\"injected\":true}", javaxValidationGroups = { ValidationGroup2.class })
String group2,
String group2;

@NotNull(groups = { ValidationGroup1.class, ValidationGroup2.class })
@JsonSchemaInject(json = "{\"injected\":true}", javaxValidationGroups = { ValidationGroup1.class, ValidationGroup2.class })
String group12
)
{
String group12;

public interface ValidationGroup1 {}
public interface ValidationGroup2 {}
public interface ValidationGroup3_notInUse {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaDefault;
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaExamples;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.experimental.FieldDefaults;
import lombok.extern.jackson.Jacksonized;


public record DefaultAndExamples
(
@FieldDefaults(level = AccessLevel.PUBLIC, makeFinal = true) @Jacksonized @Builder @EqualsAndHashCode // Can be Java 17 record
public class DefaultAndExamples
{
@JsonSchemaExamples({"[email protected]"})
String emailValue,
String emailValue;

@JsonSchemaDefault("12")
@JsonSchemaExamples({"10", "14", "18"})
int fontSize,
int fontSize;

@JsonProperty(defaultValue = "ds")
String defaultStringViaJsonValue,
String defaultStringViaJsonValue;

@JsonProperty(defaultValue = "1")
int defaultIntViaJsonValue,
int defaultIntViaJsonValue;

@JsonProperty(defaultValue = "true")
boolean defaultBoolViaJsonValue
)
{
boolean defaultBoolViaJsonValue;
}
Loading

0 comments on commit 11948a8

Please sign in to comment.