Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array schema deserialization #585

Merged
merged 2 commits into from
Nov 21, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1825,8 +1825,6 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
items.setItems(getSchema(itemsNode, location, result));
}
}


}
schema = items;
} else {
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@@ -165,12 +166,12 @@ public Schema resolveSchema(Schema schema) {

if(schema instanceof ArraySchema) {
ArraySchema arrayModel = (ArraySchema) schema;
if(arrayModel.getItems() != null){
if(arrayModel.getItems().get$ref() != null) {
arrayModel.setItems(resolveSchema(arrayModel.getItems()));
} else {
arrayModel.setItems(arrayModel.getItems());
if(arrayModel.getItems().get$ref() != null) {
arrayModel.setItems(resolveSchema(arrayModel.getItems()));
}
}

return arrayModel;
}
if (schema instanceof ObjectSchema) {
@@ -213,57 +214,39 @@ public Schema resolveSchema(Schema schema) {
}
}
}
if (requiredProperties.size() > 0) {
model.setRequired(new ArrayList<>(requiredProperties));
}
if (composedSchema.getExtensions() != null) {
Map<String, Object> extensions = composedSchema.getExtensions();
for (String key : extensions.keySet()) {
model.addExtension(key, composedSchema.getExtensions().get(key));
}
}
return model;
}

} else if (composedSchema.getOneOf() != null) {
Schema resolved;
List<Schema> list = new ArrayList<>();
for (Schema innerModel : composedSchema.getOneOf()) {
Schema resolved = resolveSchema(innerModel);
Map<String, Schema> properties = resolved.getProperties();
if (resolved.getProperties() != null) {

for (String key : properties.keySet()) {
Schema prop = (Schema) resolved.getProperties().get(key);
model.addProperties(key, resolveSchema(prop));
}
if (resolved.getRequired() != null) {
for (int i = 0; i < resolved.getRequired().size(); i++) {
if (resolved.getRequired().get(i) != null) {
requiredProperties.add(resolved.getRequired().get(i).toString());
}
}
}
}
resolved = resolveSchema(innerModel);
list.add(resolved);
}
composedSchema.setOneOf(list);

} else if (composedSchema.getAnyOf() != null) {
Schema resolved;
List<Schema> list = new ArrayList<>();
for (Schema innerModel : composedSchema.getAnyOf()) {
Schema resolved = resolveSchema(innerModel);
Map<String, Schema> properties = resolved.getProperties();
if (resolved.getProperties() != null) {

for (String key : properties.keySet()) {
Schema prop = (Schema) resolved.getProperties().get(key);
model.addProperties(key, resolveSchema(prop));
}
if (resolved.getRequired() != null) {
for (int i = 0; i < resolved.getRequired().size(); i++) {
if (resolved.getRequired().get(i) != null) {
requiredProperties.add(resolved.getRequired().get(i).toString());
}
}
}
}
resolved = resolveSchema(innerModel);
list.add(resolved);
}
composedSchema.setAnyOf(list);
}
if (requiredProperties.size() > 0) {
model.setRequired(new ArrayList<>(requiredProperties));
}
if (composedSchema.getExtensions() != null) {
Map<String, Object> extensions = composedSchema.getExtensions();
for (String key : extensions.keySet()) {
model.addExtension(key, composedSchema.getExtensions().get(key));
}
}
return model;


return composedSchema;
} else {
// User don't want to aggregate composed schema, we only solve refs
if (composedSchema.getAllOf() != null)
Original file line number Diff line number Diff line change
@@ -563,7 +563,7 @@ public void resolveAllOfWithoutAggregatingParameters(@Injectable final List<Auth
}

@Test
public void resolveComposedReferenceSchema(@Injectable final List<AuthorizationValue> auths){
public void resolveComposedReferenceAllOfSchema(@Injectable final List<AuthorizationValue> auths){



@@ -572,35 +572,35 @@ public void resolveComposedReferenceSchema(@Injectable final List<AuthorizationV
options.setResolveFully(true);

OpenAPI openAPI = new OpenAPIV3Parser().readLocation("src/test/resources/composed.yaml",auths,options).getOpenAPI();
ResolverFully resolverUtil = new ResolverFully();
resolverUtil.resolveFully(openAPI);


assertTrue(openAPI.getPaths().get("/withInvalidComposedModelArray").getPost().getRequestBody().getContent().get("application/json").getSchema() instanceof ArraySchema);
ArraySchema arraySchema = (ArraySchema) openAPI.getPaths().get("/withInvalidComposedModelArray").getPost().getRequestBody().getContent().get("application/json").getSchema();
System.out.println(arraySchema);
assertTrue(arraySchema.getItems() instanceof ObjectSchema);

}

@Test
public void resolveComposedSchema(@Injectable final List<AuthorizationValue> auths){



ParseOptions options = new ParseOptions();
options.setResolve(true);
//options.setResolveCombinators(false);
options.setResolveFully(true);

OpenAPI openAPI = new OpenAPIV3Parser().readLocation("src/test/resources/oneof-anyof.yaml",auths,options).getOpenAPI();

ResolverFully resolverUtil = new ResolverFully();
resolverUtil.resolveFully(openAPI);

assertTrue(openAPI.getPaths().get("/mixed-array").getGet().getResponses().get("200").getContent().get("application/json").getSchema() instanceof ArraySchema);
ArraySchema arraySchema = (ArraySchema) openAPI.getPaths().get("/mixed-array").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
assertTrue(arraySchema.getItems() instanceof ComposedSchema);
ComposedSchema oneOf = (ComposedSchema) arraySchema.getItems();
assertEquals(oneOf.getOneOf().get(0).getType(), "string");

//System.out.println(openAPI.getPaths().get("/oneOf").getGet().getResponses().get("200").getContent().get("application/json").getSchema() );
assertTrue(openAPI.getPaths().get("/oneOf").getGet().getResponses().get("200").getContent().get("application/json").getSchema() instanceof ComposedSchema);
ComposedSchema oneOfSchema = (ComposedSchema) openAPI.getPaths().get("/oneOf").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
assertEquals(oneOfSchema.getOneOf().get(0).getType(), "object");

}

private static int getDynamicPort() {