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

create defaultvocabulary #15

Merged
merged 1 commit into from
Sep 25, 2023
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 @@ -28,7 +28,7 @@

import io.github.sebastiantoepfer.jsonschema.Vocabulary;
import io.github.sebastiantoepfer.jsonschema.core.vocab.basic.BasicVocabulary;
import io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreVocabulary;
import io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreLazyVocabulary;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinition;
import jakarta.json.JsonValue;
Expand All @@ -49,7 +49,7 @@ final class Keywords {
static {
MANDANTORY_VOCABS =
List
.of(new BasicVocabulary(), new CoreVocabulary())
.of(new BasicVocabulary(), new CoreLazyVocabulary().vocab())
.stream()
.collect(toMap(Vocabulary::id, Function.identity()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,32 @@
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import io.github.sebastiantoepfer.jsonschema.Vocabulary;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.DefaultVocabulary;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.LazyVocabularies;
import java.net.URI;
import java.util.Objects;
import java.util.Optional;

public final class LazyCoreVocabulary implements LazyVocabularies {
public final class CoreLazyVocabulary implements LazyVocabularies {

private final Vocabulary vocab;

public LazyCoreVocabulary() {
this.vocab = new CoreVocabulary();
public CoreLazyVocabulary() {
this.vocab =
new DefaultVocabulary(
URI.create("https://json-schema.org/draft/2020-12/vocab/core"),
new SchemaKeywordType(),
new VocabularyKeywordType(),
new IdKeywordType(),
new RefKeywordType(),
new DynamicRefKeywordType(),
new DefsKeywordType(),
new CommentKeywordType()
);
}

public Vocabulary vocab() {
return vocab;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
with io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;

provides io.github.sebastiantoepfer.jsonschema.vocabulary.spi.LazyVocabularies
with io.github.sebastiantoepfer.jsonschema.core.vocab.core.LazyCoreVocabulary;
with io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreLazyVocabulary;
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.github.sebastiantoepfer.jsonschema.core.vocab.core.LazyCoreVocabulary
io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreLazyVocabulary
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.github.sebastiantoepfer.jsonschema.core.vocab.basic.BasicVocabulary;
import io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreVocabulary;
import io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreLazyVocabulary;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinition;
import java.net.URI;
import java.util.Collection;
Expand All @@ -42,7 +42,7 @@ class KeywordsTest {
@Test
void should_not_be_createbale_without_mandantory_core_vocabulary() {
final Collection<VocabularyDefinition> vocabDefs = List.of(
new VocabularyDefinition(new CoreVocabulary().id(), false)
new VocabularyDefinition(new CoreLazyVocabulary().vocab().id(), false)
);
assertThrows(IllegalArgumentException.class, () -> new Keywords(vocabDefs));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,13 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import com.github.npathai.hamcrestopt.OptionalMatchers;
import io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreVocabulary;
import jakarta.json.JsonValue;
import java.net.URI;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.jupiter.api.Test;

class CoreVocabularyTest {
class CommentKeywordTypeTest {

void should_return_the_uri_of_the_current_core_vocabulary() {
assertThat(new CoreVocabulary().id(), is(URI.create("https://json-schema.org/draft/2020-12/vocab/core")));
}

@ParameterizedTest(name = "should know keyword {0}")
@ValueSource(strings = { "$schema", "$vocabulary", "$id", "$ref", "$dynamicRef", "$defs", "$comment" })
void should_return_keywords_for_name(final String name) {
//keyword creation for easier pitesting :) -> i know it is bad
assertThat(
new CoreVocabulary()
.findKeywordTypeByName(name)
.map(keywordType -> keywordType.createKeyword(JsonValue.EMPTY_JSON_OBJECT))
.map(keyword -> keyword.hasName(name)),
OptionalMatchers.isPresentAndIs(true)
);
@Test
void should_create_keyword_with_name() {
assertThat(new CommentKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$comment"), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@
import java.net.URI;
import org.junit.jupiter.api.Test;

class LazyCoreVocabularyTest {
class CoreLazyVocabularyTest {

@Test
void should_return_empty_for_non_core_id() {
assertThat(new LazyCoreVocabulary().loadVocabularyWithId(URI.create("")), isEmpty());
assertThat(new CoreLazyVocabulary().loadVocabularyWithId(URI.create("")), isEmpty());
}

@Test
void should_return_core_vocabulary_for_core_id() {
assertThat(
new LazyCoreVocabulary()
new CoreLazyVocabulary()
.loadVocabularyWithId(URI.create("https://json-schema.org/draft/2020-12/vocab/core"))
.map(Vocabulary::id),
isPresentAndIs(URI.create("https://json-schema.org/draft/2020-12/vocab/core"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class DefsKeywordTypeTest {

@Test
void should_create_keyword_with_name() {
assertThat(new DefsKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$defs"), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import io.github.sebastiantoepfer.jsonschema.core.vocab.core.DynamicRefKeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class DynamicRefKeywordTypeTest {

@Test
void should_create_keyword_with_name() {
assertThat(
new DynamicRefKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$dynamicRef"),
is(true)
);
}

@Test
void notFinischedYet() {
final Keyword keyword = new DynamicRefKeywordType().createKeyword(JsonValue.FALSE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class IdKeywordTypeTest {

@Test
void should_create_keyword_with_name() {
assertThat(new IdKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$id"), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import io.github.sebastiantoepfer.jsonschema.core.vocab.core.RefKeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class RefKeywordTypeTest {

@Test
void should_create_keyword_with_name() {
assertThat(new RefKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$ref"), is(true));
}

@Test
void notFinischedYet() {
final Keyword keyword = new RefKeywordType().createKeyword(JsonValue.FALSE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class SchemaKeywordTypeTest {

@Test
void should_create_keyword_with_name() {
assertThat(new SchemaKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$schema"), is(true));
}
}
6 changes: 6 additions & 0 deletions vocabulary-spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@
<artifactId>hamcrest-optional</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,38 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;
package io.github.sebastiantoepfer.jsonschema.vocabulary.spi;

import io.github.sebastiantoepfer.jsonschema.Vocabulary;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* see: https://json-schema.org/draft/2020-12/json-schema-core.html#name-the-json-schema-core-vocabu
*/
public final class CoreVocabulary implements Vocabulary {
public final class DefaultVocabulary implements Vocabulary {

private final URI id;
private final List<KeywordType> keywords;

private Collection<KeywordType> supportedKeywords = List.of(
new SchemaKeywordType(),
new VocabularyKeywordType(),
new IdKeywordType(),
new RefKeywordType(),
new DynamicRefKeywordType(),
new DefsKeywordType(),
new CommentKeywordType()
);
public DefaultVocabulary(final URI id, final KeywordType... keywortds) {
this(id, Arrays.asList(keywortds));
}

public DefaultVocabulary(final URI id, final Collection<KeywordType> keywords) {
this.id = Objects.requireNonNull(id);
this.keywords = List.copyOf(keywords);
}

@Override
public URI id() {
//we need a way to find out which draft we are using!
return URI.create("https://json-schema.org/draft/2020-12/vocab/core");
return id;
}

@Override
public Optional<KeywordType> findKeywordTypeByName(final String name) {
return supportedKeywords.stream().filter(keywordType -> keywordType.hasName(name)).findFirst();
return keywords.stream().filter(keywordType -> keywordType.hasName(name)).findFirst();
}
}
Loading