Repository: networknt/json-schema-validator
Pull request #647 (2023-02-01 16:11:12)

Use Javadoc badge with dynamic version instead of plain link in README


Patch:
diff --git a/README.md b/README.md
index 7851036..6663db8 100644
--- a/README.md
+++ b/README.md
@@ -4,12 +4,12 @@
 [Subreddit](https://www.reddit.com/r/lightapi/) |
 [Youtube](https://www.youtube.com/channel/UCHCRMWJVXw8iB7zKxF55Byw) |
 [Documentation](https://doc.networknt.com/library/json-schema-validator/) |
-[Javadocs](https://www.javadoc.io/doc/com.networknt/json-schema-validator) |
 [Contribution Guide](https://doc.networknt.com/contribute/) |
 
 [![CI](https://github.com/networknt/json-schema-validator/actions/workflows/ci.yml/badge.svg)](https://github.com/networknt/json-schema-validator/actions/workflows/ci.yml)
 [![Maven Central](https://img.shields.io/maven-central/v/com.networknt/json-schema-validator.svg)](http://search.maven.org/#search%7Cga%7C1%7Cg%3Acom.networknt%20a%3Ajson-schema-validator)
 [![codecov.io](https://codecov.io/github/networknt/json-schema-validator/coverage.svg?branch=master)](https://codecov.io/github/networknt/json-schema-validator?branch=master)
+[![Javadocs](http://www.javadoc.io/badge/com.networknt/json-schema-validator.svg)](https://www.javadoc.io/doc/com.networknt/json-schema-validator)
 
 
 This is a Java implementation of the [JSON Schema Core Draft v4, v6, v7, v2019-09 and v2020-12](http://json-schema.org/latest/json-schema-core.html) specification for JSON schema validation. In addition, it also works for OpenAPI 3.0 request/response validation with some [configuration flags](doc/config.md). For users who want to collect information from a JSON node based on the schema, the [walkers](doc/walkers.md) can help. The default JSON parser is the [Jackson](https://github.com/FasterXML/jackson) that is the most popular one. As it is a key component in our [light-4j](https://github.com/networknt/light-4j) microservices framework to validate request/response against OpenAPI specification for [light-rest-4j](http://www.networknt.com/style/light-rest-4j/) and RPC schema for [light-hybrid-4j](http://www.networknt.com/style/light-hybrid-4j/) at runtime, performance is the most important aspect in the design. 
diff --git a/src/main/java/com/networknt/schema/SpecVersionDetector.java b/src/main/java/com/networknt/schema/SpecVersionDetector.java
index 1d70841..7be7ac2 100644
--- a/src/main/java/com/networknt/schema/SpecVersionDetector.java
+++ b/src/main/java/com/networknt/schema/SpecVersionDetector.java
@@ -18,49 +18,69 @@ package com.networknt.schema;
 
 import com.fasterxml.jackson.databind.JsonNode;
 
+import java.util.Optional;
+
 /**
  * This class is used to detect schema version
  *
  * @author Subhajitdas298
  * @since 25/06/20
  */
-public class SpecVersionDetector {
+public final class SpecVersionDetector {
 
     private static final String SCHEMA_TAG = "$schema";
 
+    private SpecVersionDetector() {
+        // Prevent instantiation of this utility class
+    }
+
     /**
-     * Detects schema version based on the schema tag
+     * Detects schema version based on the schema tag: if the schema tag is not present, throws
+     * {@link JsonSchemaException} with the corresponding message, otherwise - returns the detected spec version.
      *
-     * @param jsonNode Json Node to read from
-     * @return Spec version
+     * @param jsonNode JSON Node to read from
+     * @return Spec version if present, otherwise throws an exception
      */
     public static SpecVersion.VersionFlag detect(JsonNode jsonNode) {
-        JsonNode schemaTag = jsonNode.get(SCHEMA_TAG);
-        if (schemaTag == null) {
-            throw new JsonSchemaException("'" + SCHEMA_TAG + "' tag is not present");
-        }
+        return detectOptionalVersion(jsonNode).orElseThrow(
+                () -> new JsonSchemaException("'" + SCHEMA_TAG + "' tag is not present")
+        );
+    }
+
+    /**
+     * Detects schema version based on the schema tag: if the schema tag is not present, returns an empty {@link
+     * Optional} value, otherwise - returns the detected spec version wrapped into {@link Optional}.
+     *
+     * @param jsonNode JSON Node to read from
+     * @return Spec version if present, otherwise empty
+     */
+    public static Optional<SpecVersion.VersionFlag> detectOptionalVersion(JsonNode jsonNode) {
+        return Optional.ofNullable(jsonNode.get(SCHEMA_TAG)).map(schemaTag -> {
+
+            final boolean forceHttps = true;
+            final boolean removeEmptyFragmentSuffix = true;
 
-        final boolean forceHttps = true;
-        final boolean removeEmptyFragmentSuffix = true;
+            String schemaTagValue = schemaTag.asText();
+            String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(schemaTagValue, forceHttps,
+                    removeEmptyFragmentSuffix);
 
-        String schemaTagValue = schemaTag.asText();
-        String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(schemaTagValue, forceHttps, removeEmptyFragmentSuffix);
-        if (schemaUri.equals(JsonMetaSchema.getV4().getUri())) {
-            return SpecVersion.VersionFlag.V4;
-        }
-        if (schemaUri.equals(JsonMetaSchema.getV6().getUri())) {
-            return SpecVersion.VersionFlag.V6;
-        }
-        if (schemaUri.equals(JsonMetaSchema.getV7().getUri())) {
-            return SpecVersion.VersionFlag.V7;
-        }
-        if (schemaUri.equals(JsonMetaSchema.getV201909().getUri())) {
-            return SpecVersion.VersionFlag.V201909;
-        }
-        if (schemaUri.equals(JsonMetaSchema.getV202012().getUri())) {
-            return SpecVersion.VersionFlag.V202012;
-        }
-        throw new JsonSchemaException("'" + schemaTagValue + "' is unrecognizable schema");
+            if (schemaUri.equals(JsonMetaSchema.getV4().getUri())) {
+                return SpecVersion.VersionFlag.V4;
+            }
+            if (schemaUri.equals(JsonMetaSchema.getV6().getUri())) {
+                return SpecVersion.VersionFlag.V6;
+            }
+            if (schemaUri.equals(JsonMetaSchema.getV7().getUri())) {
+                return SpecVersion.VersionFlag.V7;
+            }
+            if (schemaUri.equals(JsonMetaSchema.getV201909().getUri())) {
+                return SpecVersion.VersionFlag.V201909;
+            }
+            if (schemaUri.equals(JsonMetaSchema.getV202012().getUri())) {
+                return SpecVersion.VersionFlag.V202012;
+            }
+            throw new JsonSchemaException("'" + schemaTagValue + "' is unrecognizable schema");
+        });
     }
 
 }


Test patch:
diff --git a/src/test/java/com/networknt/schema/SpecVersionDetectorTest.java b/src/test/java/com/networknt/schema/SpecVersionDetectorTest.java
index 6433709..13a689f 100644
--- a/src/test/java/com/networknt/schema/SpecVersionDetectorTest.java
+++ b/src/test/java/com/networknt/schema/SpecVersionDetectorTest.java
@@ -2,11 +2,13 @@ package com.networknt.schema;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.CsvSource;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Optional;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -42,4 +44,13 @@ class SpecVersionDetectorTest {
         JsonSchemaException exception = assertThrows(JsonSchemaException.class, () -> SpecVersionDetector.detect(node));
         assertEquals(expectedError, exception.getMessage());
     }
-}
\ No newline at end of file
+
+    @Test
+    void detectOptionalSpecVersion() throws IOException {
+        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(
+                "data/schemaTagMissing.json");
+        JsonNode node = mapper.readTree(in);
+        Optional<SpecVersion.VersionFlag> flag = SpecVersionDetector.detectOptionalVersion(node);
+        assertEquals(Optional.empty(), flag);
+    }
+}