|
3 | 3 | import com.fasterxml.jackson.annotation.JsonInclude; |
4 | 4 | import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; |
5 | 5 | import com.fasterxml.jackson.databind.DeserializationFeature; |
| 6 | +import com.fasterxml.jackson.databind.JsonNode; |
6 | 7 | import com.fasterxml.jackson.databind.ObjectMapper; |
7 | 8 | import com.fasterxml.jackson.databind.SerializationFeature; |
8 | 9 | import io.swagger.v3.oas.models.Components; |
|
22 | 23 | import io.swagger.v3.oas.models.parameters.QueryParameter; |
23 | 24 | import io.swagger.v3.oas.models.responses.ApiResponse; |
24 | 25 | import io.swagger.v3.oas.models.responses.ApiResponses; |
| 26 | +import io.swagger.v3.oas.models.security.SecurityRequirement; |
| 27 | +import io.swagger.v3.oas.models.security.SecurityScheme; |
25 | 28 | import io.swagger.v3.oas.models.tags.Tag; |
26 | 29 | import org.slf4j.Logger; |
27 | 30 | import org.slf4j.LoggerFactory; |
| 31 | +import org.testng.Assert; |
28 | 32 | import org.testng.annotations.Test; |
29 | 33 |
|
30 | 34 | import java.math.BigDecimal; |
31 | 35 | import java.util.ArrayList; |
| 36 | +import java.util.Arrays; |
32 | 37 | import java.util.HashMap; |
33 | 38 | import java.util.Map; |
34 | 39 |
|
@@ -126,6 +131,38 @@ public void testBuilder() throws Exception { |
126 | 131 | LOGGER.debug(writeJson(oai)); |
127 | 132 | } |
128 | 133 |
|
| 134 | + @Test |
| 135 | + public void serializesOperationSecurityWithAnonymousAlternative() throws Exception { |
| 136 | + OpenAPI oai = new OpenAPI() |
| 137 | + .info(new Info() |
| 138 | + .title("Security alternatives") |
| 139 | + .version("1.0.0")) |
| 140 | + .components(new Components() |
| 141 | + .addSecuritySchemes("ApiKeyAuth", new SecurityScheme() |
| 142 | + .type(SecurityScheme.Type.APIKEY) |
| 143 | + .in(SecurityScheme.In.HEADER) |
| 144 | + .name("X-API-Key"))) |
| 145 | + .paths(new Paths() |
| 146 | + .addPathItem("/search", new PathItem() |
| 147 | + .get(new Operation() |
| 148 | + .security(Arrays.asList( |
| 149 | + new SecurityRequirement(), |
| 150 | + new SecurityRequirement().addList("ApiKeyAuth"))) |
| 151 | + .responses(new ApiResponses() |
| 152 | + .addApiResponse("200", new ApiResponse() |
| 153 | + .description("ok")))))); |
| 154 | + |
| 155 | + JsonNode security = new ObjectMapper() |
| 156 | + .readTree(writeJson(oai)) |
| 157 | + .at("/paths/~1search/get/security"); |
| 158 | + |
| 159 | + Assert.assertEquals(security.size(), 2); |
| 160 | + Assert.assertTrue(security.get(0).isObject()); |
| 161 | + Assert.assertEquals(security.get(0).size(), 0); |
| 162 | + Assert.assertTrue(security.get(1).has("ApiKeyAuth")); |
| 163 | + Assert.assertEquals(security.get(1).get("ApiKeyAuth").size(), 0); |
| 164 | + } |
| 165 | + |
129 | 166 | public static String writeJson(Object value) throws Exception { |
130 | 167 | ObjectMapper mapper = new ObjectMapper(); |
131 | 168 |
|
|
0 commit comments