Skip to content

Commit 6fafd82

Browse files
committed
Merge branch 'openapi4j-v2'
Conflicts: src/testInt/groovy/com/github/hauner/openapi/processor/ProcessorEndToEndTest.groovy
2 parents a2415d8 + 101d474 commit 6fafd82

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1965
-198
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ dependencies {
4848
implementation 'org.codehaus.groovy:groovy:2.5.4'
4949
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
5050
implementation 'io.swagger.parser.v3:swagger-parser:2.0.17'
51+
implementation 'org.openapi4j:openapi-parser:0.7'
5152
implementation 'com.google.googlejavaformat:google-java-format:1.7'
5253
compileOnly "com.github.hauner.openapi:openapi-processor-api:$processorApiVersion"
5354

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

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import com.github.hauner.openapi.spring.converter.mapping.Mapping
2323
import com.github.hauner.openapi.spring.converter.mapping.MappingSchema
2424
import com.github.hauner.openapi.spring.converter.mapping.TargetType
2525
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
26-
import com.github.hauner.openapi.spring.converter.schema.RefResolver
2726
import com.github.hauner.openapi.spring.converter.schema.SchemaInfo
2827
import com.github.hauner.openapi.spring.model.Api
2928
import com.github.hauner.openapi.spring.model.DataTypes
@@ -39,17 +38,18 @@ import com.github.hauner.openapi.spring.model.parameters.MultipartParameter
3938
import com.github.hauner.openapi.spring.model.parameters.Parameter as ModelParameter
4039
import com.github.hauner.openapi.spring.model.parameters.PathParameter
4140
import com.github.hauner.openapi.spring.model.parameters.QueryParameter
42-
import com.github.hauner.openapi.spring.model.Response
41+
import com.github.hauner.openapi.spring.model.Response as ModelResponse
4342
import com.github.hauner.openapi.spring.model.datatypes.DataType
43+
import com.github.hauner.openapi.spring.parser.OpenApi
44+
import com.github.hauner.openapi.spring.parser.MediaType
45+
import com.github.hauner.openapi.spring.parser.Operation
46+
import com.github.hauner.openapi.spring.parser.Parameter
47+
import com.github.hauner.openapi.spring.parser.Path
48+
import com.github.hauner.openapi.spring.parser.RefResolver
49+
import com.github.hauner.openapi.spring.parser.Response
50+
import com.github.hauner.openapi.spring.parser.RequestBody
4451
import com.github.hauner.openapi.support.Identifier
4552
import groovy.util.logging.Slf4j
46-
import io.swagger.v3.oas.models.OpenAPI
47-
import io.swagger.v3.oas.models.PathItem
48-
import io.swagger.v3.oas.models.media.MediaType
49-
import io.swagger.v3.oas.models.parameters.Parameter
50-
import io.swagger.v3.oas.models.parameters.RequestBody
51-
import io.swagger.v3.oas.models.responses.ApiResponse
52-
import io.swagger.v3.oas.models.responses.ApiResponses
5353

5454
/**
5555
* Converts the open api model to a new model that is better suited for generating source files
@@ -100,26 +100,24 @@ class ApiConverter {
100100
* @param api the open api model
101101
* @return source generation model
102102
*/
103-
Api convert (OpenAPI api) {
103+
Api convert (OpenApi api) {
104104
def target = new Api ()
105105
createInterfaces (api, target)
106106
target
107107
}
108108

109-
private void createInterfaces (OpenAPI api, Api target) {
110-
def resolver = new RefResolver (api.components)
109+
private void createInterfaces (OpenApi api, Api target) {
111110
Map<String, Interface>interfaces = new HashMap<> ()
112111

113-
api.paths.each { Map.Entry<String, PathItem> pathEntry ->
112+
api.paths.each { Map.Entry<String, Path> pathEntry ->
114113
String path = pathEntry.key
115-
PathItem pathItem = pathEntry.value
114+
Path pathValue = pathEntry.value
116115

117-
def operations = new OperationCollector ()
118-
.collect (pathItem)
116+
def operations = pathValue.operations
117+
operations.each { Operation op ->
118+
Interface itf = createInterface (path, op, interfaces)
119119

120-
operations.each { httpOperation ->
121-
Interface itf = createInterface (path, httpOperation, interfaces)
122-
Endpoint ep = createEndpoint (path, httpOperation, target.models, resolver)
120+
Endpoint ep = createEndpoint (path, op, target.models, api.refResolver)
123121
if (ep) {
124122
itf.endpoints.add (ep)
125123
}
@@ -129,7 +127,7 @@ class ApiConverter {
129127
target.interfaces = interfaces.values () as List<Interface>
130128
}
131129

132-
private Interface createInterface (String path, def operation, Map<String, Interface> interfaces) {
130+
private Interface createInterface (String path, Operation operation, Map<String, Interface> interfaces) {
133131
def targetInterfaceName = getInterfaceName (operation, isExcluded (path))
134132

135133
def itf = interfaces.get (targetInterfaceName)
@@ -146,8 +144,8 @@ class ApiConverter {
146144
itf
147145
}
148146

149-
private Endpoint createEndpoint (String path, def operation, DataTypes dataTypes, RefResolver resolver) {
150-
Endpoint ep = new Endpoint (path: path, method: (operation as HttpMethodTrait).httpMethod)
147+
private Endpoint createEndpoint (String path, Operation operation, DataTypes dataTypes, RefResolver resolver) {
148+
Endpoint ep = new Endpoint (path: path, method: operation.method)
151149

152150
try {
153151
collectParameters (operation.parameters, ep, dataTypes, resolver)
@@ -181,12 +179,12 @@ class ApiConverter {
181179

182180
requestBody.content.each { Map.Entry<String, MediaType> requestBodyEntry ->
183181
def contentType = requestBodyEntry.key
184-
def reqBody = requestBodyEntry.value
182+
def mediaType = requestBodyEntry.value
185183

186184
def info = new SchemaInfo (
187185
path: ep.path,
188186
name: getInlineRequestBodyName (ep.path),
189-
schema: reqBody.schema,
187+
schema: mediaType.schema,
190188
resolver: resolver)
191189

192190
if (contentType == MULTIPART) {
@@ -197,15 +195,15 @@ class ApiConverter {
197195
}
198196
}
199197

200-
private collectResponses (ApiResponses responses, Endpoint ep, DataTypes dataTypes, RefResolver resolver) {
201-
responses.each { Map.Entry<String, ApiResponse> responseEntry ->
198+
private collectResponses (Map<String, Response> responses, Endpoint ep, DataTypes dataTypes, RefResolver resolver) {
199+
responses.each { Map.Entry<String, Response> responseEntry ->
202200
def httpStatus = responseEntry.key
203201
def httpResponse = responseEntry.value
204202

205203
if (!httpResponse.content) {
206-
ep.addResponses (httpStatus, [Response.EMPTY])
204+
ep.addResponses (httpStatus, [ModelResponse.EMPTY])
207205
} else {
208-
List<Response> results = createResponses (
206+
List<ModelResponse> results = createResponses (
209207
ep.path,
210208
httpStatus,
211209
httpResponse,
@@ -215,9 +213,10 @@ class ApiConverter {
215213
ep.addResponses (httpStatus, results)
216214
}
217215
}
216+
218217
}
219218

220-
private ModelParameter createParameter (String path, Parameter parameter, DataTypes dataTypes, resolver) {
219+
private ModelParameter createParameter (String path, Parameter parameter, DataTypes dataTypes, RefResolver resolver) {
221220
def info = new SchemaInfo (
222221
path: path,
223222
name: parameter.name,
@@ -274,10 +273,10 @@ class ApiConverter {
274273
}
275274
}
276275

277-
private List<Response> createResponses (String path, String httpStatus, ApiResponse apiResponse, DataTypes dataTypes, RefResolver resolver) {
276+
private List<ModelResponse> createResponses (String path, String httpStatus, Response response, DataTypes dataTypes, RefResolver resolver) {
278277
def responses = []
279278

280-
apiResponse.content.each { Map.Entry<String, MediaType> contentEntry ->
279+
response.content.each { Map.Entry<String, MediaType> contentEntry ->
281280
def contentType = contentEntry.key
282281
def mediaType = contentEntry.value
283282
def schema = mediaType.schema
@@ -291,11 +290,11 @@ class ApiConverter {
291290

292291
DataType dataType = dataTypeConverter.convert (info, dataTypes)
293292

294-
def response = new Response (
293+
def resp = new ModelResponse (
295294
contentType: contentType,
296295
responseType: dataType)
297296

298-
responses.add (response)
297+
responses.add (resp)
299298
}
300299

301300
responses
@@ -341,8 +340,8 @@ class ApiConverter {
341340
private String getInterfaceName (def op, boolean excluded) {
342341
String targetInterfaceName = INTERFACE_DEFAULT_NAME
343342

344-
if (hasTags (op)) {
345-
targetInterfaceName = op.tags.first ()
343+
if ((op.hasTags())) {
344+
targetInterfaceName = op.firstTag
346345
}
347346

348347
if (excluded) {
@@ -352,8 +351,4 @@ class ApiConverter {
352351
targetInterfaceName
353352
}
354353

355-
private boolean hasTags (op) {
356-
op.tags && !op.tags.empty
357-
}
358-
359354
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original authors
2+
* Copyright 2019-2020 the original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,17 +27,17 @@ import io.swagger.v3.oas.models.PathItem
2727
*/
2828
class OperationCollector {
2929

30-
List<Operation> collect (PathItem item) {
31-
def ops = []
30+
Map<HttpMethod, Operation> collect (PathItem item) {
31+
def ops = [:] as Map<HttpMethod, Operation>
3232

3333
HttpMethod.values ().each {
34-
def pathItem = item."${it.method}" as HttpMethodTrait
35-
if (pathItem) {
36-
pathItem.httpMethod = it
37-
ops << pathItem
34+
Operation op = item."${it.method}"
35+
if (op) {
36+
ops.put (it, op)
3837
}
3938
}
4039

4140
ops
4241
}
42+
4343
}

0 commit comments

Comments
 (0)