Search in sources :

Example 11 with ArraySchema

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

the class OpenAPIDeserializerTest method readSchemaArray.

@Test(dataProvider = "data")
public void readSchemaArray(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    final Paths paths = openAPI.getPaths();
    Assert.assertNotNull(paths);
    Assert.assertEquals(paths.size(), 19);
    // parameters operation get
    PathItem petByStatusEndpoint = paths.get("/pet/findByTags");
    Assert.assertNotNull(petByStatusEndpoint.getGet());
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().size(), 1);
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(0).getSchema());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getSchema().getType(), "array");
    Assert.assertEquals(((ArraySchema) (petByStatusEndpoint.getGet().getParameters().get(0).getSchema())).getItems().getType(), "string");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getName(), "tags");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getExplode(), Boolean.TRUE);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getStyle(), StyleEnum.FORM);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 12 with ArraySchema

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

the class OpenAPIDeserializerTest method testArraySchema.

@Test
public void testArraySchema() throws Exception {
    String json = "{\n" + "  \"properties\": {\n" + "    \"data\": {\n" + "      \"description\": \"the array type\",\n" + "      \"type\": \"array\",\n" + "      \"items\": {\n" + "        \"properties\": {\n" + "          \"name\": {\n" + "            \"description\": \"the inner type\",\n" + "            \"type\": \"string\",\n" + "            \"minLength\": 1\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"required\": [\n" + "    \"data\"\n" + "  ]\n" + "}";
    Schema m = Json.mapper().readValue(json, Schema.class);
    Schema data = (Schema) m.getProperties().get("data");
    assertTrue(data instanceof ArraySchema);
    ArraySchema ap = (ArraySchema) data;
    assertEquals("the array type", ap.getDescription());
    Schema inner = ap.getItems();
    assertNotNull(inner);
    assertTrue(inner instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) inner;
    Schema name = op.getProperties().get("name");
    assertEquals(name.getDescription(), "the inner type");
    assertTrue((name).getMinLength() == 1);
}
Also used : ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) 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) MapSchema(io.swagger.v3.oas.models.media.MapSchema) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) Test(org.testng.annotations.Test)

Example 13 with ArraySchema

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

the class OpenAPIParserTest method testIssueRelativeRefs2.

@Test
public void testIssueRelativeRefs2() {
    String location = "exampleSpecs/specs/my-domain/test-api/v1/test-api-swagger_v1.json";
    ParseOptions po = new ParseOptions();
    po.setResolve(true);
    SwaggerParseResult result = new OpenAPIParser().readLocation(location, null, po);
    assertNotNull(result.getOpenAPI());
    OpenAPI openAPI = result.getOpenAPI();
    Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
    Assert.assertTrue(schemas.get("confirmMessageType_v01").getProperties().get("resources") instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema) schemas.get("confirmMessageType_v01").getProperties().get("resources");
    Schema prop = (Schema) arraySchema.getItems().getProperties().get("resourceID");
    assertEquals(prop.get$ref(), "#/components/schemas/simpleIDType_v01");
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.junit.Test)

Example 14 with ArraySchema

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

the class OpenAPIParserTest method testIssueRelativeRefs1.

@Test
public void testIssueRelativeRefs1() {
    String location = "specs2/my-domain/test-api/v1/test-api-swagger_v1.json";
    ParseOptions po = new ParseOptions();
    po.setResolve(true);
    SwaggerParseResult result = new OpenAPIParser().readLocation(location, null, po);
    assertNotNull(result.getOpenAPI());
    OpenAPI openAPI = result.getOpenAPI();
    Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
    Assert.assertTrue(schemas.get("test-api-schema_v01").getProperties().get("testingApi") instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema) schemas.get("test-api-schema_v01").getProperties().get("testingApi");
    Schema prop = (Schema) arraySchema.getItems().getProperties().get("itemID");
    assertEquals(prop.get$ref(), "#/components/schemas/simpleIDType_v01");
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.junit.Test)

Example 15 with ArraySchema

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

the class SwaggerConverter method convert.

public Schema convert(io.swagger.models.Model v2Model) {
    if (v2Model == null) {
        return null;
    }
    Schema result;
    if (v2Model instanceof ArrayModel) {
        ArraySchema arraySchema = Json.mapper().convertValue(v2Model, ArraySchema.class);
        arraySchema.setItems(convert(((ArrayModel) v2Model).getItems()));
        result = arraySchema;
    } else if (v2Model instanceof ComposedModel) {
        ComposedModel composedModel = (ComposedModel) v2Model;
        ComposedSchema composed = new ComposedSchema();
        composed.setDescription(composedModel.getDescription());
        if (composedModel.getExample() != null) {
            composed.setExample(composedModel.getExample());
        }
        if (composedModel.getExternalDocs() != null) {
            composed.setExternalDocs(convert(composedModel.getExternalDocs()));
        }
        composed.setTitle(composedModel.getTitle());
        composed.setExtensions(convert(composedModel.getVendorExtensions()));
        composed.setAllOf(composedModel.getAllOf().stream().map(this::convert).collect(Collectors.toList()));
        addProperties(v2Model, composed);
        result = composed;
    } else {
        String v2discriminator = null;
        if (v2Model instanceof ModelImpl) {
            ModelImpl model = (ModelImpl) v2Model;
            v2discriminator = model.getDiscriminator();
            model.setDiscriminator(null);
        }
        result = Json.mapper().convertValue(v2Model, Schema.class);
        addProperties(v2Model, result);
        if (v2Model instanceof ModelImpl) {
            ModelImpl model = (ModelImpl) v2Model;
            if (model.getAdditionalProperties() != null) {
                result.setAdditionalProperties(convert(model.getAdditionalProperties()));
            }
        } else if (v2Model instanceof RefModel) {
            RefModel ref = (RefModel) v2Model;
            if (ref.get$ref().indexOf("#/definitions") == 0) {
                String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
                result.set$ref(updatedRef);
            }
        }
        if (v2discriminator != null) {
            Discriminator discriminator = new Discriminator();
            discriminator.setPropertyName(v2discriminator);
            result.setDiscriminator(discriminator);
        }
    }
    if (v2Model.getVendorExtensions() != null) {
        Object nullableExtension = v2Model.getVendorExtensions().get("x-nullable");
        if (nullableExtension != null) {
            result.setNullable((Boolean) nullableExtension);
        }
    }
    return result;
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) FileSchema(io.swagger.v3.oas.models.media.FileSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ModelImpl(io.swagger.models.ModelImpl) ArrayModel(io.swagger.models.ArrayModel) Discriminator(io.swagger.v3.oas.models.media.Discriminator)

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