Skip to content

Commit 1e49177

Browse files
authored
Merge branch 'master' into groovy-3
2 parents 7a90d39 + 96d3267 commit 1e49177

77 files changed

Lines changed: 2497 additions & 537 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.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
out
22
build
3+
.gradle

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The structure looks like this:
3636
AnotherModelClass.java
3737
.. more model files ..
3838

39-
The `mapping.yaml` contains the type mapping information and is an optional file.
39+
The `mapping.yaml` contains the type mapping information.
4040

4141
See the [existing integration tests][generatr-int-resources] for a couple of examples.
4242

build.gradle

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ plugins {
33
id 'java-library'
44
id 'maven-publish'
55
id 'com.jfrog.bintray' version '1.8.4'
6+
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
7+
id 'org.jetbrains.dokka' version '0.10.0'
68
id 'org.unbroken-dome.test-sets' version '2.2.1'
79
id "com.github.ben-manes.versions" version "0.27.0"
810
}
911

1012
group 'com.github.hauner.openapi'
11-
version '1.0.0.M5'
13+
version '1.0.0.M8'
1214

1315
sourceCompatibility = JavaVersion.VERSION_1_8
1416
targetCompatibility = JavaVersion.VERSION_1_8
@@ -23,6 +25,7 @@ ext {
2325

2426
repositories {
2527
mavenCentral()
28+
jcenter()
2629
maven {
2730
url "https://oss.sonatype.org/content/repositories/snapshots"
2831
}
@@ -43,6 +46,7 @@ check.dependsOn testInt
4346

4447
dependencies {
4548
implementation 'org.codehaus.groovy:groovy:3.0.0'
49+
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
4650
implementation 'io.swagger.parser.v3:swagger-parser:2.0.17'
4751
implementation 'com.google.googlejavaformat:google-java-format:1.7'
4852
compileOnly "com.github.hauner.openapi:openapi-generatr-api:$generatrApiVersion"
@@ -56,23 +60,33 @@ dependencies {
5660

5761
testIntImplementation "com.github.hauner.openapi:openapi-generatr-api:$generatrApiVersion"
5862
testIntImplementation 'io.github.java-diff-utils:java-diff-utils:4.5'
63+
testIntRuntimeOnly 'org.slf4j:slf4j-simple:1.7.30'
64+
// testIntRuntimeOnly 'org.slf4j:slf4j-nop:1.6.1'
5965
}
6066

6167
task sourcesJar(type: Jar, dependsOn: classes) {
62-
archiveClassifier = 'sources'
68+
archiveClassifier.set ('sources')
6369
from sourceSets.main.allSource
6470
}
6571

66-
task javadocJar(type: Jar, dependsOn: groovydoc) {
67-
archiveClassifier = 'javadoc'
68-
from groovydoc.destinationDir
72+
task javadocJar(type: Jar, dependsOn: [groovydoc, dokka]) {
73+
archiveClassifier.set ('javadoc')
74+
from "$buildDir/docs"
6975
}
7076

7177
artifacts {
7278
archives sourcesJar
7379
archives javadocJar
7480
}
7581

82+
groovydoc {
83+
destinationDir file("$buildDir/docs/groovy")
84+
}
85+
86+
dokka {
87+
outputFormat = 'html'
88+
outputDirectory = "$buildDir/docs/kotlin"
89+
}
7690

7791
bintray {
7892
user = bintrayUser

docs/Gemfile.lock

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ GEM
2626
ffi (>= 1.3.0)
2727
eventmachine (1.2.7)
2828
execjs (2.7.0)
29-
faraday (0.17.1)
29+
faraday (1.0.0)
3030
multipart-post (>= 1.2, < 3)
31-
ffi (1.11.3)
31+
ffi (1.12.2)
3232
forwardable-extended (2.6.0)
3333
gemoji (3.0.1)
34-
github-pages (203)
34+
github-pages (204)
3535
github-pages-health-check (= 1.16.1)
3636
jekyll (= 3.8.5)
3737
jekyll-avatar (= 0.7.0)
@@ -40,7 +40,7 @@ GEM
4040
jekyll-default-layout (= 0.1.4)
4141
jekyll-feed (= 0.13.0)
4242
jekyll-gist (= 1.5.0)
43-
jekyll-github-metadata (= 2.12.1)
43+
jekyll-github-metadata (= 2.13.0)
4444
jekyll-mentions (= 1.5.1)
4545
jekyll-optional-front-matter (= 0.3.2)
4646
jekyll-paginate (= 1.1.0)
@@ -117,8 +117,8 @@ GEM
117117
jekyll (>= 3.7, < 5.0)
118118
jekyll-gist (1.5.0)
119119
octokit (~> 4.2)
120-
jekyll-github-metadata (2.12.1)
121-
jekyll (~> 3.4)
120+
jekyll-github-metadata (2.13.0)
121+
jekyll (>= 3.4, < 5.0)
122122
octokit (~> 4.0, != 4.4.0)
123123
jekyll-mentions (1.5.1)
124124
html-pipeline (~> 2.3)
@@ -202,11 +202,12 @@ GEM
202202
jekyll (>= 3.5, < 5.0)
203203
jekyll-feed (~> 0.9)
204204
jekyll-seo-tag (~> 2.1)
205-
minitest (5.13.0)
205+
minitest (5.14.0)
206206
multipart-post (2.1.1)
207207
nokogiri (1.10.7)
208208
mini_portile2 (~> 2.4.0)
209-
octokit (4.14.0)
209+
octokit (4.15.0)
210+
faraday (>= 0.9)
210211
sawyer (~> 0.8.0, >= 0.5.3)
211212
pathutil (0.16.2)
212213
forwardable-extended (~> 2.6)
@@ -217,7 +218,7 @@ GEM
217218
rouge (3.13.0)
218219
ruby-enum (0.7.2)
219220
i18n
220-
rubyzip (2.0.0)
221+
rubyzip (2.2.0)
221222
safe_yaml (1.0.5)
222223
sass (3.7.4)
223224
sass-listen (~> 4.0.0)
@@ -234,7 +235,7 @@ GEM
234235
ethon (>= 0.9.0)
235236
tzinfo (1.2.6)
236237
thread_safe (~> 0.1)
237-
unicode-display_width (1.6.0)
238+
unicode-display_width (1.6.1)
238239
zeitwerk (2.2.2)
239240

240241
PLATFORMS

docs/_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ exclude:
1515
- README.md
1616
- SCRATCH.md
1717
- api.md
18+
- gradle-obsolete.md
1819

1920
##
2021
## "just-the-docs" theme options

docs/_sass/overrides.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@
2828

2929
border-left: 0.5em solid $yellow-000;
3030
}
31+
32+
.deprecated {
33+
margin-left: 1em;
34+
margin-right: 1em;
35+
padding: 0.8em;
36+
37+
border-left: 0.5em solid $red-000;
38+
}

docs/generatr/configuration.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
layout: default
3+
title: Configuration
4+
parent: The generatr
5+
nav_order: 4
6+
---
7+
8+
# Configuration
9+
10+
The configuration of the generatr is given in the `mapping.yaml`. It does contain some general options
11+
and the type mapping information.
12+
13+
14+
A mapping yaml looks like this
15+
16+
options:
17+
package-name: com.github.hauner.openapi
18+
bean-validation: true
19+
20+
map:
21+
# see link below
22+
23+
24+
## options:
25+
26+
- `package-name`: (**required**) the root package name of the generated interfaces & models. The package
27+
folder tree will be created inside the `targetDir` (see [using gradle][docs-gradle]).
28+
29+
Interfaces and models will be generated into the `api` and `model` subpackages of `package-name`.
30+
31+
- so the final package name of the generated interfaces will be `"${package-name}.api"`
32+
- and the final package name of the generated models will be `"${package-name}.model"`
33+
{: .mb-5 }
34+
35+
- `bean-validation` (**optional**, `true` or `false`) enables generation of bean validation annotations.
36+
Defaults to `false`. See [Bean Validation][bean-validation]{:target="_blank"}.
37+
38+
## map:
39+
40+
Using type mapping we can tell the generatr to map types (schemas) from an openapi.yaml description to
41+
a specific existing java type instead of generating a model class from the source OpenAPI type.
42+
43+
The type mapping is described in [java type mapping][docs-mapping].
44+
45+
46+
[docs-mapping]: /openapi-generatr-spring/mapping/
47+
[docs-gradle]: /openapi-generatr-spring/gradle.html
48+
[bean-validation]: https://beanvalidation.org/

docs/generatr/endpoints.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
layout: default
3+
title: Endpoints
4+
parent: The generatr
5+
nav_order: 6
6+
---
7+
8+
# Endpoints
9+
10+
A simple path of the OpenAPI description will usually produce a single endpoint method in the target
11+
interface as described in the introduction.
12+
13+
OpenAPI allows us to define more complex apis that behave differently based on the request header.
14+
For example the following api definition can return its response in different format. As json or as
15+
plain text:
16+
17+
```yaml
18+
openapi: 3.0.2
19+
info:
20+
title: test multiple response content types
21+
version: 1.0.0
22+
23+
paths:
24+
/foo:
25+
get:
26+
responses:
27+
'200':
28+
description: json or plain text result
29+
content:
30+
application/json:
31+
schema:
32+
$ref: '#/components/schemas/Foo'
33+
text/plain:
34+
schema:
35+
type: string
36+
default:
37+
description: error
38+
content:
39+
application/xml:
40+
schema:
41+
$ref: '#/components/schemas/Error'
42+
43+
components:
44+
45+
schemas:
46+
Foo:
47+
type: object
48+
properties:
49+
bar:
50+
type: string
51+
52+
Error:
53+
type: object
54+
properties:
55+
error:
56+
type: string
57+
```
58+
59+
A client request uses the request `Accept` header to tell the api which result content types it can
60+
handle.
61+
62+
Using a single endpoint method it has to decide which response to create. This leads to some boring
63+
`if/else` code. To avoid this the generatr creates one endpoint method for each possible response.
64+
65+
For the example above it creates the following interface:
66+
67+
```java
68+
package generated.api;
69+
70+
import org.springframework.http.ResponseEntity;
71+
import org.springframework.web.bind.annotation.GetMapping;
72+
73+
public interface Api {
74+
75+
@GetMapping(
76+
path = "/foo",
77+
produces = {"application/json", "application/xml"})
78+
ResponseEntity<?> getFooApplicationJson();
79+
80+
@GetMapping(
81+
path = "/foo",
82+
produces = {"text/plain", "application/xml"})
83+
ResponseEntity<?> getFooTextPlain();
84+
85+
}
86+
```
87+
88+
The apis normal response (status '200') can return the result as json or as plain text which leads
89+
to two methods for the same endpoint but with a different `produces` list in the mapping annotation.
90+
91+
One method which is called when json is requested and one when plain text is requested. Spring will
92+
take care of selecting the correct endpoint.
93+
94+
In the (contrived) example our api does also define another content type for all other result status
95+
codes: xml.
96+
97+
Both endpoints need to handle the success case (json or text) and the error (xml) case. So both
98+
mappings contain the xml content type. With the different responses the `ResponseEntity` return type
99+
is now the *unknown* type. Successful response and error properties are different which prevents an
100+
explicit type as the result type.

docs/generatr/models.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: default
33
title: Models
44
parent: The generatr
5-
nav_order: 6
5+
nav_order: 7
66
---
77

88
# Models

docs/generatr/responses.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,39 @@ parent: The generatr
55
nav_order: 20
66
---
77

8-
## Responses
8+
# Responses
99

1010
All generated endpoints have a [`ResponseEntity<>`][spring-responseentity] result. This allows an endpoint
1111
implementation full control of the response at the cost of having to provide a `ResponseEntity` even if
1212
it could just return its pojo result.
1313

14+
Here are a few examples:
15+
16+
public ResponseEntity<String> getFoo() {
17+
return ResponseEntity.ok("foo");
18+
}
19+
20+
public ResponseEntity<Long> getBar() {
21+
return ResponseEntity.ok(5L);
22+
}
23+
24+
public ResponseEntity<ResourceObject> getResourceObject() {
25+
return ResponseEntity.ok(resourceObject);
26+
}
27+
28+
29+
Depending on the number of defined response content types the parameter of the `ResponseEntity<>` will
30+
be either the java type or the *unknown type*.
31+
32+
33+
| # responses | ResponseEntity<> |
34+
|:-------------:|:------------------------------------:|
35+
| one | `ResponseEntity<java type>` |
36+
| multiple | `ResponseEntity<?>` |
37+
38+
39+
40+
41+
42+
1443
[spring-responseentity]: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

0 commit comments

Comments
 (0)