Hi Nuwan/API-M team, When adding an API to API-M through the new REST API, with api defintion and scopes together, the resources and scopes doesn't get added and the exception [2] gets thrown. For example use the payload [3].
I debugged the carbon-apimgt and found the issue. The PR is sent to the git repo [1]. Please review the change and merge it. [1] https://github.com/wso2/carbon-apimgt/pull/2312 The issue however doesn't happen when adding through the publisher. The reason is, the issue happens only when adding the API with scopes and api Definitions at the same time. In publisher however, it normally doesn't happen, since in the first page, the user add resources (uri-templates), and then add scopes and attach to it. But when using REST API, when both are used together, the scopes get added separately and subsequently the url templates get added without scope id's just generated without getting set in the uri-template objects, hence throwing a constraint violation at the DB level. Without the fix, the scope id in the template always get set as 0. Also when the API is updated with the REST API, the newly added uri-temples and scopes do not get updated as well. The PR contains the fix for that as well. This is due to not saving the swagger definitions in registry in the apisApiIdPut method. [2] Caused by: org.wso2.carbon.apimgt.api.APIManagementException: Error while adding URL template(s) to the database for API : admin-MyTest-1.1.1 at org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO.handleException(ApiMgtDAO.java:7259) at org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO.addURLTemplates(ApiMgtDAO.java:6611) at org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO.addAPI(ApiMgtDAO.java:6269) at org.wso2.carbon.apimgt.impl.APIProviderImpl.addAPI(APIProviderImpl.java:589) ... 52 more Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`apimdb`.`idn_oauth2_resource_scope`, CONSTRAINT `idn_oauth2_resource_scope_ibfk_1` FOREIGN KEY (`SCOPE_ID`) REFERENCES `idn_oauth2_scope` (`SCOPE_ID`) ON DELETE CASCADE ON UPDATE CASCADE) at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2056) at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1467) at org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO.addURLTemplates(ApiMgtDAO.java:6607) ... 54 more Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`apimdb`.`idn_oauth2_resource_scope`, CONSTRAINT `idn_oauth2_resource_scope_ibfk_1` FOREIGN KEY (`SCOPE_ID`) REFERENCES `idn_oauth2_scope` (`SCOPE_ID`) ON DELETE CASCADE ON UPDATE CASCADE) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2818) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2157) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2460) at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2008) ... 56 more [3] { "name": "TestAPI", "provider": "admin", "version": "2.2.3", "description": "This is a test API", "context": "testing", "cacheTimeout": 300, "subscriptionAvailability": null, "isDefaultVersion": false, "responseCaching": "Enabled", "visibility": "PUBLIC", "sequences": [ { "name": "json_to_xml_in_message", "config": null, "type": "in" }, { "name": "xml_to_json_out_message", "config": null, "type": "out" }, { "name": "json_fault", "config": null, "type": "fault" } ], "businessInformation": { "businessOwner": "sample", "businessOwnerEmail": "sam...@gmail.com", "technicalOwner": "check", "technicalOwnerEmail": "ch...@wso2.com" }, "tiers": [ "Bronze" ], "transport": [ "https", "http" ], "visibleRoles": [ "test", "good" ], "visibleTenants": [], "endpointConfig": "{\"endpoint_type\":\"http\",\"production_endpoints\":{\"url\":\"http://laptop.com/test\",\"config\":null},\"sandbox_endpoints\":{\"url\":\"https://googleplay.com/evening\",\"config\":null}}", "apiDefinition": "{\"info\":{\"title\":\"TestAPI\",\"description\":\"This is a test API\",\"version\":\"2.2.3\",\"contact\":{\"email\":\"sam...@gmail.com\",\"name\":\"sample\"}},\"swagger\":\"2.0\",\"x-wso2-security\":{\"apim\":{\"x-wso2-scopes\":[{\"description\":\"This is a test scope\",\"roles\":\"admin\",\"name\":\"appScope\",\"key\":\"appScope\"}]}},\"paths\":{\"/test\":{\"get\":{\"x-auth-type\":\"None\",\"x-scope\":\"appScope\",\"x-throttling-tier\":\"Unlimited\"}},\"\/good\":{\"get\":{\"x-auth-type\":\"None\",\"x-scope\":\"appScope\",\"x-throttling-tier\":\"Unlimited\"}}}}" } Shazni Nazeer Mob : +94 777737331 LinkedIn : http://lk.linkedin.com/in/shazninazeer Blog : http://shazninazeer.blogspot.com
_______________________________________________ Dev mailing list Dev@wso2.org http://wso2.org/cgi-bin/mailman/listinfo/dev