1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.rest.annotation;
18
19 import static org.apache.juneau.TestUtils.*;
20 import static org.apache.juneau.junit.bct.BctAssertions.*;
21
22 import java.util.*;
23
24 import org.apache.juneau.*;
25 import org.apache.juneau.annotation.*;
26 import org.apache.juneau.http.annotation.*;
27 import org.junit.jupiter.api.*;
28
29 class Swagger_Body_Test extends TestBase {
30
31
32
33
34
35 @Rest
36 public static class A {
37
38 @Content
39 @Schema(
40 d={"a","b"},
41 type="string",
42 r=true
43 )
44 public static class A1 {
45 public A1(String x) {}
46 }
47 @RestGet
48 public void a(A1 h) { }
49
50 @Content
51 @Schema(
52 description="a\nb",
53 required=true,
54 type="string"
55 )
56 public static class A2 {
57 public A2(String x) {}
58 }
59 @RestPut
60 public void b(A2 h) { }
61
62 @Content
63 @Schema(
64 description="a\nb",
65 required=true,
66 type="string"
67 )
68 public static class A3 {
69 public A3(String x) {}
70 }
71 @RestPost
72 public void c(A3 h) { }
73 }
74
75 @Test void a01_fromPojo() {
76 var s = getSwagger(A.class);
77 var x = s.getParameterInfo("/a","get","body",null);
78
79 assertBean(x, "description,required,schema{required,type}", "a\nb,true,{true,string}");
80
81 x = s.getParameterInfo("/b","put","body",null);
82 assertBean(x, "description,required,schema{required,type}", "a\nb,true,{true,string}");
83
84 x = s.getParameterInfo("/c","post","body",null);
85 assertBean(x, "description,required,schema{required,type}", "a\nb,true,{true,string}");
86 }
87
88 @Rest
89 public static class B {
90
91 @Content
92 @Schema(type="object")
93 public static class B1 {}
94 @RestGet
95 public void a(B1 h) { }
96
97 @Content
98 public static class B2 {
99 public String f1;
100 }
101 @RestPut
102 public void b(B2 b) { }
103
104 @Content
105 public static class B3 extends LinkedList<String> {
106 private static final long serialVersionUID = 1L;
107 }
108 @RestPost
109 public void c(B3 b) { }
110
111 @Content
112 public static class B4 {}
113 @RestDelete
114 public void d(B4 b) { }
115 }
116
117 @Test void b01_schemaFromPojo() {
118 var s = getSwagger(B.class);
119 var x = s.getParameterInfo("/a","get","body",null);
120
121 assertJson("{type:'object'}", x.getSchema());
122
123 x = s.getParameterInfo("/b","put","body",null);
124 assertJson("{properties:{f1:{type:'string'}},type:'object'}", x.getSchema());
125
126 x = s.getParameterInfo("/c","post","body",null);
127 assertJson("{items:{type:'string'},type:'array'}", x.getSchema());
128
129 x = s.getParameterInfo("/d","delete","body",null);
130 assertJson("{type:'string'}", x.getSchema());
131 }
132
133 @Rest
134 public static class D {
135
136 public static class D1 {
137 public D1(String x) {}
138 }
139
140 @RestGet
141 public void a(
142 @Content
143 @Schema(
144 d= {"a","b"},
145 r=true,
146 type="string"
147 )
148 D1 b) { }
149
150 public static class D2 {
151 public D2(String x) {}
152 }
153
154 @RestPut
155 public void b(
156 @Content
157 @Schema(
158 description="a\nb",
159 required=true,
160 type="string"
161 )
162 D2 b) { }
163
164 public static class D3 {
165 public D3(String x) {}
166 }
167
168 @RestPost
169 public void c(
170 @Content
171 @Schema(
172 d= {"b","c"},
173 required=true,
174 type="string"
175 )
176 D3 b) { }
177 }
178
179 @Test void d01_fromParameter() {
180 var s = getSwagger(D.class);
181 var x = s.getParameterInfo("/a","get","body",null);
182
183 assertBean(x, "description,required,schema{required,type}", "a\nb,true,{true,string}");
184
185 x = s.getParameterInfo("/b","put","body",null);
186 assertBean(x, "description,required,schema{required,type}", "a\nb,true,{true,string}");
187
188 x = s.getParameterInfo("/c","post","body",null);
189 assertBean(x, "description,required,schema{required,type}", "b\nc,true,{true,string}");
190 }
191
192 @Rest
193 public static class E {
194
195 public static class E1 {}
196 @RestGet
197 public void a(@Content @Schema(type="object") E1 b) { }
198
199 public static class E2 {
200 public String f1;
201 }
202 @RestPut
203 public void b(@Content E2 b) { }
204
205 public static class E3 extends LinkedList<String> {
206 private static final long serialVersionUID = 1L;
207 }
208 @RestPost
209 public void c(@Content E3 b) { }
210
211 public static class E4 {}
212 @RestDelete
213 public void d(@Content E4 b) { }
214
215 @RestOp
216 public void e(@Content Integer b) { }
217
218 @RestGet
219 public void f(@Content Boolean b) { }
220 }
221
222 @Test void e01_schemaFromParameter() {
223 var s = getSwagger(E.class);
224 var x = s.getParameterInfo("/a","get","body",null);
225
226 assertJson("{type:'object'}", x.getSchema());
227
228 x = s.getParameterInfo("/b","put","body",null);
229 assertJson("{properties:{f1:{type:'string'}},type:'object'}", x.getSchema());
230
231 x = s.getParameterInfo("/c","post","body",null);
232 assertJson("{items:{type:'string'},type:'array'}", x.getSchema());
233
234 x = s.getParameterInfo("/d","delete","body",null);
235 assertJson("{type:'string'}", x.getSchema());
236
237 x = s.getParameterInfo("/e","get","body",null);
238 assertJson("{format:'int32',type:'integer'}", x.getSchema());
239
240 x = s.getParameterInfo("/f","get","body",null);
241 assertJson("{type:'boolean'}", x.getSchema());
242 }
243 }