use of io.swagger.models.ModelImpl in project swagger-parser by swagger-api.
the class ResolverCache method updateLocalRefs.
protected <T> void updateLocalRefs(String file, Model schema) {
if (schema instanceof RefModel) {
RefModel ref = (RefModel) schema;
String updatedLocation = merge(file, ref.get$ref());
ref.set$ref(updatedLocation);
} else if (schema instanceof ModelImpl) {
ModelImpl impl = (ModelImpl) schema;
if (impl.getProperties() != null) {
for (Property property : schema.getProperties().values()) {
updateLocalRefs(file, property);
}
}
}
}
use of io.swagger.models.ModelImpl in project swagger-parser by swagger-api.
the class LocalReferenceTests method testAuth.
@Test
public void testAuth() throws Exception {
new Expectations() {
{
remoteUrl.urlToString("https://remote-server.com/issue-454.yaml", null);
result = issue_454_yaml;
remoteUrl.urlToString("https://remote-components.com/issue-454-components", null);
result = issue_454_components_yaml;
}
};
Swagger swagger = new SwaggerParser().read("https://remote-server.com/issue-454.yaml", null, true);
assertNotNull(swagger.getDefinitions().get("ErrorModel"));
assertNotNull(swagger.getDefinitions().get("ModelWithNestedProperties"));
ModelImpl model = (ModelImpl) swagger.getDefinitions().get("ModelWithNestedProperties");
Property property = model.getProperties().get("remoteProperty");
assertNotNull(property);
assertTrue(property instanceof RefProperty);
RefProperty ref = (RefProperty) property;
assertEquals(ref.get$ref(), "#/definitions/RemoteComponent");
assertNotNull(swagger.getDefinitions().get("NestedProperty"));
ModelImpl nestedModel = (ModelImpl) swagger.getDefinitions().get("NestedProperty");
assertNotNull(nestedModel);
assertNotNull(nestedModel.getProperties().get("name"));
assertTrue(nestedModel.getProperties().get("name") instanceof StringProperty);
}
use of io.swagger.models.ModelImpl in project swagger-parser by swagger-api.
the class SwaggerParserTest method testCodegenPetstore.
@Test
public void testCodegenPetstore() {
SwaggerParser parser = new SwaggerParser();
final Swagger swagger = parser.read("src/test/resources/petstore-codegen.yaml");
ModelImpl enumModel = (ModelImpl) swagger.getDefinitions().get("Enum_Test");
assertNotNull(enumModel);
Property enumProperty = enumModel.getProperties().get("enum_integer");
assertNotNull(enumProperty);
assertTrue(enumProperty instanceof IntegerProperty);
IntegerProperty enumIntegerProperty = (IntegerProperty) enumProperty;
List<Integer> integers = enumIntegerProperty.getEnum();
assertEquals(integers.get(0), new Integer(1));
assertEquals(integers.get(1), new Integer(-1));
Operation getOrderOperation = swagger.getPath("/store/order/{orderId}").getGet();
assertNotNull(getOrderOperation);
Parameter orderId = getOrderOperation.getParameters().get(0);
assertTrue(orderId instanceof PathParameter);
PathParameter orderIdPathParam = (PathParameter) orderId;
assertNotNull(orderIdPathParam.getMinimum());
BigDecimal minimum = orderIdPathParam.getMinimum();
assertEquals(minimum.toString(), "1");
FormParameter formParam = (FormParameter) swagger.getPath("/fake").getPost().getParameters().get(3);
assertEquals(formParam.getMinimum().toString(), "32.1");
}
use of io.swagger.models.ModelImpl in project swagger-parser by swagger-api.
the class SwaggerParserTest method testIssue360.
@Test
public void testIssue360() {
SwaggerParser parser = new SwaggerParser();
final Swagger swagger = parser.read("src/test/resources/issue_360.yaml");
assertNotNull(swagger);
Parameter parameter = swagger.getPath("/pets").getPost().getParameters().get(0);
assertNotNull(parameter);
assertTrue(parameter instanceof BodyParameter);
BodyParameter bp = (BodyParameter) parameter;
assertNotNull(bp.getSchema());
Model model = bp.getSchema();
assertTrue(model instanceof ModelImpl);
ModelImpl impl = (ModelImpl) model;
assertNotNull(impl.getProperties().get("foo"));
Map<String, Object> extensions = bp.getVendorExtensions();
assertNotNull(extensions);
assertNotNull(extensions.get("x-examples"));
Object o = extensions.get("x-examples");
assertTrue(o instanceof Map);
Map<String, Object> on = (Map<String, Object>) o;
Object jn = on.get("application/json");
assertTrue(jn instanceof Map);
Map<String, Object> objectNode = (Map<String, Object>) jn;
assertEquals(objectNode.get("foo"), "bar");
Parameter stringBodyParameter = swagger.getPath("/otherPets").getPost().getParameters().get(0);
assertTrue(stringBodyParameter instanceof BodyParameter);
BodyParameter sbp = (BodyParameter) stringBodyParameter;
assertTrue(sbp.getRequired());
assertEquals(sbp.getName(), "simple");
Model sbpModel = sbp.getSchema();
assertTrue(sbpModel instanceof ModelImpl);
ModelImpl sbpModelImpl = (ModelImpl) sbpModel;
assertEquals(sbpModelImpl.getType(), "string");
assertEquals(sbpModelImpl.getFormat(), "uuid");
Parameter refBodyParameter = swagger.getPath("/evenMorePets").getPost().getParameters().get(0);
assertTrue(refBodyParameter instanceof BodyParameter);
BodyParameter ref = (BodyParameter) refBodyParameter;
assertTrue(ref.getRequired());
assertEquals(ref.getName(), "simple");
Model refModel = ref.getSchema();
assertTrue(refModel instanceof RefModel);
RefModel refModelImpl = (RefModel) refModel;
assertEquals(refModelImpl.getSimpleRef(), "Pet");
}
use of io.swagger.models.ModelImpl 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);
}
Aggregations