• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

torand / jsonschema2java / 22553968753

01 Mar 2026 10:11PM UTC coverage: 83.689% (+1.3%) from 82.424%
22553968753

push

github

torand
feat: add config parameter 'durationClassName' to specify a custom class to represent string schemas with format "duration" in generated code

328 of 433 branches covered (75.75%)

Branch coverage included in aggregate %.

16 of 16 new or added lines in 5 files covered. (100.0%)

41 existing lines in 6 files now uncovered.

888 of 1020 relevant lines covered (87.06%)

5.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.42
/src/main/java/io/github/torand/jsonschema2java/collectors/Extensions.java
1
/*
2
 * Copyright (c) 2024-2026 Tore Eide Andersen
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package io.github.torand.jsonschema2java.collectors;
17

18
import io.github.torand.jsonschema2java.utils.JsonSchema2JavaException;
19
import tools.jackson.databind.node.BooleanNode;
20
import tools.jackson.databind.node.StringNode;
21

22
import java.util.Map;
23
import java.util.Optional;
24
import java.util.Set;
25

26
import static io.github.torand.javacommons.lang.StringHelper.nonBlank;
27
import static java.util.Collections.emptyMap;
28
import static java.util.Objects.isNull;
29
import static java.util.Objects.nonNull;
30

31
/**
32
 * Handles custom JSON Schema extensions.
33
 */
34
public class Extensions {
35
    /**
36
     * Fully qualified classname of a JSON serializer class for the schema.
37
     */
38
    public static final String EXT_JSON_SERIALIZER = "x-json-serializer";
39

40
    /**
41
     * Fully qualified classname of a Jackson deserializer class for the schema.
42
     */
43
    public static final String EXT_JSON_DESERIALIZER = "x-json-deserializer";
44

45
    /**
46
     * Custom date/time format pattern for Jackson java.time.* deserializers/serializers.
47
     */
48
    public static final String EXT_JSON_FORMAT = "x-json-format";
49

50
    /**
51
     * Fully qualified classname of an annotation class to validate the schema.
52
     */
53
    public static final String EXT_VALIDATION_CONSTRAINT = "x-validation-constraint";
54

55
    /**
56
     * If `true` the type of the schema/property can be `null`.
57
     */
58
    public static final String EXT_NULLABLE = "x-nullable";
59

60
    /**
61
     * Subdirectory to place the generated DTO model class.
62
     */
63
    public static final String EXT_MODEL_SUBDIR = "x-model-subdir";
64

65
    /**
66
     * Describing why something is deprecated, and what to use instead.
67
     */
68
    public static final String EXT_DEPRECATION_MESSAGE = "x-deprecation-message";
69

70
    public static final Set<String> KEYWORDS = Set.of(
10✔
71
        EXT_JSON_SERIALIZER,
72
        EXT_JSON_DESERIALIZER,
73
        EXT_JSON_FORMAT,
74
        EXT_VALIDATION_CONSTRAINT,
75
        EXT_NULLABLE,
76
        EXT_MODEL_SUBDIR,
77
        EXT_DEPRECATION_MESSAGE
78
    );
79

80
    private final Map<String, Object> extensionsByName;
81

82
    /**
83
     * Returns an {@link Extensions} object processing the specified JSON Schema extension map.
84
     * @param extensionsByName the JSON Schema extensions.
85
     * @return the {@link Extensions} object.
86
     */
87
    public static Extensions extensions(Map<String, Object> extensionsByName) {
88
        return new Extensions(extensionsByName);
5✔
89
    }
90

91
    private Extensions(Map<String, Object> extensionsByName) {
2✔
92
        this.extensionsByName = nonNull(extensionsByName) ? extensionsByName : emptyMap();
7!
93
    }
1✔
94

95
    /**
96
     * Gets value of a string extension property.
97
     * @param name the extension property name.
98
     * @return the extension property string value, if found; else empty.
99
     */
100
    public Optional<String> getString(String name) {
101
        Object value = extensionsByName.get(name);
5✔
102
        if (isNull(value)) {
3✔
103
            return Optional.empty();
2✔
104
        }
105
        if (!(value instanceof StringNode)) {
3!
UNCOV
106
            throw new JsonSchema2JavaException("Value of extension %s is not a String".formatted(name));
×
107
        }
108
        if (nonBlank(((StringNode)value).asText())) {
5!
109
            return Optional.of(((StringNode)value).asText());
5✔
110
        }
111

UNCOV
112
        return Optional.empty();
×
113
    }
114

115
    /**
116
     * Gets value of a boolean extension property.
117
     * @param name the extension property name.
118
     * @return the extension property value, if found; else empty.
119
     */
120
    public Optional<Boolean> getBoolean(String name) {
121
        Object value = extensionsByName.get(name);
5✔
122
        if (isNull(value)) {
3✔
123
            return Optional.empty();
2✔
124
        }
125
        if (!(value instanceof BooleanNode)) {
3!
UNCOV
126
            throw new JsonSchema2JavaException("Value of extension %s is not a Boolean".formatted(name));
×
127
        }
128

129
        return Optional.of(((BooleanNode)value).asBoolean());
6✔
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc