Skip to content

Commit a59818d

Browse files
committed
Merge remote-tracking branch 'origin/master' into 1.0.x
2 parents 4838c52 + 57f0cee commit a59818d

File tree

15 files changed

+365
-115
lines changed

15 files changed

+365
-115
lines changed

build.gradle

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ plugins {
33
id 'groovy'
44
id 'java-library'
55
id 'maven-publish'
6-
id 'com.jfrog.bintray' version '1.8.4'
7-
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
6+
id 'com.jfrog.bintray' version '1.8.5'
7+
id 'org.jetbrains.kotlin.jvm' version '1.3.72'
88
id 'org.jetbrains.dokka' version '0.10.1'
9-
id 'org.unbroken-dome.test-sets' version '2.2.1'
10-
id "com.github.ben-manes.versions" version "0.27.0"
9+
id 'org.unbroken-dome.test-sets' version '3.0.1'
10+
id "com.github.ben-manes.versions" version "0.28.0"
1111
}
1212

1313
group 'com.github.hauner.openapi'
14-
version '1.0.0.M13'
14+
version '1.0.0.M14'
15+
16+
java {
17+
sourceCompatibility = JavaVersion.VERSION_1_8
18+
targetCompatibility = JavaVersion.VERSION_1_8
19+
}
1520

16-
sourceCompatibility = JavaVersion.VERSION_1_8
17-
targetCompatibility = JavaVersion.VERSION_1_8
1821

1922
ext {
2023
processorApiVersion = '1.0.0.M4'
@@ -56,22 +59,31 @@ compileTestGroovy {
5659
classpath += files(compileKotlin.destinationDir)
5760
}
5861

62+
compileKotlin {
63+
kotlinOptions.jvmTarget = "1.8"
64+
}
5965

6066
dependencies {
61-
implementation 'org.codehaus.groovy:groovy:2.5.4'
62-
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
63-
implementation 'io.swagger.parser.v3:swagger-parser:2.0.17'
64-
implementation 'org.openapi4j:openapi-parser:0.8'
67+
implementation 'org.codehaus.groovy:groovy:2.5.10'
68+
implementation 'org.codehaus.groovy:groovy-nio:2.5.10'
69+
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
70+
71+
implementation ('io.swagger.parser.v3:swagger-parser:2.0.19') {
72+
exclude group: 'io.swagger.parser.v3', module: 'swagger-parser-v2-converter'
73+
exclude group: 'io.swagger.core.v3', module: 'swagger-annotations'
74+
}
75+
implementation 'org.openapi4j:openapi-parser:0.9'
6576
implementation 'com.google.googlejavaformat:google-java-format:1.7'
6677
compileOnly "com.github.hauner.openapi:openapi-processor-api:$processorApiVersion"
6778

79+
testImplementation 'com.google.jimfs:jimfs:1.1'
6880
testImplementation ('org.spockframework:spock-core:2.0-M2-groovy-2.5') {
6981
exclude group: 'org.codehaus.groovy'
7082
}
7183
testImplementation ('org.spockframework:spock-junit4:2.0-M2-groovy-2.5') {
7284
exclude group: 'org.codehaus.groovy'
7385
}
74-
testImplementation 'net.bytebuddy:byte-buddy:1.10.7'
86+
testImplementation 'net.bytebuddy:byte-buddy:1.10.9'
7587
testImplementation 'ch.qos.logback:logback-classic:1.2.3'
7688

7789
testIntImplementation "com.github.hauner.openapi:openapi-processor-api:$processorApiVersion"
@@ -80,7 +92,6 @@ dependencies {
8092
// testIntRuntimeOnly 'org.slf4j:slf4j-nop:1.6.1'
8193
}
8294

83-
8495
tasks.withType(Test) {
8596
finalizedBy jacocoTestReport
8697
}

docs/modules/ROOT/pages/index.adoc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ link:{oaps-license}[image:{badge-license}[]]
1717
*openapi-processor-spring* is an link:{openapi}[OpenAPI] interface & model only java code generator
1818
for link:{springboot}[Spring Boot].
1919

20-
It supports an API first approach where you API is explicitly defined and documented with OpenAPI
21-
before it gets implemented.
20+
It supports an approach where you explicitly define and document your Service API (using OpenAPI)
21+
with the Interface to the outside and its usage in mind before you implement it. You do not derive
22+
the API later from the implementation and its implicit design. (of course, adapt as you go...)
23+
24+
The advantages are:
25+
26+
* you have a single place to maintain the api which makes it easier to create a consistent api
27+
and keep the overview.
28+
* it is easy to document in plain text. You can use markdown in the OpenAPI `description`
29+
properties.
2230
2331
The processor generates java interfaces based on the endpoint description of the API and simple POJO
2432
classes for parameter or response objects defined in the API. The processor adds all the required

src/main/groovy/com/github/hauner/openapi/spring/parser/Parser.groovy

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,38 @@ package com.github.hauner.openapi.spring.parser
1818

1919
import com.github.hauner.openapi.spring.parser.swagger.Parser as Swagger
2020
import com.github.hauner.openapi.spring.parser.openapi4j.Parser as OpenApi4J
21+
import org.slf4j.Logger
22+
import org.slf4j.LoggerFactory
2123

2224
/**
2325
* OpenAPI parser abstraction. Supports swagger or openapi4 parser.
2426
*
2527
* @author Martin Hauner
2628
*/
2729
class Parser {
30+
private static final Logger LOG = LoggerFactory.getLogger (Parser.class)
2831

2932
OpenApi parse (Map<String, ?> processorOptions) {
3033
def apiPath = processorOptions.apiPath as String
3134

3235
switch (processorOptions.parser as ParserType) {
3336

3437
case ParserType.SWAGGER:
35-
println "info: using SWAGGER parser"
38+
LOG.info ("using SWAGGER parser")
3639

3740
def parser = new Swagger ()
3841
return parser.parse (apiPath)
3942

4043
case ParserType.OPENAPI4J:
41-
println "info: using OPENAPI4J parser"
44+
LOG.info ("using OPENAPI4J parser")
4245

4346
def parser = new OpenApi4J ()
4447
return parser.parse (apiPath)
4548

4649
default:
4750
if (processorOptions.parser != null) {
48-
println "warning: unknown parser type: ${processorOptions.parser}"
49-
println "warning: available parsers: SWAGGER, OPENAPI4J"
51+
LOG.warn ("unknown parser type: {}", processorOptions.parser)
52+
LOG.warn ("available parsers: SWAGGER, OPENAPI4J")
5053
}
5154

5255
def parser = new Swagger ()

src/main/groovy/com/github/hauner/openapi/spring/parser/openapi4j/Parser.groovy

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ import org.openapi4j.parser.validation.v3.OpenApi3Validator
3030
class Parser {
3131

3232
ParserOpenApi parse (String apiPath) {
33+
if (!hasScheme (apiPath)) {
34+
apiPath = "file://${apiPath}"
35+
}
3336

3437
OpenApi3 api = new OpenApi3Parser ()
35-
.parse (new File (apiPath), true)
38+
.parse (new URL (apiPath), true)
3639

3740
ValidationResults results = OpenApi3Validator
3841
.instance ()
@@ -41,4 +44,8 @@ class Parser {
4144
new OpenApi (api, results)
4245
}
4346

47+
boolean hasScheme (String path) {
48+
path.indexOf ("://") > -1
49+
}
50+
4451
}

src/main/groovy/com/github/hauner/openapi/spring/parser/swagger/Parser.groovy

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ import io.swagger.v3.parser.core.models.ParseOptions
2222
import io.swagger.v3.parser.core.models.SwaggerParseResult
2323

2424
/**
25-
* openapi4j parser.
25+
* swagger parser.
2626
*
2727
* @author Martin Hauner
2828
*/
2929
class Parser {
3030

3131
ParserOpenApi parse (String apiPath) {
32+
if (!hasScheme (apiPath)) {
33+
apiPath = "file://${apiPath}"
34+
}
35+
3236
ParseOptions opts = new ParseOptions(
3337
// loads $refs to other files into #/components/schema and replaces the $refs to the
3438
// external files with $refs to #/components/schema.
@@ -40,4 +44,8 @@ class Parser {
4044
new OpenApi(result)
4145
}
4246

47+
boolean hasScheme (String path) {
48+
path.indexOf ("://") > -1
49+
}
50+
4351
}

src/main/groovy/com/github/hauner/openapi/spring/processor/MappingReader.groovy

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ class MappingReader {
3737
return null
3838
}
3939

40-
String mapping = typeMappings
41-
if (isFileName (typeMappings)) {
40+
def mapping
41+
if (isUrl (typeMappings)) {
42+
mapping = new URL (typeMappings).text
43+
} else if (isFileName (typeMappings)) {
4244
mapping = new File (typeMappings).text
45+
} else {
46+
mapping = typeMappings
4347
}
4448

4549
def mapper = createYamlParser ()
46-
4750
mapper.readValue (mapping, Mapping)
4851
}
4952

@@ -61,4 +64,12 @@ class MappingReader {
6164
name.endsWith ('.yaml') || name.endsWith ('.yml')
6265
}
6366

67+
private boolean isUrl (String source) {
68+
try {
69+
new URL (source)
70+
} catch (MalformedURLException ignore) {
71+
false
72+
}
73+
}
74+
6475
}

src/main/groovy/com/github/hauner/openapi/spring/writer/ApiWriter.groovy

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import com.google.googlejavaformat.java.Formatter
2525
import com.google.googlejavaformat.java.JavaFormatterOptions
2626
import groovy.util.logging.Slf4j
2727

28+
import java.nio.file.Files
29+
import java.nio.file.Path
30+
2831
import static com.github.hauner.openapi.support.Identifier.toClass
2932

3033
/**
@@ -40,8 +43,8 @@ class ApiWriter {
4043
DataTypeWriter dataTypeWriter
4144
StringEnumWriter enumWriter
4245

43-
File apiFolder
44-
File modelFolder
46+
Path apiFolder
47+
Path modelFolder
4548

4649
Formatter formatter
4750

@@ -69,22 +72,22 @@ class ApiWriter {
6972
createTargetFolders ()
7073

7174
api.interfaces.each {
72-
def target = new File (apiFolder, "${it.interfaceName}.java")
73-
def writer = new FileWriter(target)
75+
def target = apiFolder.resolve ("${it.interfaceName}.java")
76+
def writer = new BufferedWriter (new PathWriter(target))
7477
writeInterface (writer, it)
7578
writer.close ()
7679
}
7780

7881
api.models.objectDataTypes.each {
79-
def target = new File (modelFolder, "${it.name}.java")
80-
def writer = new FileWriter(target)
82+
def target = modelFolder.resolve ("${it.name}.java")
83+
def writer = new BufferedWriter (new PathWriter(target))
8184
writeDataType (writer, it)
8285
writer.close ()
8386
}
8487

8588
api.models.enumDataTypes.each {
86-
def target = new File (modelFolder, "${it.name}.java")
87-
def writer = new FileWriter(target)
89+
def target = modelFolder.resolve ("${it.name}.java")
90+
def writer = new BufferedWriter (new PathWriter(target))
8891
writeEnumDataType (writer, it)
8992
writer.close ()
9093
}
@@ -132,23 +135,25 @@ class ApiWriter {
132135
log.debug ('creating target folders: {}', rootPkg)
133136

134137
apiFolder = createTargetPackage (apiPkg)
135-
log.debug ('created target folder: {}', apiFolder.absolutePath)
138+
log.debug ('created target folder: {}', apiFolder.toAbsolutePath ().toString ())
136139

137140
modelFolder = createTargetPackage (modelPkg)
138-
log.debug ('created target folder: {}', modelFolder.absolutePath)
141+
log.debug ('created target folder: {}', modelFolder.toAbsolutePath ().toString ())
139142
}
140143

141-
private File createTargetPackage (String apiPkg) {
142-
def folder = new File ([options.targetDir, apiPkg].join (File.separator))
143-
if (folder.exists () && folder.isDirectory ()) {
144-
return folder
144+
private Path createTargetPackage (String apiPkg) {
145+
String root = options.targetDir
146+
if (!hasScheme (root)) {
147+
root = "file://${root}"
145148
}
146149

147-
def success = folder.mkdirs ()
148-
if (!success) {
149-
log.error ('failed to create package {}', folder)
150-
}
151-
folder
150+
def target = Path.of (new URL ([root, apiPkg].join ('/')).toURI ())
151+
Files.createDirectories (target)
152+
target
153+
}
154+
155+
private boolean hasScheme (String source) {
156+
source.indexOf ("://") > -1
152157
}
153158

154159
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2019-2020 the original authors
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+
17+
package com.github.hauner.openapi.spring.writer
18+
19+
import java.nio.file.Files
20+
import java.nio.file.Path
21+
22+
/**
23+
* Path based Writer
24+
*
25+
* @author Martin Haner
26+
*/
27+
class PathWriter extends OutputStreamWriter {
28+
29+
PathWriter(Path target) throws IOException {
30+
super(Files.newOutputStream (target));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)