• 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

89.36
/src/main/java/io/github/torand/openapi2java/writers/java/JavaResourceWriter.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.writers.java;
17

18
import io.github.torand.openapi2java.generators.Options;
19
import io.github.torand.openapi2java.model.AnnotationInfo;
20
import io.github.torand.openapi2java.model.MethodParamInfo;
21
import io.github.torand.openapi2java.model.ResourceInfo;
22
import io.github.torand.openapi2java.utils.PackageUtils;
23
import io.github.torand.openapi2java.writers.BaseWriter;
24
import io.github.torand.openapi2java.writers.ResourceWriter;
25

26
import java.io.Writer;
27
import java.util.List;
28
import java.util.function.Predicate;
29

30
import static io.github.torand.javacommons.collection.CollectionHelper.nonEmpty;
31
import static io.github.torand.javacommons.lang.StringHelper.nonBlank;
32
import static io.github.torand.javacommons.stream.StreamHelper.streamSafely;
33
import static io.github.torand.openapi2java.utils.PackageUtils.isFqnInPackage;
34
import static java.util.Objects.nonNull;
35
import static java.util.function.Predicate.not;
36

37
/**
38
 * Writes Java code for a resource.
39
 */
40
public class JavaResourceWriter extends BaseWriter implements ResourceWriter {
41

42
    public JavaResourceWriter(Writer writer, Options opts) {
43
        super(writer, opts);
4✔
44
    }
1✔
45

46
    @Override
47
    public void write(ResourceInfo resourceInfo) {
48
        writeLine("package %s;", opts.rootPackage());
11✔
49
        writeNewLine();
2✔
50

51
        writeNonJavaImports(resourceInfo);
3✔
52
        writeJavaImports(resourceInfo);
3✔
53
        writeStaticImports(resourceInfo);
3✔
54

55
        resourceInfo.annotations().forEach(a -> writeLine(a.annotation()));
12✔
56
        writeLine("public interface %s {".formatted(resourceInfo.name()));
13✔
57
        writeNewLine();
2✔
58

59
        writeIndent(1);
3✔
60
        writeLine("String ROOT_PATH = \"%s\";", opts.rootUrlPath());
11✔
61

62
        resourceInfo.methods().forEach(m -> {
5✔
63
            writeNewLine();
2✔
64
            if (m.isDeprecated()) {
3!
65
                writeIndent(1);
×
66
                writeLine("/// @deprecated %s".formatted(m.deprecationMessage()));
×
67
                writeIndent(1);
×
68
                writeLine("@Deprecated");
×
69
            }
70

71
            m.annotations().forEach(a -> {
5✔
72
                writeIndent(1);
3✔
73
                writeLine(a.annotation());
6✔
74
            });
1✔
75

76
            writeIndent(1);
3✔
77
            if (opts.useResteasyResponse()) {
4✔
78
                writeLine("RestResponse<%s> %s(".formatted(nonNull(m.returnType()) ? m.returnType() : "Void", m.name()));
25✔
79
            } else {
80
                writeLine("Response %s(".formatted(m.name()));
13✔
81
            }
82

83
            for (int i=0; i<m.parameters().size(); i++) {
9✔
84
                MethodParamInfo paramInfo = m.parameters().get(i);
6✔
85
                writeIndent(2);
3✔
86
                if (paramInfo.isDeprecated()) {
3!
87
                    write("@Deprecated ");
×
88
                }
89

90
                if (nonEmpty(paramInfo.annotations())) {
4✔
91
                    write(String.join(" ", streamSafely(paramInfo.annotations()).map(AnnotationInfo::annotation).toList()) + " ");
13✔
92
                }
93

94
                write(paramInfo.type().getAnnotatedFullName().asString() + " ");
9✔
95
                write(paramInfo.name());
6✔
96

97
                if (i < (m.parameters().size()-1)) {
7✔
98
                    write(",");
5✔
99
                }
100
                if (nonBlank(paramInfo.comment())) {
4✔
101
                    write(" // %s", paramInfo.comment());
10✔
102
                }
103
                writeNewLine();
2✔
104
            }
105

106
            writeIndent(1);
3✔
107
            writeLine(");");
5✔
108
        });
1✔
109

110
        writeLine("}");
5✔
111
    }
1✔
112

113
    private void writeJavaImports(ResourceInfo resourceInfo) {
114
        List<String> imports = resourceInfo.aggregatedNormalImports().stream()
4✔
115
            .filter(PackageUtils::isJavaPackage)
2✔
116
            .filter(not(PackageUtils::isFundamentalJavaClass))
4✔
117
            .map("import %s;"::formatted)
10✔
118
            .toList();
2✔
119

120
        if (nonEmpty(imports)) {
3!
121
            imports.forEach(this::writeLine);
10✔
122
            writeNewLine();
2✔
123
        }
124
    }
1✔
125

126
    private void writeNonJavaImports(ResourceInfo resourceInfo) {
127
        Predicate<String> isInSamePackage = fqn -> isFqnInPackage(fqn, opts.rootPackage());
9✔
128

129
        List<String> imports = resourceInfo.aggregatedNormalImports().stream()
4✔
130
            .filter(not(PackageUtils::isJavaPackage))
3✔
131
            .filter(not(isInSamePackage))
4✔
132
            .map("import %s;"::formatted)
10✔
133
            .toList();
2✔
134

135
        if (nonEmpty(imports)) {
3!
136
            imports.forEach(this::writeLine);
10✔
137
            writeNewLine();
2✔
138
        }
139
    }
1✔
140

141
    private void writeStaticImports(ResourceInfo resourceInfo) {
142
        List<String> imports = resourceInfo.aggregatedStaticImports().stream()
5✔
143
            .map("import static %s;"::formatted)
10✔
144
            .toList();
2✔
145

146
        if (nonEmpty(imports)) {
3!
147
            imports.forEach(this::writeLine);
10✔
148
            writeNewLine();
2✔
149
        }
150
    }
1✔
151
}
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