Skip to content
Open
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 @@ -8,7 +8,6 @@
package de.ii.xtraplatform.schemas.ext.app;

import java.net.URI;
import java.nio.file.Path;
import net.jimblackler.jsonschemafriend.Schema;
import net.jimblackler.jsonschemafriend.SchemaException;
import net.jimblackler.jsonschemafriend.SchemaStore;
Expand All @@ -20,7 +19,7 @@ public JsonSchemaParser() {
this.schemaStore = new SchemaStore();
}

public Schema parse(Path file) {
public Schema parse(java.nio.file.Path file) {
try {
return schemaStore.loadSchema(file.toFile());
} catch (SchemaException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import de.ii.xtraplatform.schemas.ext.domain.JsonSchemaConfiguration;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -84,6 +83,7 @@
*/
@Singleton
@AutoBind
@SuppressWarnings({"PMD.GodClass", "PMD.CyclomaticComplexity", "PMD.TooManyMethods"})
public class JsonSchemaResolver implements SchemaFragmentResolver, FeatureQueriesExtension {

private static final Logger LOGGER = LoggerFactory.getLogger(JsonSchemaResolver.class);
Expand Down Expand Up @@ -120,9 +120,9 @@ public boolean canResolve(String ref, FeatureProviderDataV2 data) {
URI schemaUri = URI.create(ref);

if (SCHEMES.contains(schemaUri.getScheme())
|| ((Objects.isNull(schemaUri.getScheme())
|| Objects.isNull(schemaUri.getScheme())
&& !schemaUri.getSchemeSpecificPart().isBlank()
&& !schemaUri.getSchemeSpecificPart().startsWith("/")))) {
&& !schemaUri.getSchemeSpecificPart().startsWith("/")) {
return true;
}
} catch (Throwable e) {
Expand Down Expand Up @@ -167,7 +167,7 @@ Optional<Schema> parse(String schemaSource) {
if (Objects.isNull(schemaUri.getScheme())
&& !schemaUri.getSchemeSpecificPart().isBlank()
&& !schemaUri.getSchemeSpecificPart().startsWith("/")) {
Path path = Path.of(schemaUri.getSchemeSpecificPart());
java.nio.file.Path path = java.nio.file.Path.of(schemaUri.getSchemeSpecificPart());

if (!schemaStore.has(path)) {
LOGGER.error("Cannot load schema '{}', not found in 'resources/schemas'.", schemaSource);
Expand Down Expand Up @@ -204,6 +204,7 @@ Optional<Schema> parse(String schemaSource) {
return Optional.empty();
}

@SuppressWarnings("PMD.CognitiveComplexity")
private List<Schema> resolveComposition(Schema schema, FeatureProviderDataV2 data) {
if (Objects.nonNull(schema.getRef())) {
return resolveComposition(schema.getRef(), data);
Expand Down Expand Up @@ -269,11 +270,13 @@ private PartialObjectSchema toPartialSchema(
.from(original)
.schema(Optional.empty())
.propertyMap(Map.of());
Map<String, FeatureSchema> originalPropertyMap =
Objects.nonNull(original) ? original.getPropertyMap() : Map.of();

if (Objects.nonNull(s.getProperties())) {
resolveProperties(
s.getProperties(),
Objects.nonNull(original) ? original.getPropertyMap() : null,
originalPropertyMap,
schema,
builder::putPropertyMap,
data,
Expand All @@ -293,32 +296,37 @@ private PartialObjectSchema toPartialSchema(
return builder.build();
}

@SuppressWarnings({"PMD.CognitiveComplexity", "PMD.NcssCount", "PMD.NPathComplexity"})
private FeatureSchema toFeatureSchema(
String name,
Schema schema,
@Nullable Schema root,
@Nullable FeatureSchema original,
FeatureProviderDataV2 data,
boolean isRequired) {
ImmutableFeatureSchema.Builder builder = new ImmutableFeatureSchema.Builder();
List<Schema> resolved = resolveComposition(schema, data);
Schema s = resolved.get(0);
boolean isRoot = Objects.isNull(root);

Schema r = isRoot ? schema : root;
Type t = toType(s.getExplicitTypes());

if (Objects.equals(s, root)) {
return null;
}

if (Objects.nonNull(original) && original.getIgnore()) {
return null;
}

boolean isRoot = Objects.isNull(root);
Schema r = isRoot ? schema : root;
Type t = toType(s.getExplicitTypes());
ImmutableFeatureSchema.Builder builder = new ImmutableFeatureSchema.Builder();

if (Objects.nonNull(original)) {
if (original.getIgnore()) {
return null;
}
builder.from(original).schema(Optional.empty()).propertyMap(Map.of());
}

Map<String, FeatureSchema> originalPropertyMap =
Objects.nonNull(original) ? original.getPropertyMap() : Map.of();

builder
.name(name)
.label(Optional.ofNullable(schema.getTitle()).or(() -> Optional.ofNullable(s.getTitle())))
Expand All @@ -339,21 +347,14 @@ private FeatureSchema toFeatureSchema(
constrained = true;
}

t =
applyTypeRefs(
s.getUri().toString(),
builder,
t,
Optional.ofNullable(original).flatMap(FeatureSchema::getSourcePath).orElse(name),
isRoot,
data);
t = applyTypeRefs(s.getUri().toString(), builder, t, isRoot, data);

if (t == Type.OBJECT) {
if (Objects.nonNull(s.getProperties()) || resolved.size() > 1) {
if (Objects.nonNull(s.getProperties())) {
resolveProperties(
s.getProperties(),
Objects.nonNull(original) ? original.getPropertyMap() : null,
originalPropertyMap,
r,
builder::putPropertyMap,
data,
Expand All @@ -365,7 +366,7 @@ private FeatureSchema toFeatureSchema(
if (Objects.nonNull(add.getProperties())) {
resolveProperties(
add.getProperties(),
Objects.nonNull(original) ? original.getPropertyMap() : null,
originalPropertyMap,
r,
builder::putPropertyMap,
data,
Expand Down Expand Up @@ -412,11 +413,9 @@ private FeatureSchema toFeatureSchema(
s.getEnums().stream().map(Object::toString).collect(Collectors.toList()));
constrained = true;
}
if (t == Type.STRING) {
if (s.getPattern() != null) {
constraintsBuilder.regex(s.getPattern());
constrained = true;
}
if (t == Type.STRING && s.getPattern() != null) {
constraintsBuilder.regex(s.getPattern());
constrained = true;
}
if (t == Type.INTEGER || t == Type.FLOAT) {
if (s.getMinimum() != null) {
Expand Down Expand Up @@ -461,12 +460,7 @@ private void resolveProperties(
}

private Type applyTypeRefs(
String uri,
Builder builder,
Type type,
String sourcePath,
boolean isRoot,
FeatureProviderDataV2 data) {
String uri, Builder builder, Type type, boolean isRoot, FeatureProviderDataV2 data) {
Optional<JsonSchemaConfiguration> cfg = getConfiguration(data);
if (cfg.isPresent() && (type == Type.OBJECT || type == Type.OBJECT_ARRAY)) {
Optional<String> geometryTypeRef =
Expand Down
Loading