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

torand / openapi2java / 22519240185

28 Feb 2026 10:47AM UTC coverage: 84.535% (+0.1%) from 84.425%
22519240185

push

github

torand
chore: prepare release

572 of 795 branches covered (71.95%)

Branch coverage included in aggregate %.

1702 of 1895 relevant lines covered (89.82%)

5.39 hits per line

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

85.53
/src/main/java/io/github/torand/openapi2java/model/TypeInfo.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.openapi2java.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 string format.
39
 * @param schemaPattern the OpenAPI schema string pattern.
40
 * @param schemaMinLength the OpenAPI schema string minimum length.
41
 * @param schemaMaxLength the OpenAPI schema string maximum length.
42
 * @param annotations the annotations decorating this type.
43
 * @param imports the imports required by the type.
44
 */
45
public record TypeInfo (
39✔
46
    String name,
47
    String description,
48
    boolean nullable,
49
    TypeInfo keyType,
50
    boolean primitive,
51
    TypeInfo itemType,
52
    String schemaFormat,
53
    String schemaPattern,
54
    Integer schemaMinLength,
55
    Integer schemaMaxLength,
56
    List<AnnotationInfo> annotations,
57
    ImportInfo imports
58
) implements EntityInfo, ImportsSupplier {
59

60
    /**
61
     * Constructs an {@link TypeInfo} object.
62
     */
63
    public TypeInfo() {
64
        this(null, null, false, null, false, null, null, null, null, null, emptyList(), ImportInfo.empty());
14✔
65
    }
1✔
66

67
    /**
68
     * Returns a new {@link TypeInfo} object with specified name.
69
     * @param name the name.
70
     * @return the new and updated {@link TypeInfo} object.
71
     */
72
    public TypeInfo withName(String name) {
73
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, annotations, imports);
27✔
74
    }
75

76
    /**
77
     * Returns a new {@link TypeInfo} object with specified description.
78
     * @param description the description.
79
     * @return the new and updated {@link TypeInfo} object.
80
     */
81
    public TypeInfo withDescription(String description) {
82
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, annotations, imports);
27✔
83
    }
84

85
    /**
86
     * Returns a new {@link TypeInfo} object with specified nullable flag.
87
     * @param nullable the nullable flag.
88
     * @return the new and updated {@link TypeInfo} object.
89
     */
90
    public TypeInfo withNullable(boolean nullable) {
91
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, annotations, imports);
27✔
92
    }
93

94
    /**
95
     * Returns a new {@link TypeInfo} object with specified key type.
96
     * @param keyType the key type.
97
     * @return the new and updated {@link TypeInfo} object.
98
     */
99
    public TypeInfo withKeyType(TypeInfo keyType) {
100
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, annotations, imports);
27✔
101
    }
102

103
    /**
104
     * Returns a new {@link TypeInfo} object with specified primitive flag.
105
     * @param primitive the primitive flag.
106
     * @return the new and updated {@link TypeInfo} object.
107
     */
108
    public TypeInfo withPrimitive(boolean primitive) {
109
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, annotations, imports);
27✔
110
    }
111

112
    /**
113
     * Returns a new {@link TypeInfo} object with specified item type.
114
     * @param itemType the item type.
115
     * @return the new and updated {@link TypeInfo} object.
116
     */
117
    public TypeInfo withItemType(TypeInfo itemType) {
118
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, annotations, imports);
27✔
119
    }
120

121
    /**
122
     * Returns a new {@link TypeInfo} object with specified OpenAPI schema string format.
123
     * @param schemaFormat the OpenAPI schema string format.
124
     * @return the new and updated {@link TypeInfo} object.
125
     */
126
    public TypeInfo withSchemaFormat(String schemaFormat) {
127
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, annotations, imports);
27✔
128
    }
129

130
    /**
131
     * Returns a new {@link TypeInfo} object with specified OpenAPI schema string pattern.
132
     * @param schemaPattern the OpenAPI schema string pattern.
133
     * @return the new and updated {@link TypeInfo} object.
134
     */
135
    public TypeInfo withSchemaPattern(String schemaPattern) {
136
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, annotations, imports);
27✔
137
    }
138

139
    /**
140
     * Returns a new {@link TypeInfo} object with specified OpenAPI schema string minimum length.
141
     * @param schemaMinLength the OpenAPI schema string minimum length.
142
     * @return the new and updated {@link TypeInfo} object.
143
     */
144
    public TypeInfo withSchemaMinLength(Integer schemaMinLength) {
145
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, annotations, imports);
27✔
146
    }
147

148
    /**
149
     * Returns a new {@link TypeInfo} object with specified OpenAPI schema string maximum length.
150
     * @param schemaMaxLength the OpenAPI schema string maximum length.
151
     * @return the new and updated {@link TypeInfo} object.
152
     */
153
    public TypeInfo withSchemaMaxLength(Integer schemaMaxLength) {
154
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, annotations, imports);
27✔
155
    }
156

157
    /**
158
     * Returns a new {@link TypeInfo} object with specified annotation added.
159
     * @param annotation the annotation to add.
160
     * @return the new and updated {@link TypeInfo} object.
161
     */
162
    public TypeInfo withAddedAnnotation(AnnotationInfo annotation) {
163
        List<AnnotationInfo> newAnnotations = new LinkedList<>(annotations);
6✔
164
        newAnnotations.add(annotation);
4✔
165
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, newAnnotations, imports);
27✔
166
    }
167

168
    /**
169
     * Returns a new {@link PojoInfo} object with no annotations.
170
     * @return the new and updated {@link PojoInfo} object.
171
     */
172
    public TypeInfo withNoAnnotations() {
173
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, emptyList(), imports);
×
174
    }
175

176
    /**
177
     * Returns a new {@link TypeInfo} object with specified normal import added.
178
     * @param normalImport the import to add.
179
     * @return the new and updated {@link TypeInfo} object.
180
     */
181
    public TypeInfo withAddedNormalImport(String normalImport) {
182
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, schemaMinLength, schemaMaxLength, annotations, imports.withAddedNormalImport(normalImport));
30✔
183
    }
184

185
    /**
186
     * Gets whether this is an array type.
187
     * @return true if this is an array type; else false.
188
     */
189
    public boolean isArray() {
190
        return isNull(keyType) && nonNull(itemType);
×
191
    }
192

193
    /**
194
     * Gets the full name of Java/Kotlin type including generic composites.
195
     * @return the full name of Java/Kotlin type
196
     */
197
    public String getFullName() {
198
        if (nonNull(keyType) && nonNull(itemType)) {
8!
199
            return "%s<%s,%s>".formatted(name, keyType.getFullName(), itemType.getFullName());
22✔
200
        } else if (nonNull(itemType)) {
4✔
201
            return "%s<%s>".formatted(name, itemType.getFullName());
16✔
202
        } else {
203
            return name;
3✔
204
        }
205
    }
206

207
    /**
208
     * Gets full type name, including bean validation annotations.
209
     * @return the annotations and type name items.
210
     */
211
    public AnnotatedTypeName getAnnotatedFullName() {
212
        List<String> annotatedFullName = new ArrayList<>();
4✔
213
        streamSafely(annotations)
4✔
214
            .map(AnnotationInfo::annotation)
3✔
215
            .forEach(annotatedFullName::add);
4✔
216

217
        if (nonNull(itemType)) {
4✔
218
            String itemTypeWithAnnotations = itemType.getAnnotatedFullName().items()
6✔
219
                .filter(not("@Valid"::equals))
3✔
220
                .collect(joining(" "));
4✔
221

222
            if (nonNull(keyType)) {
4✔
223
                String keyTypeWithAnnotations = keyType.getAnnotatedFullName().items()
6✔
224
                    .filter(not("@Valid"::equals))
3✔
225
                    .collect(joining(" "));
4✔
226

227
                annotatedFullName.add("%s<%s, %s>".formatted(name, keyTypeWithAnnotations, itemTypeWithAnnotations));
20✔
228
            } else {
1✔
229
                annotatedFullName.add("%s<%s>".formatted(name, itemTypeWithAnnotations));
16✔
230
            }
231
        } else {
1✔
232
            annotatedFullName.add(name);
5✔
233
        }
234

235
        return new AnnotatedTypeName(annotatedFullName);
5✔
236
    }
237

238
    @Override
239
    public Set<String> aggregatedNormalImports() {
240
        ImportInfo imports = this.imports.withAddedImports(annotations);
6✔
241
        if (nonNull(keyType)) {
4!
242
            imports = imports.withAddedImports(keyType);
×
243
        }
244
        if (nonNull(itemType)) {
4✔
245
            imports = imports.withAddedImports(itemType);
5✔
246
        }
247

248
        return imports.normalImports();
3✔
249
    }
250

251
    @Override
252
    public Set<String> aggregatedStaticImports() {
253
        ImportInfo imports = this.imports.withAddedImports(annotations);
6✔
254
        if (nonNull(keyType)) {
4!
255
            imports = imports.withAddedImports(keyType);
×
256
        }
257
        if (nonNull(itemType)) {
4✔
258
            imports = imports.withAddedImports(itemType);
5✔
259
        }
260

261
        return imports.staticImports();
3✔
262
    }
263
}
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