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  import static org.junit.jupiter.api.Assertions.*;
22  
23  import java.util.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.annotation.*;
27  import org.apache.juneau.http.annotation.*;
28  import org.junit.jupiter.api.*;
29  
30  class Swagger_Query_Test extends TestBase {
31  
32  	//-----------------------------------------------------------------------------------------------------------------
33  	// Swagger tests
34  	//-----------------------------------------------------------------------------------------------------------------
35  
36  	@Rest
37  	public static class A {
38  
39  		@Query("Q")
40  		@Schema(d= {"a","b"}, t="string")
41  		public static class A1 {
42  			public A1(String x) { /* no-op */ }
43  		}
44  		@RestGet
45  		public void a(A1 q) { /* no-op */ }
46  
47  		@Query(
48  			name="Q",
49  			schema=@Schema(description="a\nb",type="string")
50  		)
51  		public static class A2 {
52  			public A2(String x) { /* no-op */ }
53  		}
54  		@RestPut
55  		public void b(A2 q) { /* no-op */ }
56  
57  		@Query(
58  			name="Q",
59  			schema=@Schema(description="b\nc",type="string")
60  		)
61  		@Schema(d={"a","b"}, t="string")
62  		public static class A3 {
63  			public A3(String x) { /* no-op */ }
64  		}
65  		@RestPost
66  		public void c(A3 q) { /* no-op */ }
67  
68  		@Query("Q")
69  		public static class A4 {}
70  		@RestDelete
71  		public void d(A4 q) { /* no-op */ }
72  	}
73  
74  	@Test void a01_fromPojo() {
75  		var s = getSwagger(A.class);
76  		var x = s.getParameterInfo("/a","get","query","Q");
77  
78  		assertBean(x, "name,description,type", "Q,a\nb,string");
79  
80  		x = s.getParameterInfo("/b","put","query","Q");
81  		assertBean(x, "name,description,type", "Q,a\nb,string");
82  
83  		x = s.getParameterInfo("/c","post","query","Q");
84  		assertBean(x, "name,description,type", "Q,b\nc,string");
85  
86  		x = s.getParameterInfo("/d","delete","query","Q");
87  		assertEquals("Q", x.getName());
88  	}
89  
90  	@Rest
91  	public static class B {
92  
93  		@Query(name="Q")
94  		public static class B1 {}
95  		@RestGet
96  		public void a(B1 q) { /* no-op */ }
97  
98  		@Query("Q")
99  		public static class B2 {
100 			public String f1;
101 		}
102 		@RestPut
103 		public void b(B2 q) { /* no-op */ }
104 
105 		@Query("Q")
106 		public static class B3 extends LinkedList<String> {
107 			private static final long serialVersionUID = 1L;
108 		}
109 		@RestPost
110 		public void c(B3 q) { /* no-op */ }
111 
112 		@Query("Q")
113 		public static class B4 {}
114 		@RestDelete
115 		public void d(B4 q) { /* no-op */ }
116 	}
117 
118 	@Test void b01_schemaFromPojo() {
119 		var s = getSwagger(B.class);
120 		var x = s.getParameterInfo("/a","get","query","Q");
121 
122 		assertJson("{'in':'query',name:'Q',type:'string'}", x);
123 
124 		x = s.getParameterInfo("/b","put","query","Q");
125 		assertJson("{'in':'query',name:'Q',schema:{properties:{f1:{type:'string'}}},type:'object'}", x);
126 
127 		x = s.getParameterInfo("/c","post","query","Q");
128 		assertJson("{'in':'query',items:{type:'string'},name:'Q',type:'array'}", x);
129 
130 		x = s.getParameterInfo("/d","delete","query","Q");
131 		assertJson("{'in':'query',name:'Q',type:'string'}", x);
132 	}
133 
134 	@Rest
135 	public static class D {
136 
137 		@RestGet
138 		public void a(
139 			@Query("Q")
140 			@Schema(d= {"a","b"}, t="string")
141 			String q
142 		) { /* no-op */ }
143 
144 		@RestPut
145 		public void b(
146 			@Query(
147 				name="Q",
148 				schema=@Schema(description="a\nb",type="string")
149 			)
150 			String q
151 		) { /* no-op */ }
152 
153 		@RestPost
154 		public void c(
155 			@Query(
156 				name="Q",
157 				schema=@Schema(description="b\nc",type="string")
158 			)
159 			@Schema(d= {"a","b"}, t="string")
160 			String q
161 		) { /* no-op */ }
162 
163 		@RestDelete
164 		public void d(@Query("Q") String q) {/* no-op */}
165 	}
166 
167 	@Test void d01_fromParameter() {
168 		var s = getSwagger(D.class);
169 		var x = s.getParameterInfo("/a","get","query","Q");
170 
171 		assertBean(x, "name,description,type", "Q,a\nb,string");
172 
173 		x = s.getParameterInfo("/b","put","query","Q");
174 		assertBean(x, "name,description,type", "Q,a\nb,string");
175 
176 		x = s.getParameterInfo("/c","post","query","Q");
177 		assertBean(x, "name,description,type", "Q,b\nc,string");
178 
179 		x = s.getParameterInfo("/d","delete","query","Q");
180 		assertEquals("Q", x.getName());
181 	}
182 
183 	//-----------------------------------------------------------------------------------------------------------------
184 	// Schema
185 	//-----------------------------------------------------------------------------------------------------------------
186 
187 	@Rest
188 	public static class E {
189 
190 		@RestGet
191 		public void a(@Query("Q") String q) { /* no-op */ }
192 	}
193 
194 	@Test void e01_schemaFromParameter() {
195 		var s = getSwagger(E.class);
196 
197 		var x = s.getParameterInfo("/a","get","query","Q");
198 		assertJson("{'in':'query',name:'Q',type:'string'}", x);
199 	}
200 }