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

torand / openapi2java / 18202480060

02 Oct 2025 06:44PM UTC coverage: 81.561% (-0.8%) from 82.388%
18202480060

push

github

web-flow
Merge pull request #48 from torand/refactor-info-classes

Refactor info classes

507 of 736 branches covered (68.89%)

Branch coverage included in aggregate %.

825 of 934 new or added lines in 38 files covered. (88.33%)

11 existing lines in 7 files now uncovered.

1541 of 1775 relevant lines covered (86.82%)

5.09 hits per line

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

69.23
/src/main/java/io/github/torand/openapi2java/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.openapi2java.model;
17

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

22
import static java.util.Collections.emptyList;
23
import static java.util.Objects.isNull;
24
import static java.util.Objects.nonNull;
25

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

52
    /**
53
     * Constructs an {@link TypeInfo} object.
54
     */
55
    public TypeInfo() {
56
        this(null, null, false, null, false, null, null, null, emptyList(), ImportInfo.empty());
12✔
57
    }
1✔
58

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

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

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

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

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

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

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

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

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

142
    /**
143
     * Returns a new {@link PojoInfo} object with no annotations.
144
     * @return the new and updated {@link PojoInfo} object.
145
     */
146
    public TypeInfo withNoAnnotations() {
147
        return new TypeInfo(name, description, nullable, keyType, primitive, itemType, schemaFormat, schemaPattern, emptyList(), imports);
23✔
148
    }
149

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

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

167
    /**
168
     * Gets the full name of Java/Kotlin type including generic composites.
169
     * @return the full name of Java/Kotlin type
170
     */
171
    public String getFullName() {
172
        if (nonNull(keyType) && nonNull(itemType)) {
4!
173
            return "%s<%s,%s>".formatted(name, keyType.getFullName(), itemType.getFullName());
×
174
        } else if (nonNull(itemType)) {
4✔
175
            return "%s<%s>".formatted(name, itemType.getFullName());
16✔
176
        } else {
177
            return name;
3✔
178
        }
179
    }
180

181
    @Override
182
    public Set<String> aggregatedNormalImports() {
183
        ImportInfo imports = this.imports.withAddedImports(annotations);
6✔
184
        if (nonNull(keyType)) {
4!
NEW
185
            imports = imports.withAddedImports(keyType);
×
186
        }
187
        if (nonNull(itemType)) {
4✔
188
            imports = imports.withAddedImports(itemType);
5✔
189
        }
190

191
        return imports.normalImports();
3✔
192
    }
193

194
    @Override
195
    public Set<String> aggregatedStaticImports() {
196
        ImportInfo imports = this.imports.withAddedImports(annotations);
6✔
197
        if (nonNull(keyType)) {
4!
NEW
198
            imports = imports.withAddedImports(keyType);
×
199
        }
200
        if (nonNull(itemType)) {
4!
NEW
201
            imports = imports.withAddedImports(itemType);
×
202
        }
203

204
        return imports.staticImports();
3✔
205
    }
206
}
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