use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class OpenAPIV3ParserTest method shouldParseApiWithParametersUsingContentvsSchema.
@Test
public void shouldParseApiWithParametersUsingContentvsSchema() {
// Tests that the content method of specifying the format of a parameter
// gets resolved.
// Test checks if an API's single parameter of array type gets fully resolved to
// referenced definitions.
String location = "src/test/resources/issue-1078/api.yaml";
ParseOptions options = new ParseOptions();
options.setResolve(true);
// This test uses an Array in the parameters, test if it get's fully resolved.
options.setResolveFully(true);
OpenAPIV3Parser tested = new OpenAPIV3Parser();
// Parse yaml
SwaggerParseResult result = tested.readLocation(location, emptyList(), options);
OpenAPI api = result.getOpenAPI();
Paths paths = api.getPaths();
// First ensure all schemas were resolved, this is important when this library
// is used to generate code
Components components = api.getComponents();
assertNotNull(components);
assertThat(components.getSchemas().size(), equalTo(4));
assertNotNull(components.getSchemas().get("LocationType"));
assertNotNull(components.getSchemas().get("Lat"));
assertNotNull(components.getSchemas().get("Long"));
assertNotNull(components.getSchemas().get("SearchResult"));
PathItem apiEndpoint = paths.get("/api-endpoint-1");
List<Parameter> parameters = apiEndpoint.getGet().getParameters();
// Ensure there's only one parameter in this test
assertThat(parameters.size(), equalTo(1));
// We are testing content for a parameter so make sure its there.
Content content = parameters.get(0).getContent();
assertNotNull(content);
// spec says only one content is permitted in 3.x
assertThat(content.size(), equalTo(1));
// Ensure there's a media type
MediaType mediaType = content.entrySet().iterator().next().getValue();
assertNotNull(mediaType);
// This test has a single parameter of type array
Schema parameterSchema = mediaType.getSchema();
Assert.assertTrue(parameterSchema instanceof ArraySchema);
ArraySchema arraySchema = (ArraySchema) parameterSchema;
// Test if the item schema was resolved properly
Schema itemSchema = arraySchema.getItems();
assertNotNull(itemSchema);
Assert.assertTrue(itemSchema instanceof ObjectSchema);
// Ensure the referenced item's schema has been resolved.
ObjectSchema objSchema = (ObjectSchema) itemSchema;
Map<String, Schema> objectItemSchemas = objSchema.getProperties();
assertThat(objectItemSchemas.size(), equalTo(2));
Assert.assertTrue(objectItemSchemas.get("lat") instanceof IntegerSchema);
Assert.assertTrue(objectItemSchemas.get("long") instanceof IntegerSchema);
}
use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class OpenAPIV3ParserTest method testIssue146.
@Test
public void testIssue146() {
OpenAPIV3Parser parser = new OpenAPIV3Parser();
final OpenAPI openAPI = parser.read("src/test/resources/issue_146.yaml");
assertNotNull(openAPI);
QueryParameter p = ((QueryParameter) openAPI.getPaths().get("/checker").getGet().getParameters().get(0));
StringSchema pp = (StringSchema) ((ArraySchema) p.getSchema()).getItems();
assertTrue("registration".equalsIgnoreCase(pp.getEnum().get(0)));
}
use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class OpenAPIV3ParserTest method testIssue594.
@Test
public void testIssue594() {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "paths:\n" + " /test:\n" + " post:\n" + " responses:\n" + " '200':\n" + " description: OK\n" + " requestBody:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " type: array\n" + " minItems: 1\n" + " maxItems: 1\n" + " items:\n" + " $ref: \"#/components/schemas/Pet\"\n" + " description: Hello world\n" + "info:\n" + " version: ''\n" + " title: ''";
SwaggerParseResult result = new OpenAPIV3Parser().readContents(yaml, null, null);
assertNotNull(result.getOpenAPI());
ArraySchema schema = (ArraySchema) (result.getOpenAPI().getPaths().get("/test").getPost().getRequestBody().getContent().get("application/json").getSchema());
assertEquals(schema.getItems().get$ref(), "#/components/schemas/Pet");
assertNotNull(schema.getMaxItems());
assertNotNull(schema.getMinItems());
}
use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method selfReferenceTest.
@Test
public void selfReferenceTest(@Injectable final List<AuthorizationValue> auths) {
String yaml = "" + "openapi: 3.0.1\n" + "paths:\n" + " /selfRefA:\n" + " get:\n" + " requestBody:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/ModelA'\n" + " responses:\n" + " default:\n" + " description: Default response\n" + " /selfRefB:\n" + " get:\n" + " requestBody:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/ModelB'\n" + " responses:\n" + " default:\n" + " description: Default response\n" + " /selfRefC:\n" + " get:\n" + " requestBody:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/ModelC'\n" + " responses:\n" + " default:\n" + " description: Default response\n" + " /selfRefD:\n" + " get:\n" + " responses:\n" + " default:\n" + " description: Default response\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " $ref: '#/components/schemas/ModelA'\n" + " /selfRefE:\n" + " get:\n" + " responses:\n" + " default:\n" + " description: Default response\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " type: array\n" + " items:\n" + " $ref: '#/components/schemas/ModelA'\n" + "info:\n" + " version: ''\n" + " title: ''\n" + "components:\n" + " schemas:\n" + " ModelA:\n" + " properties:\n" + " modelB:\n" + " $ref: '#/components/schemas/ModelB'\n" + " ModelB:\n" + " properties:\n" + " modelB:\n" + " $ref: '#/components/schemas/ModelB'\n" + " ModelC:\n" + " properties:\n" + " modelA:\n" + " $ref: '#/components/schemas/ModelA'";
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().readContents(yaml, auths, options).getOpenAPI();
Schema schemaB = openAPI.getPaths().get("/selfRefB").getGet().getRequestBody().getContent().get("application/json").getSchema();
assertTrue(schemaB instanceof Schema);
assertEquals(openAPI.getComponents().getSchemas().get("ModelB"), schemaB);
Schema schema = openAPI.getPaths().get("/selfRefE").getGet().getResponses().get("default").getContent().get("*/*").getSchema();
assertTrue(schema instanceof ArraySchema);
ArraySchema arraySchema = (ArraySchema) schema;
assertEquals(openAPI.getComponents().getSchemas().get("ModelA"), arraySchema.getItems());
}
use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method resolveComposedReferenceAllOfSchema.
@Test
public void resolveComposedReferenceAllOfSchema(@Injectable final List<AuthorizationValue> auths) {
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().readLocation("src/test/resources/composed.yaml", auths, options).getOpenAPI();
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();
assertTrue(arraySchema.getItems() instanceof ObjectSchema);
}
Aggregations