Skip to content

Commit 7c9cac3

Browse files
committed
convert schema from deepObject style parameter into query parameter.
1 parent 71f8078 commit 7c9cac3

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,11 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
22142214
if (StringUtils.isNotBlank(param.get$ref())) {
22152215
param = getParameterFromRef(param.get$ref(), openAPI);
22162216
}
2217+
if ((param instanceof QueryParameter || "query".equalsIgnoreCase(param.getIn()))
2218+
&& param.getStyle().equals(Parameter.StyleEnum.DEEPOBJECT)) {
2219+
operationParameters.parseNestedObjects(param.getName(), param.getSchema(), imports, this, openAPI);
2220+
continue;
2221+
}
22172222
CodegenParameter codegenParameter = fromParameter(param, imports);
22182223
operationParameters.addParameters(param, codegenParameter);
22192224
}

src/main/java/io/swagger/codegen/v3/generators/OperationParameters.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import io.swagger.codegen.v3.CodegenContent;
55
import io.swagger.codegen.v3.CodegenOperation;
66
import io.swagger.codegen.v3.CodegenParameter;
7+
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
8+
import io.swagger.v3.oas.models.OpenAPI;
9+
import io.swagger.v3.oas.models.media.ArraySchema;
10+
import io.swagger.v3.oas.models.media.Schema;
711
import io.swagger.v3.oas.models.parameters.CookieParameter;
812
import io.swagger.v3.oas.models.parameters.HeaderParameter;
913
import io.swagger.v3.oas.models.parameters.Parameter;
@@ -13,6 +17,8 @@
1317
import java.util.ArrayList;
1418
import java.util.Collections;
1519
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Set;
1622

1723
public class OperationParameters {
1824

@@ -147,6 +153,40 @@ public void sortRequiredAllParams() {
147153
});
148154
}
149155

156+
public void parseNestedObjects(String name, Schema schema, Set<String> imports, DefaultCodegenConfig codegenConfig, OpenAPI openAPI) {
157+
schema = OpenAPIUtil.getRefSchemaIfExists(schema, openAPI);
158+
if (schema == null || !isObjectWithProperties(schema)) {
159+
return;
160+
}
161+
final Map<String, Schema> properties = schema.getProperties();
162+
for (String key : properties.keySet()) {
163+
Schema property = properties.get(key);
164+
property = OpenAPIUtil.getRefSchemaIfExists(property, openAPI);
165+
boolean required;
166+
if (schema.getRequired() == null || schema.getRequired().isEmpty()) {
167+
required = false;
168+
} else {
169+
required = schema.getRequired().stream().anyMatch(propertyName -> key.equalsIgnoreCase(propertyName.toString()));
170+
}
171+
final String parameterName;
172+
if (property instanceof ArraySchema) {
173+
parameterName = String.format("%s[%s][]", name, key);
174+
} else {
175+
parameterName = String.format("%s[%s]", name, key);
176+
}
177+
if (isObjectWithProperties(property)) {
178+
parseNestedObjects(parameterName, property, imports, codegenConfig, openAPI);
179+
continue;
180+
}
181+
final Parameter queryParameter = new QueryParameter()
182+
.name(parameterName)
183+
.required(required)
184+
.schema(property);
185+
final CodegenParameter codegenParameter = codegenConfig.fromParameter(queryParameter, imports);
186+
addParameters(queryParameter, codegenParameter);
187+
}
188+
}
189+
150190
public static List<CodegenParameter> addHasMore(List<CodegenParameter> codegenParameters) {
151191
if (codegenParameters == null || codegenParameters.isEmpty()) {
152192
return codegenParameters;
@@ -157,4 +197,10 @@ public static List<CodegenParameter> addHasMore(List<CodegenParameter> codegenPa
157197
}
158198
return codegenParameters;
159199
}
200+
201+
private boolean isObjectWithProperties(Schema schema) {
202+
return ("object".equalsIgnoreCase(schema.getType()) || schema.getType() == null)
203+
&& schema.getProperties() != null
204+
&& !schema.getProperties().isEmpty();
205+
}
160206
}

src/main/java/io/swagger/codegen/v3/generators/util/OpenAPIUtil.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public static Schema getSchemaFromName(String name, OpenAPI openAPI) {
5151
return mapSchema.get(name);
5252
}
5353

54+
public static Schema getRefSchemaIfExists(Schema schema, OpenAPI openAPI) {
55+
if (schema == null) {
56+
return null;
57+
}
58+
if (StringUtils.isBlank(schema.get$ref()) || openAPI == null || openAPI.getComponents() == null) {
59+
return schema;
60+
}
61+
final String name = getSimpleRef(schema.get$ref());
62+
return getSchemaFromName(name, openAPI);
63+
}
64+
5465
public static Schema getSchemaFromRefSchema(Schema refSchema, OpenAPI openAPI) {
5566
if (StringUtils.isBlank(refSchema.get$ref())) {
5667
return null;

0 commit comments

Comments
 (0)