Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 9313454

Browse files
committed
Updates IntJsonSchema
1 parent b71e163 commit 9313454

2 files changed

Lines changed: 143 additions & 149 deletions

File tree

  • samples/client/petstore/kotlin/src/main/kotlin/org/openapijsonschematools/client/schemas
  • src/main/resources/kotlin/src/main/kotlin/packagename/schemas
Lines changed: 76 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,105 @@
1-
package org.openapijsonschematools.client.schemas;
1+
package org.openapijsonschematools.client.schemas
22

3-
import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags;
4-
import org.openapijsonschematools.client.exceptions.ValidationException;
5-
import org.openapijsonschematools.client.schemas.validation.JsonSchema;
6-
import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo;
7-
import org.openapijsonschematools.client.configurations.SchemaConfiguration;
8-
import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap;
9-
import org.openapijsonschematools.client.schemas.validation.NumberSchemaValidator;
10-
import org.openapijsonschematools.client.schemas.validation.ValidationMetadata;
11-
import org.checkerframework.checker.nullness.qual.Nullable;
3+
import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags
4+
import org.openapijsonschematools.client.configurations.SchemaConfiguration
5+
import org.openapijsonschematools.client.exceptions.ValidationException
6+
import org.openapijsonschematools.client.schemas.validation.JsonSchema
7+
import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo
8+
import org.openapijsonschematools.client.schemas.validation.NumberSchemaValidator
9+
import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap
10+
import org.openapijsonschematools.client.schemas.validation.ValidationMetadata
1211

13-
import java.util.HashSet;
14-
import java.util.LinkedHashSet;
15-
import java.util.List;
16-
import java.util.Objects;
17-
import java.util.Set;
18-
19-
public class IntJsonSchema {
20-
public sealed interface IntJsonSchema1Boxed permits IntJsonSchema1BoxedNumber {
21-
@Nullable Object getData();
12+
class IntJsonSchema {
13+
sealed interface IntJsonSchema1Boxed {
14+
fun getData(): Any?
2215
}
23-
public record IntJsonSchema1BoxedNumber(Number data) implements IntJsonSchema1Boxed {
24-
@Override
25-
public @Nullable Object getData() {
26-
return data;
16+
17+
data class IntJsonSchema1BoxedNumber(val data: Number) : IntJsonSchema1Boxed {
18+
override fun getData(): Number {
19+
return data
2720
}
2821
}
2922

30-
public static class IntJsonSchema1 extends JsonSchema<IntJsonSchema1Boxed> implements NumberSchemaValidator<IntJsonSchema1BoxedNumber> {
31-
private static @Nullable IntJsonSchema1 instance = null;
32-
33-
protected IntJsonSchema1() {
34-
super(new JsonSchemaInfo()
35-
.type(Set.of(
36-
Integer.class,
37-
Long.class,
38-
Float.class,
39-
Double.class
40-
))
41-
.format("int")
42-
);
23+
class IntJsonSchema1 private constructor() : JsonSchema<IntJsonSchema1Boxed>(
24+
JsonSchemaInfo()
25+
.type(
26+
setOf(
27+
Int::class.java,
28+
Long::class.java,
29+
Float::class.java,
30+
Double::class.java
31+
)
32+
)
33+
.format("int")
34+
), NumberSchemaValidator<IntJsonSchema1BoxedNumber> {
35+
@Throws(ValidationException::class)
36+
override fun validate(arg: Number, configuration: SchemaConfiguration?): Number {
37+
val pathSet: MutableSet<List<Any>> = HashSet()
38+
val pathToItem = listOf<Any>("args[0")
39+
val castArg: Number = castToAllowedTypes(arg, pathToItem, pathSet)
40+
val usedConfiguration = configuration ?: SchemaConfiguration(JsonSchemaKeywordFlags.Builder().build())
41+
val validationMetadata =
42+
ValidationMetadata(pathToItem, usedConfiguration, PathToSchemasMap(), LinkedHashSet())
43+
val pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet)
44+
return getNewInstance(castArg, validationMetadata.pathToItem, pathToSchemasMap)
4345
}
4446

45-
public static IntJsonSchema1 getInstance() {
46-
if (instance == null) {
47-
instance = new IntJsonSchema1();
48-
}
49-
return instance;
47+
@Throws(ValidationException::class)
48+
fun validate(arg: Int, configuration: SchemaConfiguration?): Int {
49+
return validate(arg as Number, configuration) as Int
5050
}
5151

52-
@Override
53-
public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException {
54-
Set<List<Object>> pathSet = new HashSet<>();
55-
List<Object> pathToItem = List.of("args[0");
56-
Number castArg = castToAllowedTypes(arg, pathToItem, pathSet);
57-
SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(new JsonSchemaKeywordFlags.Builder().build()));
58-
ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>());
59-
PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet);
60-
return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap);
52+
@Throws(ValidationException::class)
53+
fun validate(arg: Long, configuration: SchemaConfiguration?): Long {
54+
return validate(arg as Number, configuration) as Long
6155
}
6256

63-
public int validate(int arg, SchemaConfiguration configuration) throws ValidationException {
64-
return (int) validate((Number) arg, configuration);
57+
@Throws(ValidationException::class)
58+
fun validate(arg: Float, configuration: SchemaConfiguration?): Float {
59+
return validate(arg as Number, configuration) as Float
6560
}
6661

67-
public long validate(long arg, SchemaConfiguration configuration) throws ValidationException {
68-
return (long) validate((Number) arg, configuration);
62+
@Throws(ValidationException::class)
63+
fun validate(arg: Double, configuration: SchemaConfiguration?): Double {
64+
return validate(arg as Number, configuration) as Double
6965
}
7066

71-
public float validate(float arg, SchemaConfiguration configuration) throws ValidationException {
72-
return (float) validate((Number) arg, configuration);
67+
override fun getNewInstance(arg: Any?, pathToItem: List<Any>, pathToSchemas: PathToSchemasMap): Any? {
68+
if (arg is Number) {
69+
return getNewInstance(arg as Number?, pathToItem, pathToSchemas)
70+
}
71+
throw RuntimeException("Invalid input type=$javaClass. It can't be instantiated by this schema")
7372
}
7473

75-
public double validate(double arg, SchemaConfiguration configuration) throws ValidationException {
76-
return (double) validate((Number) arg, configuration);
74+
@Throws(ValidationException::class)
75+
override fun validate(arg: Any?, configuration: SchemaConfiguration?): Number {
76+
if (arg is Number) {
77+
return validate(arg, configuration)
78+
}
79+
throw ValidationException("Invalid input type=$javaClass. It can't be validated by this schema")
7780
}
7881

79-
@Override
80-
public @Nullable Object getNewInstance(@Nullable Object arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
81-
if (arg instanceof Number) {
82-
return getNewInstance((Number) arg, pathToItem, pathToSchemas);
83-
}
84-
throw new RuntimeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema");
82+
@Throws(ValidationException::class)
83+
override fun validateAndBox(arg: Number, configuration: SchemaConfiguration?): IntJsonSchema1BoxedNumber {
84+
return IntJsonSchema1BoxedNumber(validate(arg, configuration))
8585
}
8686

87-
@Override
88-
public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException {
89-
if (arg instanceof Number) {
90-
return validate((Number) arg, configuration);
87+
@Throws(ValidationException::class)
88+
override fun validateAndBox(arg: Any?, configuration: SchemaConfiguration?): IntJsonSchema1Boxed {
89+
if (arg is Number) {
90+
return validateAndBox(arg, configuration)
9191
}
92-
throw new ValidationException("Invalid input type="+getClass(arg)+". It can't be validated by this schema");
92+
throw ValidationException("Invalid input type=$javaClass. It can't be validated by this schema")
9393
}
9494

95-
@Override
96-
public IntJsonSchema1BoxedNumber validateAndBox(Number arg, SchemaConfiguration configuration) throws ValidationException {
97-
return new IntJsonSchema1BoxedNumber(validate(arg, configuration));
98-
}
95+
companion object {
96+
@Volatile
97+
private var instance: IntJsonSchema1? = null
9998

100-
@Override
101-
public IntJsonSchema1Boxed validateAndBox(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException {
102-
if (arg instanceof Number castArg) {
103-
return validateAndBox(castArg, configuration);
104-
}
105-
throw new ValidationException("Invalid input type="+getClass(arg)+". It can't be validated by this schema");
99+
fun getInstance() =
100+
instance ?: synchronized(this) {
101+
instance ?: IntJsonSchema1().also { instance = it }
102+
}
106103
}
107104
}
108105
}

src/main/resources/kotlin/src/main/kotlin/packagename/schemas/IntJsonSchema.hbs

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,101 +8,98 @@ import {{{packageName}}}.configurations.SchemaConfiguration;
88
import {{{packageName}}}.schemas.validation.PathToSchemasMap;
99
import {{{packageName}}}.schemas.validation.NumberSchemaValidator;
1010
import {{{packageName}}}.schemas.validation.ValidationMetadata;
11-
import org.checkerframework.checker.nullness.qual.Nullable;
1211

13-
import java.util.HashSet;
14-
import java.util.LinkedHashSet;
15-
import java.util.List;
16-
import java.util.Objects;
17-
import java.util.Set;
18-
19-
public class IntJsonSchema {
20-
public sealed interface IntJsonSchema1Boxed permits IntJsonSchema1BoxedNumber {
21-
@Nullable Object getData();
12+
class IntJsonSchema {
13+
sealed interface IntJsonSchema1Boxed {
14+
fun getData(): Any?
2215
}
23-
public record IntJsonSchema1BoxedNumber(Number data) implements IntJsonSchema1Boxed {
24-
@Override
25-
public @Nullable Object getData() {
26-
return data;
16+
17+
data class IntJsonSchema1BoxedNumber(val data: Number) : IntJsonSchema1Boxed {
18+
override fun getData(): Number {
19+
return data
2720
}
2821
}
2922

30-
public static class IntJsonSchema1 extends JsonSchema<IntJsonSchema1Boxed> implements NumberSchemaValidator<IntJsonSchema1BoxedNumber> {
31-
private static @Nullable IntJsonSchema1 instance = null;
32-
33-
protected IntJsonSchema1() {
34-
super(new JsonSchemaInfo()
35-
.type(Set.of(
36-
Integer.class,
37-
Long.class,
38-
Float.class,
39-
Double.class
40-
))
41-
.format("int")
42-
);
23+
class IntJsonSchema1 private constructor() : JsonSchema<IntJsonSchema1Boxed>(
24+
JsonSchemaInfo()
25+
.type(
26+
setOf(
27+
Int::class.java,
28+
Long::class.java,
29+
Float::class.java,
30+
Double::class.java
31+
)
32+
)
33+
.format("int")
34+
), NumberSchemaValidator<IntJsonSchema1BoxedNumber> {
35+
@Throws(ValidationException::class)
36+
override fun validate(arg: Number, configuration: SchemaConfiguration?): Number {
37+
val pathSet: MutableSet<List<Any>> = HashSet()
38+
val pathToItem = listOf<Any>("args[0")
39+
val castArg: Number = castToAllowedTypes(arg, pathToItem, pathSet)
40+
val usedConfiguration = configuration ?: SchemaConfiguration(JsonSchemaKeywordFlags.Builder().build())
41+
val validationMetadata =
42+
ValidationMetadata(pathToItem, usedConfiguration, PathToSchemasMap(), LinkedHashSet())
43+
val pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet)
44+
return getNewInstance(castArg, validationMetadata.pathToItem, pathToSchemasMap)
4345
}
4446

45-
public static IntJsonSchema1 getInstance() {
46-
if (instance == null) {
47-
instance = new IntJsonSchema1();
48-
}
49-
return instance;
47+
@Throws(ValidationException::class)
48+
fun validate(arg: Int, configuration: SchemaConfiguration?): Int {
49+
return validate(arg as Number, configuration) as Int
5050
}
5151

52-
@Override
53-
public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException {
54-
Set<List<Object>> pathSet = new HashSet<>();
55-
List<Object> pathToItem = List.of("args[0");
56-
Number castArg = castToAllowedTypes(arg, pathToItem, pathSet);
57-
SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(new JsonSchemaKeywordFlags.Builder().build()));
58-
ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>());
59-
PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet);
60-
return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap);
52+
@Throws(ValidationException::class)
53+
fun validate(arg: Long, configuration: SchemaConfiguration?): Long {
54+
return validate(arg as Number, configuration) as Long
6155
}
6256

63-
public int validate(int arg, SchemaConfiguration configuration) throws ValidationException {
64-
return (int) validate((Number) arg, configuration);
57+
@Throws(ValidationException::class)
58+
fun validate(arg: Float, configuration: SchemaConfiguration?): Float {
59+
return validate(arg as Number, configuration) as Float
6560
}
6661

67-
public long validate(long arg, SchemaConfiguration configuration) throws ValidationException {
68-
return (long) validate((Number) arg, configuration);
62+
@Throws(ValidationException::class)
63+
fun validate(arg: Double, configuration: SchemaConfiguration?): Double {
64+
return validate(arg as Number, configuration) as Double
6965
}
7066

71-
public float validate(float arg, SchemaConfiguration configuration) throws ValidationException {
72-
return (float) validate((Number) arg, configuration);
67+
override fun getNewInstance(arg: Any?, pathToItem: List<Any>, pathToSchemas: PathToSchemasMap): Any? {
68+
if (arg is Number) {
69+
return getNewInstance(arg as Number?, pathToItem, pathToSchemas)
70+
}
71+
throw RuntimeException("Invalid input type=$javaClass. It can't be instantiated by this schema")
7372
}
7473

75-
public double validate(double arg, SchemaConfiguration configuration) throws ValidationException {
76-
return (double) validate((Number) arg, configuration);
74+
@Throws(ValidationException::class)
75+
override fun validate(arg: Any?, configuration: SchemaConfiguration?): Number {
76+
if (arg is Number) {
77+
return validate(arg, configuration)
78+
}
79+
throw ValidationException("Invalid input type=$javaClass. It can't be validated by this schema")
7780
}
7881

79-
@Override
80-
public @Nullable Object getNewInstance(@Nullable Object arg, List<Object> pathToItem, PathToSchemasMap pathToSchemas) {
81-
if (arg instanceof Number) {
82-
return getNewInstance((Number) arg, pathToItem, pathToSchemas);
83-
}
84-
throw new RuntimeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema");
82+
@Throws(ValidationException::class)
83+
override fun validateAndBox(arg: Number, configuration: SchemaConfiguration?): IntJsonSchema1BoxedNumber {
84+
return IntJsonSchema1BoxedNumber(validate(arg, configuration))
8585
}
8686

87-
@Override
88-
public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException {
89-
if (arg instanceof Number) {
90-
return validate((Number) arg, configuration);
87+
@Throws(ValidationException::class)
88+
override fun validateAndBox(arg: Any?, configuration: SchemaConfiguration?): IntJsonSchema1Boxed {
89+
if (arg is Number) {
90+
return validateAndBox(arg, configuration)
9191
}
92-
throw new ValidationException("Invalid input type="+getClass(arg)+". It can't be validated by this schema");
92+
throw ValidationException("Invalid input type=$javaClass. It can't be validated by this schema")
9393
}
9494

95-
@Override
96-
public IntJsonSchema1BoxedNumber validateAndBox(Number arg, SchemaConfiguration configuration) throws ValidationException {
97-
return new IntJsonSchema1BoxedNumber(validate(arg, configuration));
98-
}
95+
companion object {
96+
@Volatile
97+
private var instance: IntJsonSchema1? = null
9998

100-
@Override
101-
public IntJsonSchema1Boxed validateAndBox(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException {
102-
if (arg instanceof Number castArg) {
103-
return validateAndBox(castArg, configuration);
104-
}
105-
throw new ValidationException("Invalid input type="+getClass(arg)+". It can't be validated by this schema");
99+
fun getInstance() =
100+
instance ?: synchronized(this) {
101+
instance ?: IntJsonSchema1().also { instance = it }
102+
}
106103
}
107104
}
108105
}

0 commit comments

Comments
 (0)