• 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

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

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

28
import java.io.Writer;
29
import java.util.Set;
30
import java.util.TreeSet;
31
import java.util.function.Predicate;
32

33
import static io.github.torand.javacommons.collection.CollectionHelper.nonEmpty;
34
import static io.github.torand.javacommons.lang.StringHelper.nonBlank;
35
import static io.github.torand.javacommons.stream.StreamHelper.streamSafely;
36
import static io.github.torand.openapi2java.utils.KotlinTypeMapper.toKotlinNative;
37
import static io.github.torand.openapi2java.utils.PackageUtils.isFqnInPackage;
38
import static java.util.Objects.nonNull;
39
import static java.util.function.Predicate.not;
40
import static java.util.stream.Collectors.toCollection;
41

42
/**
43
 * Writes Kotlin code for a resource.
44
 */
45
public class KotlinResourceWriter extends BaseWriter implements ResourceWriter {
46

47
    public KotlinResourceWriter(Writer writer, Options opts) {
48
        super(writer, opts);
4✔
49
    }
1✔
50

51
    @Override
52
    public void write(ResourceInfo resourceInfo) {
53
        writeLine("package %s", opts.rootPackage());
11✔
54
        writeNewLine();
2✔
55

56
        writeImports(resourceInfo);
3✔
57

58
        resourceInfo.annotations().forEach(a -> writeLine(a.annotation()));
12✔
59
        writeLine("interface %s {".formatted(resourceInfo.name()));
13✔
60

61
        resourceInfo.methods().forEach(m -> {
5✔
62
            writeNewLine();
2✔
63
            if (m.isDeprecated()) {
3!
64
                writeIndent(1);
×
65
                writeLine("@Deprecated(\"%s\")".formatted(m.deprecationMessage()));
×
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
            writeLine("fun %s(".formatted(m.name()));
13✔
75
            for (int i=0; i<m.parameters().size(); i++) {
9✔
76
                MethodParamInfo paramInfo = m.parameters().get(i);
6✔
77
                writeIndent(2);
3✔
78
                if (nonEmpty(paramInfo.annotations())) {
4✔
79
                    write(String.join(" ", streamSafely(paramInfo.annotations()).map(AnnotationInfo::annotation).toList()) + " ");
13✔
80
                }
81

82
                AnnotatedTypeName annotatedTypeName = paramInfo.type().getAnnotatedFullName();
4✔
83
                if (annotatedTypeName.hasAnnotations()) {
3✔
84
                    write(annotatedTypeName.annotationsAsString() + " ");
7✔
85
                }
86
                write(paramInfo.name() + ": ");
7✔
87
                write(toKotlinNative(annotatedTypeName.typeName()));
7✔
88

89
                if (paramInfo.nullable()) {
3✔
90
                    write("? = null");
5✔
91
                }
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
            if (opts.useResteasyResponse()) {
4✔
103
                writeLine("): RestResponse<%s>".formatted(nonNull(m.returnType()) ? m.returnType() : "Unit"));
20✔
104
            } else {
105
                writeLine("): Response");
5✔
106
            }
107
        });
1✔
108

109
        writeNewLine();
2✔
110

111
        writeIndent(1);
3✔
112
        writeLine("companion object {");
5✔
113

114
        writeIndent(2);
3✔
115
        writeLine("const val ROOT_PATH: String = \"%s\"", opts.rootUrlPath());
11✔
116

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

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

123
    private void writeImports(ResourceInfo resourceInfo) {
124
        Predicate<String> isInSamePackage = fqn -> isFqnInPackage(fqn, opts.rootPackage());
9✔
125

126
        Set<String> imports = StreamHelper.concatStreams(
8✔
127
            resourceInfo.aggregatedNormalImports().stream()
3✔
128
                .filter(not(PackageUtils::isFundamentalJavaClass))
3✔
129
                .filter(not(isInSamePackage))
4✔
130
                .filter(not("java.util.List"::equals))
4✔
131
                .filter(not("java.util.Map"::equals)),
6✔
132
            resourceInfo.aggregatedStaticImports().stream()
3✔
133
                .filter(not(i -> i.contains("ROOT_PATH")))
7✔
134
            )
135
            .map("import %s"::formatted)
11✔
136
            .collect(toCollection(TreeSet::new));
4✔
137

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

140
        imports.forEach(this::writeLine);
10✔
141
        writeNewLine();
2✔
142
    }
1✔
143
}
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