• 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

89.69
/src/main/java/io/github/torand/openapi2java/collectors/ResourceInfoCollector.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.collectors;
17

18
import io.github.torand.openapi2java.generators.Options;
19
import io.github.torand.openapi2java.model.AnnotationInfo;
20
import io.github.torand.openapi2java.model.MethodInfo;
21
import io.github.torand.openapi2java.model.ResourceInfo;
22
import io.github.torand.openapi2java.model.SecurityRequirementInfo;
23
import io.swagger.v3.oas.models.Operation;
24
import io.swagger.v3.oas.models.PathItem;
25
import io.swagger.v3.oas.models.security.SecurityRequirement;
26
import io.swagger.v3.oas.models.tags.Tag;
27

28
import java.util.ArrayList;
29
import java.util.List;
30
import java.util.Map;
31

32
import static io.github.torand.javacommons.collection.CollectionHelper.nonEmpty;
33
import static io.github.torand.openapi2java.collectors.Extensions.EXT_RESTCLIENT_CONFIGKEY;
34
import static io.github.torand.openapi2java.collectors.Extensions.extensions;
35
import static java.util.Objects.isNull;
36
import static java.util.Objects.nonNull;
37

38
/**
39
 * Collects information about a resource from a collection of path items.
40
 */
41
public class ResourceInfoCollector extends BaseCollector {
42
    public static final String AUTH_METHOD_NAME = "authorization";
43

44
    private final MethodInfoCollector methodInfoCollector;
45
    private final SecurityRequirementCollector securityRequirementCollector;
46

47
    public ResourceInfoCollector(ComponentResolver componentResolver, Options opts) {
48
        super(opts);
3✔
49
        TypeInfoCollector typeInfoCollector = new TypeInfoCollector(componentResolver.schemas(), opts);
7✔
50
        this.methodInfoCollector = new MethodInfoCollector(componentResolver, typeInfoCollector, opts);
8✔
51
        this.securityRequirementCollector = new SecurityRequirementCollector(opts);
6✔
52
    }
1✔
53

54
    public ResourceInfo getResourceInfo(String resourceName, Map<String, PathItem> paths, List<SecurityRequirement> securityRequirements, Tag tag) {
55
        ResourceInfo resourceInfo = new ResourceInfo(resourceName + opts.resourceNameSuffix());
9✔
56

57
        if (opts.useResteasyResponse()) {
4✔
58
            resourceInfo = resourceInfo.withAddedNormalImport("org.jboss.resteasy.reactive.RestResponse");
5✔
59
        } else {
60
            resourceInfo = resourceInfo.withAddedNormalImport("jakarta.ws.rs.core.Response");
4✔
61
        }
62

63
        if (nonEmpty(securityRequirements)) {
3!
64
            SecurityRequirementInfo secReqInfo = securityRequirementCollector.getSequrityRequirementInfo(securityRequirements);
5✔
65
            resourceInfo = resourceInfo.withAddedAnnotation(secReqInfo.annotation());
5✔
66
        }
67

68
        if (opts.addMpOpenApiAnnotations() && nonNull(tag)) {
7!
69
            AnnotationInfo tagAnnotation = new AnnotationInfo(
8✔
70
                "@Tag(name = \"%s\", description = \"%s\")".formatted(tag.getName(), normalizeDescription(tag.getDescription())),
13✔
71
                "org.eclipse.microprofile.openapi.annotations.tags.Tag"
72
            );
73
            resourceInfo = resourceInfo.withAddedAnnotation(tagAnnotation);
4✔
74
        }
75

76
        if (opts.addMpRestClientAnnotations()) {
4!
77
            String configKey = nonNull(tag) ?
3✔
78
                extensions(tag.getExtensions())
3✔
79
                    .getString(EXT_RESTCLIENT_CONFIGKEY)
2✔
80
                    .orElse(tag.getName().toLowerCase()+"-api") :
7✔
81
                resourceName.toLowerCase()+"-api";
4✔
82

83
            AnnotationInfo registerRestClientAnnotation = new AnnotationInfo(
9✔
84
                "@RegisterRestClient(configKey = \"%s\")".formatted(configKey),
4✔
85
                "org.eclipse.microprofile.rest.client.inject.RegisterRestClient"
86
            );
87
            AnnotationInfo clientHeaderParamAnnotation = new AnnotationInfo(
15✔
88
                "@ClientHeaderParam(name = AUTHORIZATION, value = %s)".formatted(formatAnnotationNamedParam(List.of("\"{%s}\"".formatted(AUTH_METHOD_NAME)))),
8✔
89
                "org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam"
90
            ).withAddedStaticImport("jakarta.ws.rs.core.HttpHeaders.AUTHORIZATION");
2✔
91

92
            resourceInfo = resourceInfo
2✔
93
                .withAddedAnnotation(registerRestClientAnnotation)
2✔
94
                .withAddedAnnotation(clientHeaderParamAnnotation)
2✔
95
                .withAuthMethod(getAuthMethodInfo());
3✔
96
        }
97

98
        AnnotationInfo pathAnnotation = new AnnotationInfo("@Path(ROOT_PATH)", "jakarta.ws.rs.Path")
12✔
99
            .withAddedStaticImport("%s.%s.ROOT_PATH".formatted(opts.rootPackage(), resourceInfo.name()));
10✔
100
        resourceInfo = resourceInfo.withAddedAnnotation(pathAnnotation);
4✔
101

102
        String tagName = nonNull(tag) ? tag.getName() : null;
8✔
103

104
        List<MethodInfo> methods = new ArrayList<>();
4✔
105
        paths.forEach((path, pathInfo) -> {
6✔
106
            if (shouldProcessOperation(pathInfo.getGet(), tagName)) {
6✔
107
                methods.add(methodInfoCollector.getMethodInfo("GET", path, pathInfo.getGet()));
10✔
108
            }
109
            if (shouldProcessOperation(pathInfo.getPost(), tagName)) {
6✔
110
                methods.add(methodInfoCollector.getMethodInfo("POST", path, pathInfo.getPost()));
10✔
111
            }
112
            if (shouldProcessOperation(pathInfo.getDelete(), tagName)) {
6!
NEW
113
                methods.add(methodInfoCollector.getMethodInfo("DELETE", path, pathInfo.getDelete()));
×
114
            }
115
            if (shouldProcessOperation(pathInfo.getPut(), tagName)) {
6!
NEW
116
                methods.add(methodInfoCollector.getMethodInfo("PUT", path, pathInfo.getPut()));
×
117
            }
118
            if (shouldProcessOperation(pathInfo.getPatch(), tagName)) {
6!
NEW
119
                methods.add(methodInfoCollector.getMethodInfo("PATCH", path, pathInfo.getPatch()));
×
120
            }
121
        });
1✔
122

123
        for (MethodInfo method : methods) {
10✔
124
            resourceInfo = resourceInfo.withAddedMethod(method);
4✔
125
        }
1✔
126

127
        return resourceInfo;
2✔
128
    }
129

130
    private MethodInfo getAuthMethodInfo() {
131
        MethodInfo authMethod = new MethodInfo(AUTH_METHOD_NAME);
5✔
132

133
        if (!opts.useKotlinSyntax()) {
4✔
134
            authMethod = authMethod.withAddedAnnotation(new AnnotationInfo("@SuppressWarnings(\"unused\") // Used by @ClientHeaderParam"));
7✔
135
        }
136

137
        return authMethod;
2✔
138
    }
139

140
    private boolean shouldProcessOperation(Operation operation, String tag) {
141
        if (isNull(operation)) {
3✔
142
            return false;
2✔
143
        }
144

145
        if (isNull(tag)) {
3✔
146
            return true;
2✔
147
        }
148

149
        return nonNull(operation.getTags()) && operation.getTags().contains(tag);
13!
150
    }
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