• 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

51.35
/src/main/java/io/github/torand/jsonschema2java/model/TypeInfo.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.model;
17

18
import java.util.ArrayList;
19
import java.util.LinkedList;
20
import java.util.List;
21
import java.util.Set;
22

23
import static io.github.torand.javacommons.stream.StreamHelper.streamSafely;
24
import static java.util.Collections.emptyList;
25
import static java.util.Objects.isNull;
26
import static java.util.Objects.nonNull;
27
import static java.util.function.Predicate.not;
28
import static java.util.stream.Collectors.joining;
29

30
/**
31
 * Describes a type.
32
 * @param name the type name.
33
 * @param description the type description.
34
 * @param nullable the nullable flag.
35
 * @param keyType the key type, if this is a map type.
36
 * @param primitive the primitive flag.
37
 * @param itemType the item type, if this is an array type or map type.
38
 * @param schemaFormat the OpenAPI schema format.
39
 * @param schemaPattern the OpenAPI schema pattern.
40
 * @param annotations the annotations decorating this type.
41
 * @param imports the imports required by the type.
42
 */
43
public record TypeInfo (
33✔
44
    String name,
45
    String description,
46
    boolean nullable,
47
    TypeInfo keyType,
48
    boolean primitive,
49
    TypeInfo itemType,
50
    String schemaFormat,
51
    String schemaPattern,
52
    List<AnnotationInfo> annotations,
53
    ImportInfo imports
54
) implements EntityInfo, ImportsSupplier {
55

56
    /**
57
     * Constructs an {@link TypeInfo} object.
58
     */
59
    public TypeInfo() {
60
        this(null, null, false, null, false, null, null, null, emptyList(), ImportInfo.empty());
12✔
61
    }
1✔
62

63
    /**
64
     * Returns a new {@link TypeInfo} object with specified name.
65
     * @param name the name.
66
     * @return the new and updated {@link TypeInfo} object.
67
     */
68
    public TypeInfo withName(String name) {
69
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, annotations, imports);
23✔
70
    }
71

72
    /**
73
     * Returns a new {@link TypeInfo} object with specified description.
74
     * @param description the description.
75
     * @return the new and updated {@link TypeInfo} object.
76
     */
77
    public TypeInfo withDescription(String description) {
78
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, annotations, imports);
23✔
79
    }
80

81
    /**
82
     * Returns a new {@link TypeInfo} object with specified nullable flag.
83
     * @param nullable the nullable flag.
84
     * @return the new and updated {@link TypeInfo} object.
85
     */
86
    public TypeInfo withNullable(boolean nullable) {
87
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, annotations, imports);
23✔
88
    }
89

90
    /**
91
     * Returns a new {@link TypeInfo} object with specified key type.
92
     * @param keyType the key type.
93
     * @return the new and updated {@link TypeInfo} object.
94
     */
95
    public TypeInfo withKeyType(TypeInfo keyType) {
NEW
96
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, annotations, imports);
×
97
    }
98

99
    /**
100
     * Returns a new {@link TypeInfo} object with specified primitive flag.
101
     * @param primitive the primitive flag.
102
     * @return the new and updated {@link TypeInfo} object.
103
     */
104
    public TypeInfo withPrimitive(boolean primitive) {
105
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, annotations, imports);
23✔
106
    }
107

108
    /**
109
     * Returns a new {@link TypeInfo} object with specified item type.
110
     * @param itemType the item type.
111
     * @return the new and updated {@link TypeInfo} object.
112
     */
113
    public TypeInfo withItemType(TypeInfo itemType) {
114
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, annotations, imports);
23✔
115
    }
116

117
    /**
118
     * Returns a new {@link TypeInfo} object with specified OpenAPI schema format.
119
     * @param schemaFormat the OpenAPI schema format.
120
     * @return the new and updated {@link TypeInfo} object.
121
     */
122
    public TypeInfo withSchemaFormat(String schemaFormat) {
123
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, annotations, imports);
23✔
124
    }
125

126
    /**
127
     * Returns a new {@link TypeInfo} object with specified OpenAPI schema pattern.
128
     * @param schemaPattern the OpenAPI schema pattern.
129
     * @return the new and updated {@link TypeInfo} object.
130
     */
131
    public TypeInfo withSchemaPattern(String schemaPattern) {
132
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, annotations, imports);
23✔
133
    }
134

135
    /**
136
     * Returns a new {@link TypeInfo} object with specified annotation added.
137
     * @param annotation the annotation to add.
138
     * @return the new and updated {@link TypeInfo} object.
139
     */
140
    public TypeInfo withAddedAnnotation(AnnotationInfo annotation) {
141
        List<AnnotationInfo> newAnnotations = new LinkedList<>(annotations);
6✔
142
        newAnnotations.add(annotation);
4✔
143
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, newAnnotations, imports);
23✔
144
    }
145

146
    /**
147
     * Returns a new {@link PojoInfo} object with no annotations.
148
     * @return the new and updated {@link PojoInfo} object.
149
     */
150
    public TypeInfo withNoAnnotations() {
NEW
151
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, emptyList(), imports);
×
152
    }
153

154
    /**
155
     * Returns a new {@link TypeInfo} object with specified normal import added.
156
     * @param normalImport the import to add.
157
     * @return the new and updated {@link TypeInfo} object.
158
     */
159
    public TypeInfo withAddedNormalImport(String normalImport) {
160
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, annotations, imports.withAddedNormalImport(normalImport));
26✔
161
    }
162

163
    /**
164
     * Gets whether this is an array type.
165
     * @return true if this is an array type; else false.
166
     */
167
    public boolean isArray() {
NEW
168
        return isNull(keyType) && nonNull(itemType);
×
169
    }
170

171
    /**
172
     * Gets the full name of Java/Kotlin type including generic composites.
173
     * @return the full name of Java/Kotlin type
174
     */
175
    public String getFullName() {
176
        if (nonNull(keyType) && nonNull(itemType)) {
×
177
            return "%s<%s,%s>".formatted(name, keyType.getFullName(), itemType.getFullName());
×
178
        } else if (nonNull(itemType)) {
×
179
            return "%s<%s>".formatted(name, itemType.getFullName());
×
180
        } else {
181
            return name;
×
182
        }
183
    }
184

185
    /**
186
     * Gets full type name, including bean validation annotations.
187
     * @return the annotations and type name items.
188
     */
189
    public AnnotatedTypeName getAnnotatedFullName() {
190
        List<String> annotatedFullName = new ArrayList<>();
4✔
191
        streamSafely(annotations)
4✔
192
            .map(AnnotationInfo::annotation)
3✔
193
            .forEach(annotatedFullName::add);
4✔
194

195
        if (nonNull(itemType)) {
4✔
196
            String itemTypeWithAnnotations = itemType.getAnnotatedFullName().items()
6✔
197
                .filter(not("@Valid"::equals))
3✔
198
                .collect(joining(" "));
4✔
199

200
            if (nonNull(keyType)) {
4!
NEW
201
                String keyTypeWithAnnotations = keyType.getAnnotatedFullName().items()
×
NEW
202
                    .filter(not("@Valid"::equals))
×
NEW
203
                    .collect(joining(" "));
×
204

NEW
205
                annotatedFullName.add("%s<%s, %s>".formatted(name, keyTypeWithAnnotations, itemTypeWithAnnotations));
×
NEW
206
            } else {
×
207
                annotatedFullName.add("%s<%s>".formatted(name, itemTypeWithAnnotations));
16✔
208
            }
209
        } else {
1✔
210
            annotatedFullName.add(name);
5✔
211
        }
212

213
        return new AnnotatedTypeName(annotatedFullName);
5✔
214
    }
215

216
    @Override
217
    public Set<String> aggregatedNormalImports() {
218
        ImportInfo imports = this.imports.withAddedImports(annotations);
6✔
219
        if (nonNull(keyType)) {
4!
NEW
220
            imports = imports.withAddedImports(keyType);
×
221
        }
222
        if (nonNull(itemType)) {
4✔
223
            imports = imports.withAddedImports(itemType);
5✔
224
        }
225

226
        return imports.normalImports();
3✔
227
    }
228

229
    @Override
230
    public Set<String> aggregatedStaticImports() {
NEW
231
        ImportInfo imports = this.imports.withAddedImports(annotations);
×
NEW
232
        if (nonNull(keyType)) {
×
NEW
233
            imports = imports.withAddedImports(keyType);
×
234
        }
NEW
235
        if (nonNull(itemType)) {
×
NEW
236
            imports = imports.withAddedImports(itemType);
×
237
        }
238

NEW
239
        return imports.staticImports();
×
240
    }
241
}
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