• 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

88.68
/src/main/java/io/github/torand/openapi2java/writers/java/JavaResourceWriter.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.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.writers.BaseWriter;
23
import io.github.torand.openapi2java.writers.ResourceWriter;
24

25
import java.io.Writer;
26
import java.util.List;
27

28
import static io.github.torand.javacommons.collection.CollectionHelper.nonEmpty;
29
import static io.github.torand.javacommons.lang.StringHelper.nonBlank;
30
import static io.github.torand.javacommons.stream.StreamHelper.streamSafely;
31
import static java.util.Objects.nonNull;
32
import static java.util.function.Predicate.not;
33

34
/**
35
 * Writes Java code for a resource.
36
 */
37
public class JavaResourceWriter extends BaseWriter implements ResourceWriter {
38

39
    public JavaResourceWriter(Writer writer, Options opts) {
40
        super(writer, opts);
4✔
41
    }
1✔
42

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

48
        writeNonJavaImports(resourceInfo);
3✔
49
        writeJavaImports(resourceInfo);
3✔
50
        writeStaticImports(resourceInfo);
3✔
51

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

56
        writeIndent(1);
3✔
57
        writeLine("String ROOT_PATH = \"%s\";", opts.rootUrlPath());
11✔
58

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

68
            m.annotations().forEach(a -> {
5✔
69
                writeIndent(1);
3✔
70
                writeLine(a.annotation());
6✔
71
            });
1✔
72

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

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

87
                if (nonEmpty(paramInfo.annotations())) {
4!
88
                    write(String.join(" ", streamSafely(paramInfo.annotations()).map(AnnotationInfo::annotation).toList()) + " ");
13✔
89
                }
90
                write(paramInfo.type().getFullName() + " ");
8✔
91
                write(paramInfo.name());
6✔
92
                if (i < (m.parameters().size()-1)) {
7✔
93
                    write(",");
5✔
94
                }
95
                if (nonBlank(paramInfo.comment())) {
4✔
96
                    write(" // %s", paramInfo.comment());
10✔
97
                }
98
                writeNewLine();
2✔
99
            }
100

101
            writeIndent(1);
3✔
102
            writeLine(");");
5✔
103
        });
1✔
104

105
        if (nonNull(resourceInfo.authMethod())) {
4!
106
            writeNewLine();
2✔
107

108
            resourceInfo.authMethod().annotations().forEach(a -> {
6✔
109
                writeIndent(1);
3✔
110
                writeLine(a.annotation());
6✔
111
            });
1✔
112

113
            writeIndent(1);
3✔
114
            writeLine("default String %s() {".formatted(resourceInfo.authMethod().name()));
14✔
115
            writeIndent(2);
3✔
116
            writeLine("return \"TODO\";");
5✔
117
            writeIndent(1);
3✔
118
            writeLine("}");
5✔
119
        }
120

121
        writeLine("}");
5✔
122
    }
1✔
123

124
    private void writeJavaImports(ResourceInfo resourceInfo) {
125
        List<String> imports = resourceInfo.aggregatedNormalImports().stream()
5✔
126
            .filter(this::isJavaPackage)
3✔
127
            .map("import %s;"::formatted)
10✔
128
            .toList();
2✔
129

130
        if (nonEmpty(imports)) {
3!
131
            imports.forEach(this::writeLine);
10✔
132
            writeNewLine();
2✔
133
        }
134
    }
1✔
135

136
    private void writeNonJavaImports(ResourceInfo resourceInfo) {
137
        List<String> imports = resourceInfo.aggregatedNormalImports().stream()
5✔
138
            .filter(not(this::isJavaPackage))
4✔
139
            .map("import %s;"::formatted)
10✔
140
            .toList();
2✔
141

142
        if (nonEmpty(imports)) {
3!
143
            imports.forEach(this::writeLine);
10✔
144
            writeNewLine();
2✔
145
        }
146
    }
1✔
147

148
    private void writeStaticImports(ResourceInfo resourceInfo) {
149
        List<String> imports = resourceInfo.aggregatedStaticImports().stream()
5✔
150
            .map("import static %s;"::formatted)
10✔
151
            .toList();
2✔
152

153
        if (nonEmpty(imports)) {
3!
154
            imports.forEach(this::writeLine);
10✔
155
            writeNewLine();
2✔
156
        }
157
    }
1✔
158

159
    private boolean isJavaPackage(String qualifiedType) {
160
        return qualifiedType.startsWith("java.");
4✔
161
    }
162
}
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