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());
}
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"));
}
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);
}
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());
}
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);
}
Aggregations