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