• 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

90.12
/src/main/java/io/github/torand/openapi2java/writers/kotlin/KotlinResourceWriter.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.kotlin;
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.Set;
27
import java.util.TreeSet;
28

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

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

42
    public KotlinResourceWriter(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
        writeImports(resourceInfo);
3✔
52

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

56
        resourceInfo.methods().forEach(m -> {
5✔
57
            writeNewLine();
2✔
58
            if (m.isDeprecated()) {
3!
59
                writeIndent(1);
×
NEW
60
                writeLine("@Deprecated(\"%s\")".formatted(m.deprecationMessage()));
×
61
            }
62

63
            m.annotations().forEach(a -> {
5✔
64
                writeIndent(1);
3✔
65
                writeLine(a.annotation());
6✔
66
            });
1✔
67

68
            writeIndent(1);
3✔
69
            writeLine("fun %s(".formatted(m.name()));
13✔
70
            for (int i=0; i<m.parameters().size(); i++) {
9✔
71
                MethodParamInfo paramInfo = m.parameters().get(i);
6✔
72
                writeIndent(2);
3✔
73
                if (nonEmpty(paramInfo.annotations())) {
4!
74
                    write(String.join(" ", streamSafely(paramInfo.annotations()).map(AnnotationInfo::annotation).toList()) + " ");
13✔
75
                }
76
                write(paramInfo.name() + ": ");
7✔
77
                write(toKotlinNative(paramInfo.type().getFullName()));
8✔
78
                if (paramInfo.nullable()) {
3✔
79
                    write("? = null");
5✔
80
                }
81
                if (i < (m.parameters().size()-1)) {
7✔
82
                    write(",");
5✔
83
                }
84
                if (nonBlank(paramInfo.comment())) {
4✔
85
                    write(" // %s", paramInfo.comment());
10✔
86
                }
87
                writeNewLine();
2✔
88
            }
89

90
            writeIndent(1);
3✔
91
            if (opts.useResteasyResponse()) {
4✔
92
                writeLine("): RestResponse<%s>".formatted(nonNull(m.returnType()) ? m.returnType() : "Unit"));
20✔
93
            } else {
94
                writeLine("): Response");
5✔
95
            }
96
        });
1✔
97

98
        writeNewLine();
2✔
99

100
        writeIndent(1);
3✔
101
        writeLine("companion object {");
5✔
102

103
        writeIndent(2);
3✔
104
        writeLine("const val ROOT_PATH: String = \"%s\"", opts.rootUrlPath());
11✔
105

106
        if (nonNull(resourceInfo.authMethod())) {
4!
107
            resourceInfo.authMethod().annotations().forEach(a -> {
6✔
108
                writeIndent(2);
×
NEW
109
                writeLine(a.annotation());
×
110
            });
×
111

112
            writeIndent(2);
3✔
113
            writeLine("fun %s() = \"TODO\"".formatted(resourceInfo.authMethod().name()));
14✔
114
        }
115

116
        writeIndent(1);
3✔
117
        writeLine("}");
5✔
118

119
        writeLine("}");
5✔
120
    }
1✔
121

122
    private void writeImports(ResourceInfo resourceInfo) {
123
        Set<String> imports = resourceInfo.aggregatedImports().stream()
5✔
124
            .filter(not("java.util.List"::equals))
4✔
125
            .filter(not("java.util.Map"::equals))
3✔
126
            .filter(not(i -> i.contains("ROOT_PATH")))
8✔
127
            .map("import %s"::formatted)
11✔
128
            .collect(toCollection(TreeSet::new));
4✔
129

130
        imports.add("import %s.%s.Companion.ROOT_PATH".formatted(opts.rootPackage(), resourceInfo.name()));
18✔
131

132
        imports.forEach(this::writeLine);
10✔
133
        writeNewLine();
2✔
134
    }
1✔
135
}
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