You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/modules/ROOT/pages/processor/configuration.adoc
+8-1Lines changed: 8 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,7 @@ openapi-processor-mapping: v2
13
13
options:
14
14
package-name: io.openapiprocessor.sample
15
15
model-name-suffix: Resource
16
+
one-of-interface: true
16
17
bean-validation: true
17
18
format-code: false
18
19
javadoc: true
@@ -33,7 +34,7 @@ Interfaces and models will be generated into the `api` and `model` subpackages o
33
34
** so the final package name of the generated interfaces will be `"$\{package-name\}.api"`,
34
35
** and the final package name of the generated models will be `"$\{package-name\}.model"`
35
36
36
-
* `model-suffix-name` (**optional**, default is empty).See xref:_model_name_suffix[below].
37
+
* `model-suffix-name` (**optional**, default is empty).See xref:_model_name_suffix[below].
37
38
38
39
* `bean-validation` (**optional**, `true` or `false`) enables generation of bean validation
39
40
annotations. Default is `false`. See link:{bean-validation}[Bean Validation Specification, window="_blank"].
@@ -42,6 +43,8 @@ annotations. Default is `false`. See link:{bean-validation}[Bean Validation Spec
42
43
43
44
* `format-code` (**optional**, `true` or `false`) enable or disable the code formatter: Default is `true`.
44
45
46
+
* `one-of-interface` (**optional**, `true` or `false`) enables generation of marker interfaces for `oneOf` objects. See xref:processor/one-of-interface.adoc#_marker_interfaces[oneOf marker interfaces].
47
+
45
48
[#_model_name_suffix]
46
49
=== model name suffix:
47
50
@@ -134,6 +137,10 @@ public class BarResource { // <4>
134
137
<3> the class name of the `Foo` schema got the configured `Resource` suffix
135
138
<4> the class name of the `BarResource` is identical to the original schema name. Since the existing suffix is equal to `model-name-suffix` it is ignored. Otherwise, This prevents funny class names like `BarResourceResource`.
136
139
140
+
141
+
142
+
143
+
137
144
== map:
138
145
139
146
Using type mapping we can tell the processor to map types (schemas) from an `openapi.yaml`
Generating model classes from an `oneOf` has the challenge to handle a number of usually unrelated objects with different properties.
4
+
5
+
Java has no way to define a class member that can have multiple unrelated types (e.g. it can be of class `Foo` or class `Bar`), except using `Object`.
6
+
7
+
This is the default behavior of the processor.
8
+
9
+
The problem with `Object` is that it doesn't provide any information at all. You have to know (from the OpenAPI) what that `Object` could be.
10
+
11
+
To improve usability the processor is able to generate marker interfaces to provide a bit more information than `Object`.
12
+
13
+
[#_marker_interfaces]
14
+
== marker interfaces
15
+
16
+
[.badge .badge-since]+since 2022.3+
17
+
18
+
Generation of marker interfaces is enabled by setting the `one-of-interface` option to `true` (See xref:processor/configuration.adoc[configuration]). For backward compatibility it is `false` by default.
19
+
20
+
[source,yaml]
21
+
----
22
+
openapi-processor-mapping: v2
23
+
24
+
options:
25
+
one-of-interface: true
26
+
----
27
+
28
+
The processor will now create a marker interface for a `oneOf` of `object` s that is implemented by all `object` s in the `oneOf` list.
29
+
30
+
Here is an example. The response is an object `Foo` with a `foo` property that can have the type `Foo` or `Bar`.
31
+
32
+
[source,yaml]
33
+
----
34
+
openapi: 3.0.3
35
+
info:
36
+
title: oneOf marker interface
37
+
version: 1.0.0
38
+
39
+
paths:
40
+
/foo:
41
+
get:
42
+
responses:
43
+
'200':
44
+
description: oneOf
45
+
content:
46
+
application/json:
47
+
schema:
48
+
$ref: '#/components/schemas/Foo'
49
+
50
+
components:
51
+
schemas:
52
+
53
+
Foo:
54
+
type: object
55
+
properties:
56
+
foo:
57
+
$ref: '#/components/schemas/FooOneOf'
58
+
59
+
FooOneOf:
60
+
oneOf:
61
+
- $ref: '#/components/schemas/Foo'
62
+
- $ref: '#/components/schemas/Bar'
63
+
64
+
# omitted description of Foo & Bar
65
+
----
66
+
67
+
The processor generates the class `Foo` as
68
+
69
+
[source,java]
70
+
----
71
+
// simplified
72
+
public class Foo {
73
+
private FooOneOf foo;
74
+
}
75
+
----
76
+
77
+
with the type `FooOneOf` instead of `Object`. `FooOneOf` is the marker interface:
78
+
79
+
[source,java]
80
+
----
81
+
public interface FooOneOf {}
82
+
----
83
+
84
+
The two model classes `Foo` & `Bar` implement the marker interface:
85
+
86
+
[source,java]
87
+
----
88
+
// simplified
89
+
public class Foo implements FooOneOf { /* ... */ }
90
+
----
91
+
92
+
[source,java]
93
+
----
94
+
// simplified
95
+
public class Bar implements FooOneOf { /* ... */ }
96
+
----
97
+
98
+
Which is better than having `foo` just as `Object`. The marker interface helps to find the possible types of `foo`.
0 commit comments