Skip to content

Commit 9990ccb

Browse files
committed
create 'excluded' interface
1 parent bb7770a commit 9990ccb

2 files changed

Lines changed: 72 additions & 3 deletions

File tree

src/main/groovy/com/github/hauner/openapi/spring/converter/InterfaceCollector.groovy

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package com.github.hauner.openapi.spring.converter
1818

19+
import com.github.hauner.openapi.spring.converter.mapping.AmbiguousTypeMappingException
20+
import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
21+
import com.github.hauner.openapi.spring.converter.mapping.Mapping
22+
import com.github.hauner.openapi.spring.converter.mapping.MappingSchema
1923
import com.github.hauner.openapi.spring.model.Interface
2024
import io.swagger.v3.oas.models.Operation
2125
import io.swagger.v3.oas.models.PathItem
@@ -32,6 +36,25 @@ class InterfaceCollector {
3236

3337
private ApiOptions options
3438

39+
class MappingSchemaEndpoint implements MappingSchema {
40+
String path
41+
42+
@Override
43+
String getPath () {
44+
path
45+
}
46+
47+
@Override
48+
String getName () {
49+
null
50+
}
51+
52+
@Override
53+
String getContentType () {
54+
null
55+
}
56+
}
57+
3558
InterfaceCollector(ApiOptions options) {
3659
this.options = options
3760
}
@@ -49,7 +72,11 @@ class InterfaceCollector {
4972
paths.each { Map.Entry<String, PathItem> entry ->
5073
def operations = collectOperations (entry.value)
5174
operations.each { op ->
52-
String targetInterfaceName = getInterfaceName (op)
75+
String targetInterfaceName = getInterfaceName (op, isExcluded (entry.key))
76+
77+
if (interfaces.containsKey (targetInterfaceName)) {
78+
return
79+
}
5380

5481
def itf = new Interface (
5582
pkg: [options.packageName, 'api'].join ('.'),
@@ -63,17 +90,38 @@ class InterfaceCollector {
6390
interfaces.values () as List
6491
}
6592

93+
private boolean isExcluded (String path) {
94+
def endpointMatches = options.typeMappings.findAll {
95+
it.matches (Mapping.Level.ENDPOINT, new MappingSchemaEndpoint(path: path))
96+
}
97+
98+
if (!endpointMatches.empty) {
99+
if (endpointMatches.size () != 1) {
100+
throw new AmbiguousTypeMappingException (endpointMatches)
101+
}
102+
103+
def match = endpointMatches.first () as EndpointTypeMapping
104+
return match.exclude
105+
}
106+
107+
false
108+
}
109+
66110
private List<Operation> collectOperations(PathItem item) {
67111
new OperationCollector ().collect (item)
68112
}
69113

70-
private String getInterfaceName (def op) {
114+
private String getInterfaceName (def op, boolean excluded) {
71115
String targetInterfaceName = INTERFACE_DEFAULT_NAME
72116

73117
if (hasTags (op)) {
74118
targetInterfaceName = op.tags.first ()
75119
}
76120

121+
if (excluded) {
122+
targetInterfaceName += 'Excluded'
123+
}
124+
77125
targetInterfaceName
78126
}
79127

src/test/groovy/com/github/hauner/openapi/spring/converter/InterfaceCollectorSpec.groovy

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.github.hauner.openapi.spring.converter
1818

19-
19+
import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
2020
import io.swagger.v3.oas.models.Operation
2121
import io.swagger.v3.oas.models.PathItem
2222
import io.swagger.v3.oas.models.Paths
@@ -52,4 +52,25 @@ class InterfaceCollectorSpec extends Specification {
5252
then:
5353
result.first ().interfaceName == 'Api'
5454
}
55+
56+
void "creates 'Excluded' interface when an endpoint should be skipped" () {
57+
def options = new ApiOptions(typeMappings: [
58+
new EndpointTypeMapping (path: '/foo', exclude: true, typeMappings: [])
59+
])
60+
61+
def collector = new InterfaceCollector(options)
62+
63+
def paths = new Paths()
64+
paths.put ('/bar', new PathItem(get: new Operation()))
65+
paths.put ('/foo', new PathItem(get: new Operation()))
66+
67+
when:
68+
def result = collector.collect (paths)
69+
70+
then:
71+
result.size () == 2
72+
result[0].interfaceName == 'Api'
73+
result[1].interfaceName == 'ExcludedApi'
74+
}
75+
5576
}

0 commit comments

Comments
 (0)