Search in sources :

Example 51 with StringSchema

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

the class InlineModelResolverTest method resolveInlineArrayModelWithTitle.

@Test
public void resolveInlineArrayModelWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());
    Schema objectSchema = new ObjectSchema();
    objectSchema.setTitle("InnerUserTitle");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());
    ArraySchema arraySchema = new ArraySchema();
    List<String> required = new LinkedList<>();
    required.add("name");
    arraySchema.setRequired(required);
    arraySchema.setItems(objectSchema);
    openAPI.getComponents().addSchemas("User", arraySchema);
    new InlineModelResolver().flatten(openAPI);
    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ArraySchema);
    Schema user = openAPI.getComponents().getSchemas().get("InnerUserTitle");
    assertNotNull(user);
    assertEquals("description", user.getDescription());
}
Also used : Components(io.swagger.v3.oas.models.Components) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 52 with StringSchema

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

the class InlineModelResolverTest method resolveInlineRequestBodyWithTitle.

@Test
public void resolveInlineRequestBodyWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("name", new StringSchema());
    Schema addressModelItem = new Schema();
    String addressModelName = "DetailedAddress";
    addressModelItem.setTitle(addressModelName);
    addressModelItem.addProperties("address", objectSchema);
    openAPI.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(addressModelItem))))));
    new InlineModelResolver().flatten(openAPI);
    Operation operation = openAPI.getPaths().get("/hello").getGet();
    RequestBody requestBody = operation.getRequestBody();
    assertTrue(requestBody.getContent().get("*/*").getSchema().get$ref() != null);
    Schema body = openAPI.getComponents().getSchemas().get(addressModelName);
    assertTrue(body instanceof Schema);
    assertNotNull(body.getProperties().get("address"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 53 with StringSchema

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

the class OpenAPIDeserializerTest method testSchema.

@Test
public void testSchema() throws Exception {
    String json = "{\n" + "  \"type\":\"object\",\n" + "  \"properties\": {\n" + "    \"data\": {\n" + "      \"properties\": {\n" + "        \"name\": {\n" + "          \"type\": \"string\",\n" + "          \"minLength\": 1\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"required\": [\n" + "    \"data\"\n" + "  ]\n" + "}";
    Schema m = Json.mapper().readValue(json, Schema.class);
    assertNotNull(m);
    Map<String, Schema> properties = m.getProperties();
    assertTrue(properties.keySet().size() == 1);
    Schema data = properties.get("data");
    assertTrue(data instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) data;
    Map<String, Schema> innerProperties = ((ObjectSchema) data).getProperties();
    assertTrue(innerProperties.keySet().size() == 1);
    Schema name = innerProperties.get("name");
    assertTrue(name instanceof StringSchema);
}
Also used : 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) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 54 with StringSchema

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

the class ExternalRefProcessorTest method testNestedExternalRefs.

@Test
public void testNestedExternalRefs(@Injectable final Schema mockedModel) {
    final RefFormat refFormat = RefFormat.URL;
    // Swagger test instance
    OpenAPI testedOpenAPI = new OpenAPI();
    // Start with customer, add address property to it
    final String customerURL = "http://my.company.com/path/to/customer.json#/definitions/Customer";
    final Schema customerModel = new Schema();
    Map<String, Schema> custProps = new HashMap<>();
    Schema address = new Schema();
    final String addressURL = "http://my.company.com/path/to/address.json#/definitions/Address";
    address.set$ref(addressURL);
    custProps.put("Address", address);
    // Create a 'local' reference to something in #/definitions, this should be ignored a no longer result in a null pointer exception
    final String loyaltyURL = "#/definitions/LoyaltyScheme";
    Schema loyaltyProp = new Schema();
    loyaltyProp.set$ref(loyaltyURL);
    loyaltyProp.setName("LoyaltyCardNumber");
    List<String> required = new ArrayList<>();
    required.add("LoyaltyCardNumber");
    loyaltyProp.setRequired(required);
    custProps.put("Loyalty", loyaltyProp);
    customerModel.setProperties(custProps);
    // create address model, add Contact Ref Property to it
    final Schema addressModel = new Schema();
    Map<String, Schema> addressProps = new HashMap<>();
    Schema contact = new Schema();
    final String contactURL = "http://my.company.com/path/to/Contact.json#/definitions/Contact";
    contact.set$ref(contactURL);
    addressProps.put("Contact", contact);
    addressModel.setProperties(addressProps);
    // Create contact model, with basic type property
    final Schema contactModel = new Schema();
    Schema contactProp = new StringSchema();
    contactProp.setName("PhoneNumber");
    List<String> requiredList = new ArrayList<>();
    requiredList.add("PhoneNumber");
    contactProp.setRequired(requiredList);
    Map<String, Schema> contactProps = new HashMap<>();
    contactProps.put("PhoneNumber", contactProp);
    contactModel.setProperties(contactProps);
    new Expectations() {

        {
            cache.loadRef(customerURL, refFormat, Schema.class);
            result = customerModel;
            times = 1;
            cache.loadRef(addressURL, refFormat, Schema.class);
            result = addressModel;
            times = 1;
            cache.loadRef(contactURL, refFormat, Schema.class);
            result = contactModel;
            times = 1;
        }
    };
    new ExternalRefProcessor(cache, testedOpenAPI).processRefToExternalSchema(customerURL, refFormat);
    assertThat(testedOpenAPI.getComponents().getSchemas().get("Customer"), notNullValue());
    assertThat(testedOpenAPI.getComponents().getSchemas().get("Contact"), notNullValue());
    assertThat(testedOpenAPI.getComponents().getSchemas().get("Address"), notNullValue());
}
Also used : Expectations(mockit.Expectations) StrictExpectations(mockit.StrictExpectations) HashMap(java.util.HashMap) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) RefFormat(io.swagger.v3.parser.models.RefFormat) ArrayList(java.util.ArrayList) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 55 with StringSchema

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

the class LocalReferenceTest method testAuth.

@Test
public void testAuth() throws Exception {
    new Expectations() {

        {
            remoteUrl.urlToString("https://remote-server.com/issue-454.yaml", new ArrayList<>());
            result = issue_454_yaml;
            remoteUrl.urlToString("https://remote-components.com/issue-454-components", new ArrayList<>());
            result = issue_454_components_yaml;
        }
    };
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    OpenAPI swagger = new OpenAPIV3Parser().read("https://remote-server.com/issue-454.yaml", null, options);
    assertNotNull(swagger.getComponents().getSchemas().get("ErrorModel"));
    assertNotNull(swagger.getComponents().getSchemas().get("ModelWithNestedProperties"));
    Schema model = swagger.getComponents().getSchemas().get("ModelWithNestedProperties");
    Schema property = (Schema) model.getProperties().get("remoteProperty");
    assertNotNull(property);
    assertTrue(property.get$ref() != null);
    assertEquals(property.get$ref(), "#/components/schemas/RemoteComponent");
    assertNotNull(swagger.getComponents().getSchemas().get("NestedProperty"));
    Schema nestedModel = swagger.getComponents().getSchemas().get("NestedProperty");
    assertNotNull(nestedModel);
    assertNotNull(nestedModel.getProperties().get("name"));
    assertTrue(nestedModel.getProperties().get("name") instanceof StringSchema);
}
Also used : Expectations(mockit.Expectations) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

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