@Test public void resolveInlineModelTest() throws Exception { Swagger swagger = new Swagger(); swagger.addDefinition( "User", new ModelImpl() .name("user") .description("a common user") .property("name", new StringProperty()) .property( "address", new ObjectProperty() .title("title") ._default("default") .access("access") .readOnly(false) .required(true) .description("description") .name("name") .property("street", new StringProperty()) .property("city", new StringProperty()))); new InlineModelResolver().flatten(swagger); ModelImpl user = (ModelImpl) swagger.getDefinitions().get("User"); assertNotNull(user); assertTrue(user.getProperties().get("address") instanceof RefProperty); ModelImpl address = (ModelImpl) swagger.getDefinitions().get("User_address"); assertNotNull(address); assertNotNull(address.getProperties().get("city")); assertNotNull(address.getProperties().get("street")); }
@Test public void resolveInlineBodyParameter() throws Exception { Swagger swagger = new Swagger(); swagger.path( "/hello", new Path() .get( new Operation() .parameter( new BodyParameter() .name("body") .schema( new ModelImpl() .property( "address", new ObjectProperty() .property("street", new StringProperty())) .property("name", new StringProperty()))))); new InlineModelResolver().flatten(swagger); Operation operation = swagger.getPaths().get("/hello").getGet(); BodyParameter bp = (BodyParameter) operation.getParameters().get(0); assertTrue(bp.getSchema() instanceof RefModel); Model body = swagger.getDefinitions().get("body"); assertTrue(body instanceof ModelImpl); ModelImpl impl = (ModelImpl) body; assertNotNull(impl.getProperties().get("address")); }
@Override public Model resolve(Type type) { if (processedTypes.contains(type)) { return modelByType.get(type); } else { processedTypes.add(type); } if (LOGGER.isDebugEnabled()) { LOGGER.debug(String.format("resolve %s", type)); } Iterator<ModelConverter> converters = this.getConverters(); Model resolved = null; if (converters.hasNext()) { ModelConverter converter = converters.next(); LOGGER.debug("trying extension " + converter); resolved = converter.resolve(type, this, converters); } if (resolved != null) { modelByType.put(type, resolved); if (resolved instanceof ModelImpl) { ModelImpl impl = (ModelImpl) resolved; if (impl.getName() != null) { modelByName.put(impl.getName(), resolved); } } } return resolved; }
void updateObjectModel( final ModelImpl model, final ObjectSpecification objectSpecification, final List<OneToOneAssociation> objectProperties, final List<OneToManyAssociation> objectCollections) { final String objectType = objectTypeFor(objectSpecification); final String className = objectSpecification.getFullIdentifier(); model.type("object").description(String.format("%s (%s)", objectType, className)); for (OneToOneAssociation objectProperty : objectProperties) { model.property(objectProperty.getId(), propertyFor(objectProperty.getSpecification())); } for (OneToManyAssociation objectCollection : objectCollections) { final ObjectSpecification elementSpec = objectCollection.getSpecification(); model.property(objectCollection.getId(), arrayPropertyOf(elementSpec)); } }
@Test public void testInlineResponseModel() throws Exception { Swagger swagger = new Swagger(); swagger .path( "/foo/bar", new Path() .get( new Operation() .response( 200, new Response() .description("it works!") .schema( new ObjectProperty().property("name", new StringProperty()))))) .path( "/foo/baz", new Path() .get( new Operation() .response( 200, new Response() .vendorExtension("x-foo", "bar") .description("it works!") .schema( new ObjectProperty().property("name", new StringProperty()))))); new InlineModelResolver().flatten(swagger); Map<String, Response> responses = swagger.getPaths().get("/foo/bar").getGet().getResponses(); Response response = responses.get("200"); assertNotNull(response); assertTrue(response.getSchema() instanceof RefProperty); ModelImpl model = (ModelImpl) swagger.getDefinitions().get("inline_response_200"); assertTrue(model.getProperties().size() == 1); assertNotNull(model.getProperties().get("name")); }
@Test public void notResolveNonModelBodyParameter() throws Exception { Swagger swagger = new Swagger(); swagger.path( "/hello", new Path() .get( new Operation() .parameter( new BodyParameter() .name("body") .schema(new ModelImpl().type("string").format("binary"))))); new InlineModelResolver().flatten(swagger); Operation operation = swagger.getPaths().get("/hello").getGet(); BodyParameter bp = (BodyParameter) operation.getParameters().get(0); assertTrue(bp.getSchema() instanceof ModelImpl); ModelImpl m = (ModelImpl) bp.getSchema(); assertEquals("string", m.getType()); assertEquals("binary", m.getFormat()); }
@Test public void testInlineMapResponseWithObjectProperty() throws Exception { Swagger swagger = new Swagger(); MapProperty schema = new MapProperty(); schema.setAdditionalProperties(new ObjectProperty().property("name", new StringProperty())); swagger.path( "/foo/baz", new Path() .get( new Operation() .response( 200, new Response() .vendorExtension("x-foo", "bar") .description("it works!") .schema(schema)))); new InlineModelResolver().flatten(swagger); Response response = swagger.getPaths().get("/foo/baz").getGet().getResponses().get("200"); Property property = response.getSchema(); assertTrue(property instanceof RefProperty); Model inline = swagger.getDefinitions().get("inline_response_200"); assertTrue(inline instanceof ModelImpl); ModelImpl impl = (ModelImpl) inline; Property innerProperty = impl.getAdditionalProperties(); assertTrue(innerProperty instanceof ObjectProperty); ObjectProperty obj = (ObjectProperty) innerProperty; Property name = obj.getProperties().get("name"); assertTrue(name instanceof StringProperty); }
void appendObjectActionInvokePath( final ObjectSpecification objectSpec, final ObjectAction objectAction) { final String objectType = objectTypeFor(objectSpec); final String actionId = objectAction.getId(); final List<ObjectActionParameter> parameters = objectAction.getParameters(); final Path path = new Path(); swagger.path( String.format("/objects/%s/{objectId}/actions/%s/invoke", objectType, actionId), path); final String tag = tagFor(objectType, null); final Operation invokeOperation = new Operation() .tag(tag) .description( Util.roSpec("19.1") + ": (invoke) resource of " + objectType + "#" + actionId) .parameter(new PathParameter().name("objectId").type("string")) .produces("application/json;profile=urn:org.apache.isis/v1") .produces("application/json;profile=urn:org.apache.isis/v1;suppress=true") .produces("application/json;profile=urn:org.restfulobjects:repr-types/action-result"); final ActionSemantics.Of semantics = objectAction.getSemantics(); if (semantics.isSafeInNature()) { path.get(invokeOperation); for (final ObjectActionParameter parameter : parameters) { invokeOperation.parameter( new QueryParameter() .name(parameter.getId()) .description( Util.roSpec("2.9.1") + (!Strings.isNullOrEmpty(parameter.getDescription()) ? (": " + parameter.getDescription()) : "")) .required(false) .type("string")); } if (!parameters.isEmpty()) { invokeOperation.parameter( new QueryParameter() .name("x-isis-querystring") .description( Util.roSpec("2.10") + ": all (formal) arguments as base64 encoded string") .required(false) .type("string")); } } else { if (semantics.isIdempotentInNature()) { path.put(invokeOperation); } else { path.post(invokeOperation); } final ModelImpl bodyParam = new ModelImpl().type("object"); for (final ObjectActionParameter parameter : parameters) { final ObjectSpecification specification = parameter.getSpecification(); final Property valueProperty = specification.isValue() ? modelFor(specification) : refToLinkModel(); bodyParam.property( parameter.getId(), new ObjectProperty().property("value", valueProperty)); } invokeOperation .consumes("application/json") .parameter(new BodyParameter().name("body").schema(bodyParam)); } invokeOperation.response( 200, new Response() .description(objectType + "#" + actionId) .schema(actionReturnTypeFor(objectAction))); }
void appendServiceActionInvokePath( final ObjectSpecification serviceSpec, final ObjectAction serviceAction) { final String serviceId = serviceIdFor(serviceSpec); final String actionId = serviceAction.getId(); final List<ObjectActionParameter> parameters = serviceAction.getParameters(); final Path path = new Path(); swagger.path(String.format("/services/%s/actions/%s/invoke", serviceId, actionId), path); final String tag = tagFor(serviceId, "> services"); final Operation invokeOperation = new Operation() .tag(tag) .description( Util.roSpec("19.1") + ": (invoke) resource of " + serviceId + "#" + actionId) .produces("application/json;profile=urn:org.apache.isis/v1") .produces("application/json;profile=urn:org.apache.isis/v1;suppress=true") .produces("application/json;profile=urn:org.restfulobjects:repr-types/action-result"); final ActionSemantics.Of semantics = serviceAction.getSemantics(); if (semantics.isSafeInNature()) { path.get(invokeOperation); for (final ObjectActionParameter parameter : parameters) { invokeOperation.parameter( new QueryParameter() .name(parameter.getId()) .description( Util.roSpec("2.9.1") + (!Strings.isNullOrEmpty(parameter.getDescription()) ? (": " + parameter.getDescription()) : "")) .required(false) .type("string")); } if (!parameters.isEmpty()) { invokeOperation.parameter( new QueryParameter() .name("x-isis-querystring") .description( Util.roSpec("2.10") + ": all (formal) arguments as base64 encoded string") .required(false) .type("string")); } } else { if (semantics.isIdempotentInNature()) { path.put(invokeOperation); } else { path.post(invokeOperation); } final ModelImpl bodyParam = new ModelImpl().type("object"); for (final ObjectActionParameter parameter : parameters) { final Property valueProperty; // TODO: need to switch on parameter's type and create appropriate impl of valueProperty // if(parameter.getSpecification().isValue()) ... valueProperty = stringProperty(); bodyParam.property( parameter.getId(), new ObjectProperty().property("value", valueProperty)); } invokeOperation .consumes("application/json") .parameter(new BodyParameter().name("body").schema(bodyParam)); } invokeOperation.response( 200, new Response() .description( serviceId + "#" + actionId + " , if Accept: application/json;profile=urn:org.apache.isis/v1") .schema(actionReturnTypeFor(serviceAction))); }
public Object clone() { ModelImpl cloned = new ModelImpl(); super.cloneTo(cloned); cloned.type = this.type; cloned.name = this.name; cloned.required = this.required; cloned.properties = this.properties; cloned.isSimple = this.isSimple; cloned.description = this.description; cloned.example = this.example; cloned.additionalProperties = this.additionalProperties; cloned.discriminator = this.discriminator; cloned.xml = this.xml; cloned.defaultValue = this.defaultValue; return cloned; }
public Model definition(ObjectNode node, String location, ParseResult result) { if (node == null) { result.missing(location, "empty schema"); } if (node.get("$ref") != null) { return refModel(node, location, result); } if (node.get("allOf") != null) { return allOfModel(node, location, result); } Model model = null; String value = null; String type = getString("type", node, false, location, result); Model m = new ModelImpl(); if ("array".equals(type)) { ArrayModel am = new ArrayModel(); ObjectNode propertyNode = getObject("properties", node, false, location, result); Map<String, Property> properties = properties(propertyNode, location, result); am.setProperties(properties); ObjectNode itemsNode = getObject("items", node, false, location, result); Property items = property(itemsNode, location, result); if (items != null) { am.items(items); } model = am; } else { ModelImpl impl = new ModelImpl(); impl.setType(value); JsonNode ap = node.get("additionalProperties"); if (ap != null && ap.getNodeType().equals(JsonNodeType.OBJECT)) { impl.setAdditionalProperties(Json.mapper().convertValue(ap, Property.class)); } value = getString("default", node, false, location, result); impl.setDefaultValue(value); value = getString("format", node, false, location, result); impl.setFormat(value); value = getString("discriminator", node, false, location, result); impl.setDiscriminator(value); JsonNode xml = node.get("xml"); if (xml != null) { impl.setXml(Json.mapper().convertValue(xml, Xml.class)); } ObjectNode externalDocs = getObject("externalDocs", node, false, location, result); ExternalDocs docs = externalDocs(externalDocs, location, result); impl.setExternalDocs(docs); ObjectNode properties = getObject("properties", node, true, location, result); if (properties != null) { Set<String> propertyNames = getKeys(properties); for (String propertyName : propertyNames) { JsonNode propertyNode = properties.get(propertyName); if (propertyNode.getNodeType().equals(JsonNodeType.OBJECT)) { ObjectNode on = (ObjectNode) propertyNode; Property property = property(on, location, result); impl.property(propertyName, property); } else { result.invalidType(location, "properties", "object", propertyNode); } } } // need to set properties first ArrayNode required = getArray("required", node, false, location, result); if (required != null) { List<String> requiredProperties = new ArrayList<String>(); for (JsonNode n : required) { if (n.getNodeType().equals(JsonNodeType.STRING)) { requiredProperties.add(((TextNode) n).textValue()); } else { result.invalidType(location, "required", "string", n); } } if (requiredProperties.size() > 0) { impl.setRequired(requiredProperties); } } // extra keys Set<String> keys = getKeys(node); for (String key : keys) { if (key.startsWith("x-")) { impl.setVendorExtension(key, extension(node.get(key))); } else if (!SCHEMA_KEYS.contains(key)) { result.extra(location, key, node.get(key)); } } if ("{ }".equals(Json.pretty(impl))) return null; model = impl; } JsonNode exampleNode = node.get("example"); if (exampleNode != null) { // we support text or object nodes if (exampleNode.getNodeType().equals(JsonNodeType.OBJECT)) { ObjectNode on = getObject("example", node, false, location, result); if (on != null) { model.setExample(on); } } else { model.setExample(exampleNode.asText()); } } if (model != null) { value = getString("description", node, false, location, result); model.setDescription(value); value = getString("title", node, false, location, result); model.setTitle(value); } return model; }