Search in sources :

Example 86 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.

the class ParameterSerializationTest method testReadOnlyParameter.

@Test(description = "should mark a parameter as readOnly")
public void testReadOnlyParameter() throws Exception {
    final QueryParameter qp = new QueryParameter();
    qp.setSchema(new StringSchema().readOnly(true));
    final String json = "{" + "   \"in\":\"query\"," + "   \"schema\":{" + "      \"type\":\"string\"," + "      \"readOnly\":true" + "   }" + "}";
    SerializationMatchers.assertEqualsToJson(qp, json);
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 87 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.

the class PropertySerializationTest method deserializeEnumStringProperty.

@Test(description = "it should deserialize a StringProperty with enums")
public void deserializeEnumStringProperty() throws IOException {
    final String json = "{\"type\":\"string\",\"enum\":[\"a\",\"b\"]}";
    final Schema p = m.readValue(json, Schema.class);
    assertEquals(p.getType(), "string");
    List<String> _enum = ((StringSchema) p).getEnum();
    assertNotNull(_enum);
    assertEquals(_enum, Arrays.asList("a", "b"));
    assertEquals(p.getClass(), StringSchema.class);
    assertEquals(m.writeValueAsString(p), json);
}
Also used : FileSchema(io.swagger.v3.oas.models.media.FileSchema) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 88 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.

the class PropertySerializationTest method deserializeObjectPropertyWithRequiredProperties.

@Test(description = "it should deserialize an object property with required set")
public void deserializeObjectPropertyWithRequiredProperties() throws IOException {
    final Schema p = new ObjectSchema().addProperties("stringProperty", new StringSchema());
    p.required(Arrays.asList("stringProperty"));
    final String json = "{\"type\":\"object\",\"properties\":{\"stringProperty\":{\"type\":\"string\"}},\"required\":[\"stringProperty\"]}";
    assertEquals(p, m.readValue(json, Schema.class));
}
Also used : ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) FileSchema(io.swagger.v3.oas.models.media.FileSchema) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 89 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.

the class PropertySerializationTest method deserializeNotReadOnlyStringProperty.

@Test(description = "it should serialize a string property with readOnly unset")
public void deserializeNotReadOnlyStringProperty() throws IOException {
    final StringSchema p = new StringSchema();
    p.setReadOnly(false);
    final String json = "{\"type\":\"string\",\"readOnly\":false}";
    assertEquals(m.writeValueAsString(p), json);
}
Also used : StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 90 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project cruise-control by linkedin.

the class ResponseTest method checkSchema.

/**
 * Check OpenApi's JSON schema definition against with Java response class.
 * The check focus on two things.
 * <ul>
 *   <li>The response hierarchy are the same. In Json response, if a field's value is an array or map, a new layer is
 *   introduced. The check ensures the layout of layer are the same.</li>
 *   <li>Within each layer, the field key set are the same.</li>
 * </ul>
 *
 * @param schema The OpenApi schema
 * @param className The name of corresponding Java response class.
 * @param isOutermost Whether the schema is the outermost layer of the response.
 */
private void checkSchema(Schema schema, String className, boolean isOutermost) {
    // Get the complete field key set from Java class.
    Map<String, Boolean> fields = extractFieldKeys(_schemaToClass.get(className));
    Map<String, Schema> properties = schema.getProperties();
    // otherwise it may contain version field because the it can be reused as the outermost layer in another response.
    if (isOutermost) {
        assertEquals(properties.size(), fields.size() + 1);
        assertTrue(properties.get(VERSION) instanceof IntegerSchema);
    } else {
        if (properties.size() - fields.size() == 1) {
            assertTrue(properties.get(VERSION) instanceof IntegerSchema);
        } else {
            assertEquals(className, properties.size(), fields.size());
        }
    }
    // Check other fields
    properties.forEach((k, v) -> {
        // Skip version field here.
        if (!k.equals(VERSION)) {
            // Check key exists.
            assertTrue(k + " does not exist in corresponding Java class.", fields.containsKey(k));
            // Check key's necessity.
            assertEquals(className + ":" + k, fields.get(k), schema.getRequired() != null && schema.getRequired().contains(k));
            // Check value
            Schema schemaToCheck = v;
            while (schemaToCheck instanceof ArraySchema || schemaToCheck instanceof MapSchema) {
                if (schemaToCheck instanceof ArraySchema) {
                    schemaToCheck = ((ArraySchema) schemaToCheck).getItems();
                } else {
                    schemaToCheck = (Schema) schemaToCheck.getAdditionalProperties();
                }
            }
            if (schemaToCheck instanceof StringSchema || schemaToCheck instanceof IntegerSchema || schemaToCheck instanceof NumberSchema || schemaToCheck instanceof ComposedSchema || schemaToCheck instanceof BooleanSchema) {
                return;
            }
            assertNotEquals(schemaToCheck.get$ref(), null);
            String refName = schemaToCheck.get$ref();
            String schemaName = refName.substring(refName.lastIndexOf('/') + 1);
            checkSchema(_openAPI.getComponents().getSchemas().get(schemaName), schemaName, false);
        }
    });
}
Also used : BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) 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) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema)

Aggregations

StringSchema (io.swagger.v3.oas.models.media.StringSchema)89 Test (org.testng.annotations.Test)79 Schema (io.swagger.v3.oas.models.media.Schema)70 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)54 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)53 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)47 OpenAPI (io.swagger.v3.oas.models.OpenAPI)40 PathItem (io.swagger.v3.oas.models.PathItem)22 Operation (io.swagger.v3.oas.models.Operation)21 MediaType (io.swagger.v3.oas.models.media.MediaType)21 Content (io.swagger.v3.oas.models.media.Content)20 MapSchema (io.swagger.v3.oas.models.media.MapSchema)20 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)20 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)19 DateSchema (io.swagger.v3.oas.models.media.DateSchema)17 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)16 Components (io.swagger.v3.oas.models.Components)15 Parameter (io.swagger.v3.oas.models.parameters.Parameter)15 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)15 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)14