Search in sources :

Example 76 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class V2ConverterTest method testIssue1.

@Test(description = "Missing array item type in parameters")
public void testIssue1() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(PET_STORE_JSON);
    Parameter statusParameter = oas.getPaths().get(PET_FIND_BY_STATUS_PATH).getGet().getParameters().get(0);
    assertNotNull(statusParameter);
    assertTrue(statusParameter.getSchema() instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema) statusParameter.getSchema();
    assertEquals(ARRAY_TYPE, arraySchema.getType());
    assertEquals(ENUM_SIZE, arraySchema.getItems().getEnum().size());
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Parameter(io.swagger.v3.oas.models.parameters.Parameter) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 77 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testYamlArrayResponseRemoteRefs.

@Test(description = "resolve array response remote refs in yaml")
public void testYamlArrayResponseRemoteRefs() {
    final OpenAPI swagger = new OpenAPI();
    swagger.path("/fun", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", new ApiResponse().content(new Content().addMediaType("*/*", new MediaType().schema(new ArraySchema().items(new Schema().$ref(replacePort(REMOTE_REF_YAML))))))))));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final ApiResponse response = swagger.getPaths().get("/fun").getGet().getResponses().get("200");
    final ArraySchema array = (ArraySchema) response.getContent().get("*/*").getSchema();
    assertNotNull(array.getItems());
    assertEquals(array.getItems().get$ref(), "#/components/schemas/Tag");
    assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Content(io.swagger.v3.oas.models.media.Content) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MediaType(io.swagger.v3.oas.models.media.MediaType) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 78 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testUpdateInternalReferencesOfExternalFiles.

@Test(description = "update internal references of external files")
public void testUpdateInternalReferencesOfExternalFiles() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    OpenAPI openAPI = new OpenAPIV3Parser().read("internal-references-in-external-files/main.yaml", null, options);
    ComposedSchema commonSchema = (ComposedSchema) openAPI.getComponents().getSchemas().get("common");
    assertEquals(commonSchema.getAllOf().get(0).get$ref(), "#/components/schemas/core");
    assertEquals(((Schema) commonSchema.getAllOf().get(1).getProperties().get("direct")).get$ref(), "#/components/schemas/core");
    assertEquals(((ArraySchema) commonSchema.getAllOf().get(1).getProperties().get("referenced")).getItems().get$ref(), "#/components/schemas/core");
    Schema coreSchema = openAPI.getComponents().getSchemas().get("core");
    assertEquals(((Schema) coreSchema.getProperties().get("inner")).get$ref(), "#/components/schemas/innerCore");
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 79 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method pathsResolver.

@Test
public void pathsResolver() throws Exception {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
    pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
    final JsonNode rootNode = mapper.readTree(pathFile.getBytes());
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    assertEquals(new OpenAPIResolver(openAPI, new ArrayList<>(), null).resolve(), openAPI);
    // internal url pathItem
    assertEquals(openAPI.getPaths().get("/pathItemRef2"), openAPI.getPaths().get("/pet"));
    // internal array schema inside operation -> responses -> content
    ArraySchema schema = (ArraySchema) openAPI.getPaths().get("/pet").getPut().getResponses().get("400").getContent().get("application/json").getSchema();
    assertEquals(schema.getItems().get$ref(), "#/components/schemas/VeryComplexType");
    // replace of parameters in operation and remove the ones from the pathItem
    Assert.assertNotNull(openAPI.getPaths().get("/pet").getPost().getParameters());
    Assert.assertNull(openAPI.getPaths().get("/pet").getParameters());
    // remote ref pathItem
    assertEquals(openAPI.getPaths().get("/pathItemRef").getSummary(), "summary");
    assertEquals(openAPI.getPaths().get("/pathItemRef").getPost().getResponses().get("405").getDescription(), "Invalid input");
    // internal pathItem operation -> response -> schema
    Assert.assertNotNull(openAPI.getPaths().get("/pet/{petId}").getGet().getResponses());
    assertEquals(openAPI.getPaths().get("/pet/{petId}").getGet().getResponses().get("200").getContent().get("application/xml").getSchema().get$ref(), "#/components/schemas/Pet");
    // internal pathItem -> operation -> callback -> pathItem -> operation -> response -> schema
    assertEquals(openAPI.getPaths().get("/pet/{petId}").getGet().getCallbacks().get("mainHook").get("$request.body#/url").getPost().getResponses().get("200").getContent().get("application/xml").getSchema().get$ref(), "#/components/schemas/Pet");
    // internal pathItem -> operation -> requestBody
    Schema id = (Schema) openAPI.getPaths().get("/pet/findByStatus").getGet().getRequestBody().getContent().get("multipart/mixed").getSchema().getProperties().get("id");
    assertEquals(id.get$ref(), "#/components/schemas/Pet");
    // internal parameter url
    assertEquals(openAPI.getPaths().get("/store/inventory").getGet().getParameters().get(0), openAPI.getComponents().getParameters().get("limitParam"));
}
Also used : OpenAPIDeserializer(io.swagger.v3.parser.util.OpenAPIDeserializer) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) JsonNode(com.fasterxml.jackson.databind.JsonNode) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) File(java.io.File) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 80 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testIssue85.

@Test
public void testIssue85(@Injectable final List<AuthorizationValue> auths) {
    String yaml = "openapi: '3.0.1'\n" + "paths: \n" + "  /test/method: \n" + "    post: \n" + "      parameters: \n" + "        - \n" + "          in: \"path\"\n" + "          name: \"body\"\n" + "          required: false\n" + "          schema: \n" + "            $ref: '#/components/Schemas/StructureA'\n" + "components: \n" + "   schemas:\n" + "       StructureA: \n" + "           type: object\n" + "           properties: \n" + "               someProperty: \n" + "                   type: string\n" + "               arrayOfOtherType: \n" + "                   type: array\n" + "                   items: \n" + "                       $ref: '#/definitions/StructureB'\n" + "       StructureB: \n" + "           type: object\n" + "           properties: \n" + "               someProperty: \n" + "                   type: string\n";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().readContents(yaml, auths, options).getOpenAPI();
    ResolverFully resolverUtil = new ResolverFully();
    resolverUtil.resolveFully(openAPI);
    Parameter param = openAPI.getPaths().get("/test/method").getPost().getParameters().get(0);
    Schema schema = param.getSchema();
    assertNotNull(schema.getProperties().get("someProperty"));
    ArraySchema am = (ArraySchema) schema.getProperties().get("arrayOfOtherType");
    assertNotNull(am);
    Schema prop = am.getItems();
    assertTrue(prop instanceof Schema);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ResolverFully(io.swagger.v3.parser.util.ResolverFully) Test(org.testng.annotations.Test)

Aggregations

ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)107 Schema (io.swagger.v3.oas.models.media.Schema)85 Test (org.testng.annotations.Test)76 StringSchema (io.swagger.v3.oas.models.media.StringSchema)63 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)53 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)53 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)51 OpenAPI (io.swagger.v3.oas.models.OpenAPI)49 MapSchema (io.swagger.v3.oas.models.media.MapSchema)28 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)22 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)21 PathItem (io.swagger.v3.oas.models.PathItem)16 Parameter (io.swagger.v3.oas.models.parameters.Parameter)16 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)16 MediaType (io.swagger.v3.oas.models.media.MediaType)15 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)15 Operation (io.swagger.v3.oas.models.Operation)14 Content (io.swagger.v3.oas.models.media.Content)12 DateSchema (io.swagger.v3.oas.models.media.DateSchema)11 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)11