Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -1817,9 +1817,13 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
schema = oneOfList;
} else if(itemsNode != null) {
ArraySchema items = new ArraySchema();
for(JsonNode n : itemsNode) {
if(n.isValueNode()) {
items.setItems(getSchema(itemsNode, location, result));
if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)){
items.setItems(getSchema(itemsNode, location, result));
}else if (itemsNode.getNodeType().equals(JsonNodeType.ARRAY)){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which case is this handling? should it be perhaps a recursive call instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi I don't understand the question, getSchema() is a recursive call

for (JsonNode n : itemsNode) {
if (n.isValueNode()) {
items.setItems(getSchema(itemsNode, location, result));
}
}
}
schema = items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,7 +168,10 @@ public Schema resolveSchema(Schema schema) {
ArraySchema arrayModel = (ArraySchema) schema;
if(arrayModel.getItems().get$ref() != null) {
arrayModel.setItems(resolveSchema(arrayModel.getItems()));
} else {
arrayModel.setItems(arrayModel.getItems());
}

return arrayModel;
}
if (schema instanceof ObjectSchema) {
Expand Down Expand Up @@ -210,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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){



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

OpenAPI openAPI = new OpenAPIV3Parser().readLocation("src/test/resources/composed.yaml",auths,options).getOpenAPI();
openAPI.getPaths();
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.setResolveCombinators(false);
options.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().readLocation("src/test/resources/oneof-anyof.yaml",auths,options).getOpenAPI();


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() {
return new Random().ints(50000, 60000).findFirst().getAsInt();
}
Expand Down
82 changes: 82 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/oneof-anyof.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
openapi: 3.0.0
info:
version: 0.0.0
title: test

paths:
/oneOf:
get:
responses:
'200':
description: A book or movie object
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Book'
- $ref: '#/components/schemas/Movie'

/anyOf:
get:
responses:
'200':
description: A book or movie object
content:
application/json:
schema:
anyOf:
- $ref: '#/components/schemas/Movie'
- $ref: '#/components/schemas/Book'

/mixed-array:
get:
responses:
'200':
description: An array containing strings and/or integers
content:
application/json:
schema:
type: array
items:
oneOf:
- type: string
- type: integer

components:
schemas:
Book:
type: object
properties:
title:
type: string
authors:
type: array
items:
type: string
isbn:
type: string
required:
- title
example:
title: The Hitchhiker's Guide to the Galaxy
authors:
- Douglas Adams
isbn: 0-330-25864-8
Movie:
type: object
properties:
title:
type: string
directors:
type: array
items:
type: string
year:
type: integer
required:
- title
example:
title: Blade Runner
directors:
- Ridley Scott
year: 1982
37 changes: 37 additions & 0 deletions modules/swagger-parser/src/test/resources/definitions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
definitions:
pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
newPet:
type: object
required:
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
errorModel:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
Loading