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  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  	// Swagger
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) { /* no-op */ }
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) { /* no-op */ }
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) { /* no-op */ }
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) { /* no-op */ }
96  
97  		@Content
98  		public static class B2 {
99  			public String f1;
100 		}
101 		@RestPut
102 		public void b(B2 b) { /* no-op */ }
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) { /* no-op */ }
110 
111 		@Content
112 		public static class B4 {}
113 		@RestDelete
114 		public void d(B4 b) { /* no-op */ }
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) { /* no-op */ }
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) { /* no-op */ }
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) { /* no-op */ }
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) { /* no-op */ }
198 
199 		public static class E2 {
200 			public String f1;
201 		}
202 		@RestPut
203 		public void b(@Content E2 b) { /* no-op */ }
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) { /* no-op */ }
210 
211 		public static class E4 {}
212 		@RestDelete
213 		public void d(@Content E4 b) { /* no-op */ }
214 
215 		@RestOp
216 		public void e(@Content Integer b) { /* no-op */ }
217 
218 		@RestGet
219 		public void f(@Content Boolean b) { /* no-op */ }
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 }