|
1 | 1 | package com.akto.open_api;
|
2 | 2 |
|
3 |
| -import com.akto.dao.ApiCollectionsDao; |
4 |
| -import com.akto.dao.SingleTypeInfoDao; |
5 |
| -import com.akto.dto.ApiCollection; |
6 | 3 | import com.akto.dto.type.SingleTypeInfo;
|
7 | 4 | import com.akto.log.LoggerMaker;
|
8 | 5 | import com.akto.log.LoggerMaker.LogDb;
|
9 | 6 | import com.fasterxml.jackson.databind.ObjectMapper;
|
10 | 7 | import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
|
11 | 8 | import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
|
12 |
| -import com.mongodb.client.model.Filters; |
13 | 9 | import io.swagger.v3.oas.models.OpenAPI;
|
14 | 10 | import io.swagger.v3.oas.models.PathItem;
|
15 | 11 | import io.swagger.v3.oas.models.Paths;
|
|
21 | 17 | import java.net.URI;
|
22 | 18 | import java.net.URISyntaxException;
|
23 | 19 | import java.util.*;
|
24 |
| -import java.util.regex.Matcher; |
25 | 20 | import java.util.regex.Pattern;
|
26 | 21 | import org.slf4j.Logger;
|
27 | 22 | import org.slf4j.LoggerFactory;
|
28 | 23 |
|
29 | 24 | import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
|
30 | 25 |
|
31 | 26 | public class Main {
|
32 |
| - private static final LoggerMaker loggerMaker = new LoggerMaker(Main.class); |
| 27 | + private static final LoggerMaker loggerMaker = new LoggerMaker(Main.class, LogDb.DASHBOARD); |
33 | 28 | private static final Logger logger = LoggerFactory.getLogger(Main.class);
|
34 | 29 |
|
35 | 30 | private static final ObjectMapper mapper = new ObjectMapper();
|
@@ -99,38 +94,63 @@ public static void addPathItems(int responseCode, Paths paths, String url, Strin
|
99 | 94 | }
|
100 | 95 | List<Parameter> headerParameters = new ArrayList<>();
|
101 | 96 | try{
|
102 |
| - headerParameters = buildHeaders(singleTypeInfoList); |
| 97 | + headerParameters = buildParams(singleTypeInfoList, ParamLocation.HEADER); |
103 | 98 | } catch (Exception e) {
|
104 | 99 | loggerMaker.errorAndAddToDb("ERROR in building headers in addPathItems " + e, LogDb.DASHBOARD);
|
105 | 100 | }
|
| 101 | + |
| 102 | + List<Parameter> queryParameters = new ArrayList<>(); |
| 103 | + try{ |
| 104 | + queryParameters = buildParams(singleTypeInfoList, ParamLocation.QUERY); |
| 105 | + } catch (Exception e) { |
| 106 | + loggerMaker.errorAndAddToDb("ERROR in building query params in addPathItems " + e, LogDb.DASHBOARD); |
| 107 | + } |
106 | 108 |
|
107 |
| - PathBuilder.addPathItem(paths, url, method, responseCode, schema, headerParameters, includeHeaders); |
| 109 | + PathBuilder.addPathItem(paths, url, method, responseCode, schema, headerParameters, queryParameters, includeHeaders); |
108 | 110 | }
|
109 | 111 |
|
110 |
| - public static List<Parameter> buildHeaders(List<SingleTypeInfo> singleTypeInfoList) throws Exception{ |
111 |
| - List<Parameter> headerParameters = new ArrayList<>(); |
| 112 | + // Ref: https://github.com/OAI/OpenAPI-Specification/blob/3.0.1/versions/3.0.1.md#parameter-locations |
| 113 | + public enum ParamLocation { |
| 114 | + HEADER, QUERY, PATH, COOKIE |
| 115 | + } |
| 116 | + |
| 117 | + public static List<Parameter> buildParams(List<SingleTypeInfo> singleTypeInfoList, ParamLocation location) throws Exception{ |
| 118 | + List<Parameter> parameters = new ArrayList<>(); |
112 | 119 | ObjectSchema schema =new ObjectSchema();
|
113 |
| - for (SingleTypeInfo singleTypeInfo: singleTypeInfoList) { |
114 |
| - if(singleTypeInfo.isIsHeader()){ |
115 |
| - List<SchemaBuilder.CustomSchema> cc = SchemaBuilder.getCustomSchemasFromSingleTypeInfo(singleTypeInfo); |
116 |
| - SchemaBuilder.build(schema, cc); |
| 120 | + for (SingleTypeInfo singleTypeInfo : singleTypeInfoList) { |
| 121 | + switch (location) { |
| 122 | + case HEADER: |
| 123 | + if (singleTypeInfo.isIsHeader()) { |
| 124 | + List<SchemaBuilder.CustomSchema> cc = SchemaBuilder.getCustomSchemasFromSingleTypeInfo(singleTypeInfo); |
| 125 | + SchemaBuilder.build(schema, cc); |
| 126 | + } |
| 127 | + break; |
| 128 | + case QUERY: |
| 129 | + if (singleTypeInfo.isQueryParam()) { |
| 130 | + List<SchemaBuilder.CustomSchema> cc = SchemaBuilder.getCustomSchemasFromSingleTypeInfo(singleTypeInfo); |
| 131 | + SchemaBuilder.build(schema, cc); |
| 132 | + } |
| 133 | + break; |
| 134 | + default: |
| 135 | + break; |
117 | 136 | }
|
118 | 137 | }
|
119 |
| - if (schema.getProperties() == null) return headerParameters; |
120 |
| - for(String header:schema.getProperties().keySet()){ |
121 |
| - Parameter head = new Parameter(); |
122 |
| - head.setName(header); |
123 |
| - head.setIn("header"); |
124 |
| - head.setSchema(schema.getProperties().get(header)); |
125 |
| - headerParameters.add(head); |
| 138 | + |
| 139 | + if (schema.getProperties() == null) return parameters; |
| 140 | + for(String param:schema.getProperties().keySet()){ |
| 141 | + Parameter parameter = new Parameter(); |
| 142 | + parameter.setName(param); |
| 143 | + parameter.setIn(location.name().toLowerCase()); |
| 144 | + parameter.setSchema(schema.getProperties().get(param)); |
| 145 | + parameters.add(parameter); |
126 | 146 | }
|
127 |
| - return headerParameters; |
| 147 | + return parameters; |
128 | 148 | }
|
129 | 149 |
|
130 | 150 | public static Schema<?> buildSchema(List<SingleTypeInfo> singleTypeInfoList) throws Exception {
|
131 | 151 | ObjectSchema schema =new ObjectSchema();
|
132 | 152 | for (SingleTypeInfo singleTypeInfo: singleTypeInfoList) {
|
133 |
| - if(singleTypeInfo.isIsHeader()){ |
| 153 | + if(singleTypeInfo.isIsHeader() || singleTypeInfo.isQueryParam() || singleTypeInfo.getIsUrlParam()){ |
134 | 154 | continue;
|
135 | 155 | }
|
136 | 156 | List<SchemaBuilder.CustomSchema> cc = SchemaBuilder.getCustomSchemasFromSingleTypeInfo(singleTypeInfo);
|
|
0 commit comments