Search in sources :

Example 61 with RefProperty

use of io.swagger.models.properties.RefProperty in project swagger-parser by swagger-api.

the class SwaggerParserTest method testPetstore.

@Test
public void testPetstore() throws Exception {
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo("src/test/resources/petstore.json", null, true);
    assertNotNull(result);
    assertTrue(result.getMessages().isEmpty());
    Swagger swagger = result.getSwagger();
    Map<String, Model> definitions = swagger.getDefinitions();
    Set<String> expectedDefinitions = new HashSet<String>();
    expectedDefinitions.add("User");
    expectedDefinitions.add("Category");
    expectedDefinitions.add("Pet");
    expectedDefinitions.add("Tag");
    expectedDefinitions.add("Order");
    expectedDefinitions.add("PetArray");
    assertEquals(definitions.keySet(), expectedDefinitions);
    Model petModel = definitions.get("Pet");
    Set<String> expectedPetProps = new HashSet<String>();
    expectedPetProps.add("id");
    expectedPetProps.add("category");
    expectedPetProps.add("name");
    expectedPetProps.add("photoUrls");
    expectedPetProps.add("tags");
    expectedPetProps.add("status");
    assertEquals(petModel.getProperties().keySet(), expectedPetProps);
    ArrayModel petArrayModel = (ArrayModel) definitions.get("PetArray");
    assertEquals(petArrayModel.getType(), "array");
    RefProperty refProp = (RefProperty) petArrayModel.getItems();
    assertEquals(refProp.get$ref(), "#/definitions/Pet");
    assertNull(petArrayModel.getProperties());
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ArrayModel(io.swagger.models.ArrayModel) ArrayModel(io.swagger.models.ArrayModel) HashSet(java.util.HashSet) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Example 62 with RefProperty

use of io.swagger.models.properties.RefProperty in project swagger-parser by swagger-api.

the class SwaggerParserTest method assertResponse.

private void assertResponse(Swagger swagger, Map<String, Response> responsesMap, String responseCode, String expectedDescription, String expectedSchemaRef) {
    final Response response = responsesMap.get(responseCode);
    final RefProperty schema = (RefProperty) response.getSchema();
    assertEquals(response.getDescription(), expectedDescription);
    assertEquals(schema.getClass(), RefProperty.class);
    assertEquals(schema.get$ref(), expectedSchemaRef);
    assertTrue(swagger.getDefinitions().containsKey(schema.getSimpleRef()));
}
Also used : Response(io.swagger.models.Response) RefProperty(io.swagger.models.properties.RefProperty)

Example 63 with RefProperty

use of io.swagger.models.properties.RefProperty in project swagger-parser by swagger-api.

the class ExternalRefProcessorTest method testNestedExternalRefs.

@Test
public void testNestedExternalRefs(@Injectable final Model mockedModel) {
    final RefFormat refFormat = RefFormat.URL;
    // Swagger test instance
    Swagger testedSwagger = new Swagger();
    // Start with customer, add address property to it
    final String customerURL = "http://my.company.com/path/to/customer.json#/definitions/Customer";
    final Model customerModel = new ModelImpl();
    Map<String, Property> custProps = new HashMap<String, Property>();
    RefProperty address = new RefProperty();
    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";
    RefProperty loyaltyProp = new RefProperty();
    loyaltyProp.set$ref(loyaltyURL);
    loyaltyProp.setName("LoyaltyCardNumber");
    loyaltyProp.setRequired(true);
    custProps.put("Loyalty", loyaltyProp);
    customerModel.setProperties(custProps);
    // create address model, add Contact Ref Property to it
    final Model addressModel = new ModelImpl();
    Map<String, Property> addressProps = new HashMap<String, Property>();
    RefProperty contact = new RefProperty();
    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 Model contactModel = new ModelImpl();
    Property contactProp = new StringProperty();
    contactProp.setName("PhoneNumber");
    contactProp.setRequired(true);
    Map<String, Property> contactProps = new HashMap<String, Property>();
    contactProps.put("PhoneNumber", contactProp);
    contactModel.setProperties(contactProps);
    new Expectations() {

        {
            cache.loadRef(customerURL, refFormat, Model.class);
            result = customerModel;
            times = 1;
            cache.loadRef(addressURL, refFormat, Model.class);
            result = addressModel;
            times = 1;
            cache.loadRef(contactURL, refFormat, Model.class);
            result = contactModel;
            times = 1;
        }
    };
    String actualRef = new ExternalRefProcessor(cache, testedSwagger).processRefToExternalDefinition(customerURL, refFormat);
    assertTrue(testedSwagger.getDefinitions().get("Customer") != null);
    assertTrue(testedSwagger.getDefinitions().get("Contact") != null);
    assertTrue(testedSwagger.getDefinitions().get("Address") != null);
}
Also used : Expectations(mockit.Expectations) StrictExpectations(mockit.StrictExpectations) HashMap(java.util.HashMap) Swagger(io.swagger.models.Swagger) RefFormat(io.swagger.models.refs.RefFormat) Model(io.swagger.models.Model) StringProperty(io.swagger.models.properties.StringProperty) ModelImpl(io.swagger.models.ModelImpl) StringProperty(io.swagger.models.properties.StringProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Example 64 with RefProperty

use of io.swagger.models.properties.RefProperty in project swagger-parser by swagger-api.

the class PropertyProcessorTest method testProcessRefProperty_InternalRef.

@Test
public void testProcessRefProperty_InternalRef() throws Exception {
    expectCreationOfExternalRefProcessor();
    final String expectedRef = "#/definitions/foo";
    final RefProperty property = new RefProperty(expectedRef);
    new PropertyProcessor(cache, swagger).processProperty(property);
    new FullVerifications() {

        {
        }
    };
    assertEquals(property.get$ref(), expectedRef);
}
Also used : FullVerifications(mockit.FullVerifications) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Example 65 with RefProperty

use of io.swagger.models.properties.RefProperty in project swagger-parser by swagger-api.

the class PropertyProcessorTest method testProcessMapProperty_AdditionalPropertiesIsRefProperty.

@Test
public void testProcessMapProperty_AdditionalPropertiesIsRefProperty() throws Exception {
    expectCreationOfExternalRefProcessor();
    final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
    final RefProperty refProperty = new RefProperty(ref);
    MapProperty mapProperty = new MapProperty();
    mapProperty.setAdditionalProperties(refProperty);
    expectCallToExternalRefProcessor(ref, RefFormat.URL, "bar");
    new PropertyProcessor(cache, swagger).processProperty(mapProperty);
    new FullVerifications() {

        {
        }
    };
    assertEquals(((RefProperty) mapProperty.getAdditionalProperties()).get$ref(), "#/definitions/bar");
}
Also used : MapProperty(io.swagger.models.properties.MapProperty) FullVerifications(mockit.FullVerifications) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Aggregations

RefProperty (io.swagger.models.properties.RefProperty)74 Property (io.swagger.models.properties.Property)50 ArrayProperty (io.swagger.models.properties.ArrayProperty)46 StringProperty (io.swagger.models.properties.StringProperty)35 Test (org.testng.annotations.Test)35 Model (io.swagger.models.Model)23 MapProperty (io.swagger.models.properties.MapProperty)20 ModelImpl (io.swagger.models.ModelImpl)18 Response (io.swagger.models.Response)18 IntegerProperty (io.swagger.models.properties.IntegerProperty)18 Operation (io.swagger.models.Operation)17 RefModel (io.swagger.models.RefModel)17 LongProperty (io.swagger.models.properties.LongProperty)14 Path (io.swagger.models.Path)12 Swagger (io.swagger.models.Swagger)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 ArrayModel (io.swagger.models.ArrayModel)11 BodyParameter (io.swagger.models.parameters.BodyParameter)11 Map (java.util.Map)11