Skip to content

Commit dc15628

Browse files
authored
Merge pull request #97 from hauner/map-result
`result` & `single` mapping.
2 parents 8924c4a + d68d039 commit dc15628

63 files changed

Lines changed: 1535 additions & 437 deletions

File tree

Some content is hidden

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

build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
plugins {
2+
id 'jacoco'
23
id 'groovy'
34
id 'java-library'
45
id 'maven-publish'
@@ -79,6 +80,21 @@ dependencies {
7980
// testIntRuntimeOnly 'org.slf4j:slf4j-nop:1.6.1'
8081
}
8182

83+
84+
tasks.withType(Test) {
85+
finalizedBy jacocoTestReport
86+
}
87+
88+
89+
jacoco {
90+
toolVersion = "0.8.5"
91+
}
92+
93+
project.jacocoTestReport {
94+
getExecutionData().setFrom(fileTree(buildDir).include("/jacoco/*.exec"))
95+
}
96+
97+
8298
task sourcesJar(type: Jar, dependsOn: classes) {
8399
archiveClassifier.set ('sources')
84100
from sourceSets.main.allSource
@@ -103,6 +119,7 @@ dokka {
103119
outputDirectory = "$buildDir/docs/kotlin"
104120
}
105121

122+
106123
bintray {
107124
user = bintrayUser
108125
key = bintrayKey
@@ -121,6 +138,7 @@ bintray {
121138
}
122139
}
123140

141+
124142
publishing {
125143
publications {
126144
processor (MavenPublication) {

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

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717
package com.github.hauner.openapi.spring.converter
1818

1919
import com.github.hauner.openapi.spring.converter.mapping.AddParameterTypeMapping
20-
import com.github.hauner.openapi.spring.converter.mapping.AmbiguousTypeMappingException
21-
import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
2220
import com.github.hauner.openapi.spring.converter.mapping.Mapping
23-
import com.github.hauner.openapi.spring.converter.mapping.MappingSchema
2421
import com.github.hauner.openapi.spring.converter.mapping.TargetType
2522
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
26-
import com.github.hauner.openapi.spring.converter.schema.SchemaInfo
2723
import com.github.hauner.openapi.spring.model.Api
2824
import com.github.hauner.openapi.spring.model.DataTypes
2925
import com.github.hauner.openapi.spring.model.Endpoint
3026
import com.github.hauner.openapi.spring.model.Interface
3127
import com.github.hauner.openapi.spring.model.RequestBody as ModelRequestBody
3228
import com.github.hauner.openapi.spring.model.datatypes.MappedDataType
29+
import com.github.hauner.openapi.spring.model.datatypes.NoneDataType
3330
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
3431
import com.github.hauner.openapi.spring.model.parameters.AdditionalParameter
3532
import com.github.hauner.openapi.spring.model.parameters.CookieParameter
@@ -63,27 +60,11 @@ class ApiConverter {
6360
public static final String INTERFACE_DEFAULT_NAME = ''
6461

6562
private DataTypeConverter dataTypeConverter
63+
private ResultDataTypeWrapper dataTypeWrapper
64+
private SingleDataTypeWrapper singleDataTypeWrapper
65+
private MappingFinder mappingFinder
6666
private ApiOptions options
6767

68-
class MappingSchemaEndpoint implements MappingSchema {
69-
String path
70-
71-
@Override
72-
String getPath () {
73-
path
74-
}
75-
76-
@Override
77-
String getName () {
78-
null
79-
}
80-
81-
@Override
82-
String getContentType () {
83-
null
84-
}
85-
}
86-
8768
ApiConverter(ApiOptions options) {
8869
this.options = options
8970

@@ -92,6 +73,9 @@ class ApiConverter {
9273
}
9374

9475
dataTypeConverter = new DataTypeConverter(this.options)
76+
dataTypeWrapper = new ResultDataTypeWrapper(this.options)
77+
singleDataTypeWrapper = new SingleDataTypeWrapper(this.options)
78+
mappingFinder = new MappingFinder (typeMappings: this.options.typeMappings)
9579
}
9680

9781
/**
@@ -164,7 +148,7 @@ class ApiConverter {
164148
ep.parameters.add (createParameter (ep.path, parameter, dataTypes, resolver))
165149
}
166150

167-
List<Mapping> addMappings = findAdditionalParameter (ep)
151+
List<Mapping> addMappings = mappingFinder.findAdditionalEndpointParameter (ep.path)
168152
addMappings.each {
169153
ep.parameters.add (createAdditionalParameter (ep.path, it as AddParameterTypeMapping, dataTypes, resolver))
170154
}
@@ -200,18 +184,14 @@ class ApiConverter {
200184
def httpStatus = responseEntry.key
201185
def httpResponse = responseEntry.value
202186

203-
if (!httpResponse.content) {
204-
ep.addResponses (httpStatus, [ModelResponse.EMPTY])
205-
} else {
206-
List<ModelResponse> results = createResponses (
207-
ep.path,
208-
httpStatus,
209-
httpResponse,
210-
dataTypes,
211-
resolver)
212-
213-
ep.addResponses (httpStatus, results)
214-
}
187+
List<ModelResponse> results = createResponses (
188+
ep.path,
189+
httpStatus,
190+
httpResponse,
191+
dataTypes,
192+
resolver)
193+
194+
ep.addResponses (httpStatus, results)
215195
}
216196

217197
}
@@ -255,10 +235,11 @@ class ApiConverter {
255235

256236
private ModelRequestBody createRequestBody (String contentType, SchemaInfo info, boolean required, DataTypes dataTypes) {
257237
DataType dataType = dataTypeConverter.convert (info, dataTypes)
238+
DataType singleDataType = singleDataTypeWrapper.wrap (dataType, info)
258239

259240
new ModelRequestBody(
260241
contentType: contentType,
261-
requestBodyType: dataType,
242+
requestBodyType: singleDataType,
262243
required: required)
263244
}
264245

@@ -274,8 +255,20 @@ class ApiConverter {
274255
}
275256

276257
private List<ModelResponse> createResponses (String path, String httpStatus, Response response, DataTypes dataTypes, RefResolver resolver) {
277-
def responses = []
258+
if (!response.content) {
259+
def info = new SchemaInfo (path: path)
260+
261+
DataType dataType = new NoneDataType()
262+
DataType singleDataType = singleDataTypeWrapper.wrap (dataType, info)
263+
DataType resultDataType = dataTypeWrapper.wrap (singleDataType, info)
278264

265+
def resp = new ModelResponse (
266+
responseType: resultDataType)
267+
268+
return [resp]
269+
}
270+
271+
def responses = []
279272
response.content.each { Map.Entry<String, MediaType> contentEntry ->
280273
def contentType = contentEntry.key
281274
def mediaType = contentEntry.value
@@ -289,28 +282,19 @@ class ApiConverter {
289282
resolver: resolver)
290283

291284
DataType dataType = dataTypeConverter.convert (info, dataTypes)
285+
DataType singleDataType = singleDataTypeWrapper.wrap (dataType, info)
286+
DataType resultDataType = dataTypeWrapper.wrap (singleDataType, info)
292287

293288
def resp = new ModelResponse (
294289
contentType: contentType,
295-
responseType: dataType)
290+
responseType: resultDataType)
296291

297292
responses.add (resp)
298293
}
299294

300295
responses
301296
}
302297

303-
private List<Mapping> findAdditionalParameter (Endpoint ep) {
304-
def addMappings = options.typeMappings.findAll {
305-
it.matches (Mapping.Level.ENDPOINT, new MappingSchemaEndpoint (path: ep.path))
306-
}.collectMany {
307-
it.childMappings
308-
}.findAll {
309-
it.matches (Mapping.Level.ADD, null as MappingSchema)
310-
}
311-
addMappings as List<Mapping>
312-
}
313-
314298
private String getInlineRequestBodyName (String path) {
315299
Identifier.toClass (path) + 'RequestBody'
316300
}
@@ -319,22 +303,8 @@ class ApiConverter {
319303
Identifier.toClass (path) + 'Response' + httpStatus
320304
}
321305

322-
323306
private boolean isExcluded (String path) {
324-
def endpointMatches = options.typeMappings.findAll {
325-
it.matches (Mapping.Level.ENDPOINT, new MappingSchemaEndpoint(path: path))
326-
}
327-
328-
if (!endpointMatches.empty) {
329-
if (endpointMatches.size () != 1) {
330-
throw new AmbiguousTypeMappingException (endpointMatches)
331-
}
332-
333-
def match = endpointMatches.first () as EndpointTypeMapping
334-
return match.exclude
335-
}
336-
337-
false
307+
mappingFinder.isExcludedEndpoint (path)
338308
}
339309

340310
private String getInterfaceName (def op, boolean excluded) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ApiOptions {
4444
* - interfaces => "${packageName}.api"
4545
* - models => "${packageName}.model"
4646
*/
47-
String packageName = 'generatr'
47+
String packageName = 'processor'
4848

4949
/**
5050
* provide enabling Bean Validation (JSR303) annotations. Default is false (disabled)

0 commit comments

Comments
 (0)