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

torand / jsonschema2java / 18399814123

10 Oct 2025 07:35AM UTC coverage: 78.214% (-0.9%) from 79.157%
18399814123

push

github

torand
fix: bean validation annotations on primitive subtypes of compound pojo property types now generated

287 of 413 branches covered (69.49%)

Branch coverage included in aggregate %.

440 of 533 new or added lines in 26 files covered. (82.55%)

1 existing line in 1 file now uncovered.

808 of 987 relevant lines covered (81.86%)

4.75 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-2025 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 com.fasterxml.jackson.databind.node.BooleanNode;
19
import com.fasterxml.jackson.databind.node.TextNode;
20
import io.github.torand.jsonschema2java.utils.JsonSchema2JavaException;
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 an annotation class to validate the schema.
42
     */
43
    public static final String EXT_VALIDATION_CONSTRAINT = "x-validation-constraint";
44

45
    /**
46
     * If `true` the type of the schema/property can be `null`.
47
     */
48
    public static final String EXT_NULLABLE = "x-nullable";
49

50
    /**
51
     * Subdirectory to place the generated DTO model class.
52
     */
53
    public static final String EXT_MODEL_SUBDIR = "x-model-subdir";
54

55
    /**
56
     * Describing why something is deprecated, and what to use instead.
57
     */
58
    public static final String EXT_DEPRECATION_MESSAGE = "x-deprecation-message";
59

60
    public static final Set<String> KEYWORDS = Set.of(
8✔
61
        EXT_JSON_SERIALIZER,
62
        EXT_VALIDATION_CONSTRAINT,
63
        EXT_NULLABLE,
64
        EXT_MODEL_SUBDIR,
65
        EXT_DEPRECATION_MESSAGE
66
    );
67

68
    private final Map<String, Object> extensionsByName;
69

70
    /**
71
     * Returns an {@link Extensions} object processing the specified JSON Schema extension map.
72
     * @param extensionsByName the JSON Schema extensions.
73
     * @return the {@link Extensions} object.
74
     */
75
    public static Extensions extensions(Map<String, Object> extensionsByName) {
76
        return new Extensions(extensionsByName);
5✔
77
    }
78

79
    private Extensions(Map<String, Object> extensionsByName) {
2✔
80
        this.extensionsByName = nonNull(extensionsByName) ? extensionsByName : emptyMap();
7!
81
    }
1✔
82

83
    /**
84
     * Gets value of a string extension property.
85
     * @param name the extension property name.
86
     * @return the extension property string value, if found; else empty.
87
     */
88
    public Optional<String> getString(String name) {
89
        Object value = extensionsByName.get(name);
5✔
90
        if (isNull(value)) {
3✔
91
            return Optional.empty();
2✔
92
        }
93
        if (!(value instanceof TextNode)) {
3!
NEW
94
            throw new JsonSchema2JavaException("Value of extension %s is not a String".formatted(name));
×
95
        }
96
        if (nonBlank(((TextNode)value).asText())) {
5!
97
            return Optional.of(((TextNode)value).asText());
5✔
98
        }
99

100
        return Optional.empty();
×
101
    }
102

103
    /**
104
     * Gets value of a boolean extension property.
105
     * @param name the extension property name.
106
     * @return the extension property value, if found; else empty.
107
     */
108
    public Optional<Boolean> getBoolean(String name) {
109
        Object value = extensionsByName.get(name);
5✔
110
        if (isNull(value)) {
3✔
111
            return Optional.empty();
2✔
112
        }
113
        if (!(value instanceof BooleanNode)) {
3!
NEW
114
            throw new JsonSchema2JavaException("Value of extension %s is not a Boolean".formatted(name));
×
115
        }
116

117
        return Optional.of(((BooleanNode)value).asBoolean());
6✔
118
    }
119
}
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