Skip to content

Commit ebc26c8

Browse files
authored
Add notes about thread-safety to the documentation (#229)
1 parent d1fbcfb commit ebc26c8

File tree

9 files changed

+27
-8
lines changed

9 files changed

+27
-8
lines changed

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,11 @@ val valid = schema.validate(elementToValidate, errors::add)
351351
| | not | Supported |
352352
</details>
353353

354+
## Thread safety
355+
356+
All public API is thread-safe unless stated otherwise.
357+
Please, read the documentation for each class/interface carefully before using an instance from different threads.
358+
354359
## Format assertion
355360

356361
The library supports `format` assertion.

Diff for: json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/AnnotationKey.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import kotlin.reflect.KClass
1414
*}
1515
* ```
1616
*/
17-
public sealed class AnnotationKey<T : Any> private constructor(
17+
public sealed class AnnotationKey<T : Any>(
1818
private val name: String,
1919
internal val type: KClass<T>,
2020
) {

Diff for: json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/FormatValidator.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import kotlin.jvm.JvmStatic
77
* The [FormatValidator] is used to check whether the [AbstractElement] matches the expected format.
88
* If the [AbstractElement] is not of the required type
99
* (e.g. validator expects string but the [AbstractElement] is an object)
10-
* the validator **MUST** return [FormatValidator.Valid] result
10+
* the validator **MUST** return [FormatValidator.Valid] result.
11+
*
12+
* If you create an implementation of [FormatValidator] that will be shared with others
13+
* please make sure that it will be state-less since it might be invoked from different threads.
1114
*/
1215
@ExperimentalApi
1316
public interface FormatValidator {

Diff for: json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/JsonSchemaLoader.kt

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import io.github.optimumcode.json.schema.internal.wellknown.Draft7
1616
import kotlinx.serialization.json.JsonElement
1717
import kotlin.jvm.JvmStatic
1818

19+
/**
20+
* By default, implementations of [JsonSchemaLoader] are NOT thread-safe
21+
*/
1922
@Suppress("detekt:TooManyFunctions")
2023
public interface JsonSchemaLoader {
2124
public fun registerWellKnown(draft: SchemaType): JsonSchemaLoader =
@@ -119,6 +122,11 @@ public interface JsonSchemaLoader {
119122
): JsonSchema
120123

121124
public companion object {
125+
/**
126+
* Creates an instance of [JsonSchemaLoader].
127+
*
128+
* @return implementation of [JsonSchemaLoader]. The implementation is **NOT thread-safe**.
129+
*/
122130
@JvmStatic
123131
public fun create(): JsonSchemaLoader = SchemaLoader()
124132
}

Diff for: json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/OutputCollector.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ private val NO_TRANSFORMATION: OutputErrorTransformer<*> = { it }
1313
/**
1414
* Provides collectors' implementations for outputs
1515
* defined in [draft 2020-12](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-01#section-12.4)
16+
*
17+
* **The implementations of [OutputCollector] are NOT thread-safe**.
1618
*/
17-
public sealed class OutputCollector<T> private constructor(
19+
public sealed class OutputCollector<T>(
1820
parent: OutputCollector<T>? = null,
1921
transformer: OutputErrorTransformer<T> = NO_TRANSFORMATION,
2022
) {

Diff for: json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/ValidationOutput.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import kotlinx.serialization.Serializable
55
import kotlinx.serialization.Transient
66
import kotlin.jvm.JvmField
77

8-
public sealed class ValidationOutput private constructor() {
8+
public sealed class ValidationOutput {
99
public abstract val valid: Boolean
1010

1111
@Serializable

Diff for: json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/extension/ExternalAssertion.kt

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import kotlinx.serialization.json.JsonElement
1010
* This interface allows you to implement your own schema assertion.
1111
* This interface **does not** allow implementing custom applicators.
1212
* Only simple assertions (like: _format_, _type_) can be implemented.
13+
*
14+
* If you create an implementation of [ExternalAssertion] that will be shared with others
15+
* please make sure that it will be state-less since it might be invoked from different threads.
1316
*/
1417
@Suppress("detekt:ForbiddenComment")
1518
@ExperimentalApi

Diff for: json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/extension/ExternalAssertionFactory.kt

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ public interface ExternalAssertionFactory {
1010
* This keyword **must not** overlap with any existing keywords for existing drafts.
1111
* If keyword overlaps with any keyword for any existing draft and [IllegalStateException] will be thrown
1212
* when this factory is registered in [io.github.optimumcode.json.schema.JsonSchemaLoader].
13-
*
14-
* NOTE: currently the library does not have **format** assertion implemented. But it will have.
15-
* If you decide to implement it as an [ExternalAssertion] please be aware
16-
* that one day this will cause an [IllegalStateException] as it was added to the library itself
1713
*/
1814
public val keywordName: String
1915

Diff for: json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/model/Model.kt

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import io.github.optimumcode.json.schema.ExperimentalApi
77
* * [ObjectElement] - corresponds to a JSON/YAML object or [Map]
88
* * [ArrayElement] - corresponds to a collection of [AbstractElement]s
99
* * [PrimitiveElement] - corresponds to a scalar value (string, number, boolean)
10+
*
11+
* The implementations are NOT required to be thread-safe.
1012
*/
1113
@ExperimentalApi
1214
public sealed interface AbstractElement {

0 commit comments

Comments
 (0)