Search in sources :

Example 16 with ArraySchema

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

the class V2ConverterTest method testIssue15.

@Test(description = "Convert extensions everywhere applicable #15")
public void testIssue15() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_15_JSON);
    assertNotNull(oas);
    Info info = oas.getInfo();
    assertNotNull(info.getExtensions().get("x-apis-json"));
    assertNotNull(info.getLicense().getExtensions().get("x-notes"));
    assertNotNull(oas.getExternalDocs().getExtensions().get("x-docs-extension"));
    assertNotNull(oas.getTags().get(0).getExtensions().get("x-tag-extension"));
    assertNotNull(oas.getTags().get(0).getExternalDocs().getExtensions().get("x-tag-docs-extension"));
    PathItem pathItem = oas.getPaths().get("/something");
    assertNotNull(pathItem.getExtensions().get("x-path-item-extension"));
    Operation get = pathItem.getGet();
    assertNotNull(get.getExtensions().get("x-version"));
    assertNotNull(get.getExternalDocs().getExtensions().get("x-operation-docs-extension"));
    assertNotNull(get.getResponses().get("200").getExtensions().get("x-response-extension"));
    ArraySchema schema = (ArraySchema) get.getParameters().get(0).getSchema();
    assertNull(schema.getItems().getExtensions().get(X_EXAMPLE));
    Map<String, SecurityScheme> securitySchemes = oas.getComponents().getSecuritySchemes();
    assertNotNull(securitySchemes);
    assertNotNull(securitySchemes.get("OAuth2Implicit").getExtensions().get("x-auth-extension"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Test(org.testng.annotations.Test)

Example 17 with ArraySchema

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

the class SwaggerConverter method convert.

private Schema convert(SerializableParameter sp) {
    Schema schema;
    if ("file".equals(sp.getType())) {
        schema = new FileSchema();
    } else if ("array".equals(sp.getType())) {
        ArraySchema as = new ArraySchema();
        if (sp.getItems() != null) {
            as.setItems(convert(sp.getItems()));
        }
        schema = as;
    } else {
        PrimitiveType ptype = PrimitiveType.fromTypeAndFormat(sp.getType(), sp.getFormat());
        if (ptype != null) {
            schema = ptype.createProperty();
        } else {
            ptype = PrimitiveType.fromTypeAndFormat(sp.getType(), null);
            if (ptype != null) {
                schema = ptype.createProperty();
                schema.setFormat(sp.getFormat());
            } else {
                schema = new Schema();
                schema.setType(sp.getType());
                schema.setFormat(sp.getFormat());
            }
        }
    }
    schema.setDescription(sp.getDescription());
    schema.setReadOnly(sp.isReadOnly());
    schema.setEnum(sp.getEnum());
    if (sp.getMaxItems() != null) {
        schema.setMaxItems(sp.getMaxItems());
    }
    if (sp.getMinItems() != null) {
        schema.setMinItems(sp.getMinItems());
    }
    if (sp.isUniqueItems() != null) {
        schema.setUniqueItems(sp.isUniqueItems());
    }
    schema.setMaximum(sp.getMaximum());
    schema.setExclusiveMaximum(sp.isExclusiveMaximum());
    schema.setMinimum(sp.getMinimum());
    schema.setExclusiveMinimum(sp.isExclusiveMinimum());
    schema.setMinLength(sp.getMinLength());
    schema.setMaxLength(sp.getMaxLength());
    schema.setName(sp.getName());
    if (sp.getVendorExtensions() != null) {
        Object exampleExtension = sp.getVendorExtensions().get("x-example");
        if (exampleExtension != null) {
            schema.setExample(exampleExtension);
        }
        Object nullableExtension = sp.getVendorExtensions().get("x-nullable");
        if (nullableExtension != null) {
            schema.setNullable((Boolean) nullableExtension);
        }
        schema.setExtensions(convert(sp.getVendorExtensions()));
    }
    if (sp.getMultipleOf() != null) {
        schema.setMultipleOf(new BigDecimal(sp.getMultipleOf().toString()));
    }
    schema.setPattern(sp.getPattern());
    if (sp instanceof AbstractSerializableParameter) {
        AbstractSerializableParameter ap = (AbstractSerializableParameter) sp;
        schema.setDefault(ap.getDefault());
    }
    return schema;
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) 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) PrimitiveType(io.swagger.v3.core.util.PrimitiveType) FileSchema(io.swagger.v3.oas.models.media.FileSchema) BigDecimal(java.math.BigDecimal)

Example 18 with ArraySchema

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

the class V2ConverterTest method testIssue19.

@Test(description = "Request Body Completions")
public void testIssue19() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_19_JSON);
    RequestBody requestBody = oas.getPaths().get(POST_PATH).getPost().getRequestBody();
    assertNotNull(requestBody);
    Schema schema = requestBody.getContent().get(URLENCODED_CONTENT).getSchema();
    assertNotNull(schema);
    Map properties = schema.getProperties();
    assertEquals(PROPERTIES_SIZE, properties.size());
    ArraySchema ids = (ArraySchema) properties.get("ids");
    assertEquals(new Integer(MIN_ITEMS), ids.getMinItems());
    assertEquals(new Integer(MAXIMUM), ids.getMaxItems());
    // Fixed from Issue 19
    assertEquals(Boolean.TRUE, ids.getUniqueItems());
    Schema login = (Schema) properties.get("login");
    assertEquals(new Integer(MIN_LENGTH), login.getMinLength());
    assertEquals(new Integer(MAX_LENGTH), login.getMaxLength());
    assertEquals(PATTERN, login.getPattern());
    Schema favNumber = (Schema) properties.get("favNumber");
    assertEquals(new BigDecimal(MAXIMUM), favNumber.getMinimum());
    assertEquals(new BigDecimal(MAXIMUM), favNumber.getMaximum());
    assertEquals(Boolean.TRUE, favNumber.getExclusiveMinimum());
    assertEquals(Boolean.TRUE, favNumber.getExclusiveMaximum());
    assertEquals(new BigDecimal(MULTIPLE_OF_VALUE), new BigDecimal(favNumber.getMultipleOf().doubleValue()));
    Schema dayOfWeek = (Schema) properties.get("dayOfWeek");
    assertEquals(MONDAY_TUESDAY_VALUE, dayOfWeek.getDefault());
    assertEquals(MONDAY_TUESDAY_VALUE, dayOfWeek.getEnum().get(0));
    assertEquals(TUESDAY_WEDNESDAY_VALUE, dayOfWeek.getEnum().get(1));
    assertEquals(WEDNESDAY_THURSDAY_VALUE, dayOfWeek.getEnum().get(2));
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) BigDecimal(java.math.BigDecimal) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 19 with ArraySchema

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

the class OpenAPIResolverTest method componentsResolver.

@Test
public void componentsResolver() 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);
    Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
    // internal url schema
    Schema pet = schemas.get("Pet");
    Schema category = (Schema) pet.getProperties().get("category");
    assertEquals(category.get$ref(), "#/components/schemas/Category");
    // remote url schema
    Schema user = (Schema) pet.getProperties().get("user");
    assertEquals(user.get$ref(), "#/components/schemas/User");
    // ArraySchema items
    ArraySchema tagsProperty = (ArraySchema) pet.getProperties().get("tags");
    assertEquals(tagsProperty.getItems().get$ref(), "#/components/schemas/ExampleSchema");
    assertEquals(tagsProperty.getType(), "array");
    Assert.assertNotNull(openAPI.getComponents().getSchemas().get("ExampleSchema"));
    // Schema not
    assertEquals(schemas.get("OrderRef").getNot().get$ref(), "#/components/schemas/Category");
    // Schema additionalProperties
    assertTrue(schemas.get("OrderRef").getAdditionalProperties() instanceof Schema);
    Schema additionalProperties = (Schema) schemas.get("OrderRef").getAdditionalProperties();
    assertEquals(additionalProperties.get$ref(), "#/components/schemas/User");
    // AllOfSchema
    ComposedSchema extended = (ComposedSchema) schemas.get("ExtendedErrorModel");
    Schema root = (Schema) extended.getAllOf().get(0).getProperties().get("rootCause");
    assertEquals(root.get$ref(), "#/components/schemas/Category");
    Map<String, ApiResponse> responses = openAPI.getComponents().getResponses();
    // internal response headers
    ApiResponse illegalInput = responses.get("IllegalInput");
    assertEquals(illegalInput.getHeaders().get("X-Ref-Limit-Limit").get$ref(), "#/components/headers/X-Rate-Limit-Reset");
    // internal response links
    assertEquals(illegalInput.getLinks().get("address").get$ref(), "#/components/links/unsubscribe");
    // internal url response schema
    MediaType generalError = responses.get("GeneralError").getContent().get("application/json");
    assertEquals(generalError.getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
    Map<String, RequestBody> requestBodies = openAPI.getComponents().getRequestBodies();
    // internal url requestBody schema
    RequestBody requestBody1 = requestBodies.get("requestBody1");
    MediaType xmlMedia = requestBody1.getContent().get("application/json");
    assertEquals(xmlMedia.getSchema().get$ref(), "#/components/schemas/Pet");
    // internal url requestBody ArraySchema
    RequestBody requestBody2 = requestBodies.get("requestBody2");
    MediaType jsonMedia = requestBody2.getContent().get("application/json");
    ArraySchema items = (ArraySchema) jsonMedia.getSchema();
    assertEquals(items.getItems().get$ref(), "#/components/schemas/User");
    // internal request body
    assertEquals("#/components/requestBodies/requestBody2", requestBodies.get("requestBody3").get$ref());
    // remote request body url
    assertEquals(requestBodies.get("reference").get$ref(), "#/components/requestBodies/remote_requestBody");
    Map<String, Parameter> parameters = openAPI.getComponents().getParameters();
    // remote url parameter
    assertEquals(parameters.get("remoteParameter").get$ref(), "#/components/parameters/parameter");
    // internal Schema Parameter
    assertEquals(parameters.get("newParam").getSchema().get$ref(), "#/components/schemas/Tag");
    // parameter examples
    assertEquals(parameters.get("contentParameter").getExamples().get("cat"), openAPI.getComponents().getExamples().get("cat"));
    // parameter content schema
    assertEquals(parameters.get("contentParameter").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
    // internal Schema header
    Map<String, Header> headers = openAPI.getComponents().getHeaders();
    // header remote schema ref
    assertEquals(headers.get("X-Rate-Limit-Remaining").getSchema().get$ref(), "#/components/schemas/User");
    // header examples
    assertEquals(headers.get("X-Rate-Limit-Reset").getExamples().get("headerExample").get$ref(), "#/components/examples/dog");
    // remote header ref
    assertEquals(headers.get("X-Ref-Limit-Limit").get$ref(), "#/components/headers/X-Rate-Limit-Reset");
    // header content
    assertEquals(headers.get("X-Rate-Limit-Reset").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
    Map<String, Example> examples = openAPI.getComponents().getExamples();
    // internal url example
    Example frogExample = examples.get("frog");
    assertEquals(frogExample.get$ref(), "#/components/examples/cat");
    // remote example url
    assertEquals(examples.get("referenceCat").get$ref(), "#/components/examples/example");
    // internal url securityScheme
    SecurityScheme scheme = openAPI.getComponents().getSecuritySchemes().get("reference");
    assertEquals(scheme.getType(), SecurityScheme.Type.APIKEY);
    SecurityScheme remoteScheme = openAPI.getComponents().getSecuritySchemes().get("remote_reference");
    assertEquals(remoteScheme.getType(), SecurityScheme.Type.OAUTH2);
    Map<String, Link> links = openAPI.getComponents().getLinks();
    // internal link
    assertEquals(openAPI.getComponents().getLinks().get("referenced").get$ref(), "#/components/links/unsubscribe");
    // remote ref link
    assertEquals(openAPI.getComponents().getLinks().get("subscribe").get$ref(), "#/components/links/link");
    Map<String, Callback> callbacks = openAPI.getComponents().getCallbacks();
    // internal callback reference
    assertEquals(callbacks.get("referenced").get$ref(), "#/components/callbacks/failed");
    // callback pathItem -> operation ->requestBody
    assertEquals(callbacks.get("heartbeat").get("$request.query.heartbeat-url").getPost().getRequestBody().get$ref(), "#/components/requestBodies/requestBody3");
    // remote callback ref
    assertEquals(callbacks.get("remoteCallback").get$ref(), "#/components/callbacks/callback");
}
Also used : OpenAPIDeserializer(io.swagger.v3.parser.util.OpenAPIDeserializer) 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) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Example(io.swagger.v3.oas.models.examples.Example) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) MediaType(io.swagger.v3.oas.models.media.MediaType) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Callback(io.swagger.v3.oas.models.callbacks.Callback) Header(io.swagger.v3.oas.models.headers.Header) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) File(java.io.File) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Link(io.swagger.v3.oas.models.links.Link) Test(org.testng.annotations.Test)

Example 20 with ArraySchema

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

the class OpenAPIResolverTest method testIssue1170.

@Test
public void testIssue1170(@Injectable final List<AuthorizationValue> auths) {
    String path = "/issue-1170/swagger.yaml";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().readLocation(path, auths, options).getOpenAPI();
    // Array schema with items $ref
    Schema breedsListSchema = openAPI.getComponents().getSchemas().get("BreedsList");
    Schema breedSchema = openAPI.getComponents().getSchemas().get("Breed");
    assertNotNull(breedsListSchema);
    assertNotNull(breedSchema);
    assertTrue(breedsListSchema instanceof ArraySchema);
    Schema breedPropertySchema = ((ArraySchema) breedsListSchema).getItems().getProperties().get("breed");
    assertNotNull(breedPropertySchema);
    // Verify items resolved fully
    assertTrue(breedPropertySchema.get$ref() == null);
    assertTrue(breedPropertySchema == breedSchema);
    // Array schema with inline items object with $ref properties
    Schema petsListSchema = openAPI.getComponents().getSchemas().get("PetsList");
    Schema colouringsSchema = openAPI.getComponents().getSchemas().get("Colouring");
    Schema colourSchema = openAPI.getComponents().getSchemas().get("Colour");
    assertNotNull(petsListSchema);
    assertNotNull(colouringsSchema);
    assertNotNull(colourSchema);
    assertTrue(petsListSchema instanceof ArraySchema);
    Schema colouringPropertySchema = ((ArraySchema) petsListSchema).getItems().getProperties().get("colouring");
    assertNotNull(colouringPropertySchema);
    // Verify inline items resolved fully
    assertTrue(colouringPropertySchema.get$ref() == null);
    assertTrue(colouringPropertySchema == colouringsSchema);
}
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) OpenAPI(io.swagger.v3.oas.models.OpenAPI) 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