Skip to content

Commit

Permalink
Workaroud for faulty openapi codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
pulledtim committed Oct 26, 2023
1 parent 83e31ae commit f56c653
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 35 deletions.
9 changes: 9 additions & 0 deletions src/main/java/org/fiware/iam/ccs/model/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.fiware.iam.ccs.model;

/**
* Manipulated the generated models to avoid bug in openapi generator:
* - When creating a model for collections of objects, the equals/hashcode function are incorrect.
* - Maps are generated using the additionalProperties field, with rather inconvenient access methods
*
* Once this is fixed, the unmanipulated generated models should be used again
*/
22 changes: 0 additions & 22 deletions src/main/java/org/fiware/iam/repository/ServiceRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,4 @@
*/
public interface ServiceRepository extends PageableRepository<Service, String> {

/**
* Find services by their ID. All child values will be returned through left-joins to fill out the full entity.
*
* @param id of the service
* @return the complete service
*/
// @Join(value = "oidcScopes", type = Join.Type.LEFT_FETCH)
// @Join(value = "oidcScopes.credentials", type = Join.Type.LEFT_FETCH)
// @Join(value = "oidcScopes.credentials.trustedLists", type = Join.Type.LEFT_FETCH)
Service getById(String id);

/**
* Get all services. All child values will be returned through left-joins to fill out the full entity.
*
* @param pageable pagination information to be used
* @return the current page
*/
@NonNull
// @Join(value = "oidcScopes", type = Join.Type.LEFT_FETCH)
// @Join(value = "oidcScopes.credentials", type = Join.Type.LEFT_FETCH)
// @Join(value = "oidcScopes.credentials.trustedLists", type = Join.Type.LEFT_FETCH)
Page<Service> findAll(@NonNull Pageable pageable);
}
20 changes: 10 additions & 10 deletions src/main/java/org/fiware/iam/rest/ServiceApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public HttpResponse<Object> createService(@NonNull ServiceVO serviceVO) {
ServiceApi.PATH_GET_SERVICE.replace(
"{id}", savedService.getId())));
}

@Override
public HttpResponse<Object> deleteServiceById(@NonNull String id) {
if (!serviceRepository.existsById(id)) {
Expand All @@ -61,14 +61,15 @@ public HttpResponse<Object> deleteServiceById(@NonNull String id) {

@Override
public HttpResponse<ScopeVO> getScopeForService(@NonNull String id, @Nullable String oidcScope) {
if (!serviceRepository.existsById(id)) {
Optional<Service> service = serviceRepository.findById(id);
if (service.isEmpty()) {
return HttpResponse.notFound();
}
String selectedOidcScope =
oidcScope == null ?
serviceMapper.map(serviceRepository.getById(id)).getDefaultOidcScope() :
serviceMapper.map(service.get()).getDefaultOidcScope() :
oidcScope;
ServiceScopesEntryVO serviceScopesEntryVO = serviceMapper.map(serviceRepository.getById(id))
ServiceScopesEntryVO serviceScopesEntryVO = serviceMapper.map(service.get())
.getOidcScopes()
.get(selectedOidcScope);
ScopeVO scopeVO = new ScopeVO();
Expand All @@ -78,10 +79,10 @@ public HttpResponse<ScopeVO> getScopeForService(@NonNull String id, @Nullable St

@Override
public HttpResponse<ServiceVO> getService(@NonNull String id) {
if (!serviceRepository.existsById(id)) {
return HttpResponse.notFound();
}
return HttpResponse.ok(serviceMapper.map(serviceRepository.getById(id)));
return serviceRepository.findById(id)
.map(serviceMapper::map)
.map(HttpResponse::ok)
.orElse(HttpResponse.notFound());
}

@Override
Expand Down Expand Up @@ -119,8 +120,7 @@ public HttpResponse<ServiceVO> updateService(@NonNull String id, @NonNull Servic
// just in case none is set in the object
serviceVO.setId(id);
serviceRepository.deleteById(id);
return HttpResponse.ok(
serviceMapper.map(serviceRepository.save(serviceMapper.map(serviceVO))));
return HttpResponse.ok(serviceMapper.map(serviceRepository.save(serviceMapper.map(serviceVO))));
}

// validate a service vo, e.g. check forbidden null values
Expand Down
19 changes: 16 additions & 3 deletions src/main/resources/db/migration/V0_0_1__initial-migration.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
CREATE TABLE IF NOT EXISTS `service` (
`id` varchar(255) NOT NULL PRIMARY KEY,
`default_oidc_scope` varchar(255) NOT NULL,
`oidc_scopes` LONGTEXT NOT NULL
`id` varchar(255) NOT NULL PRIMARY KEY
);

CREATE TABLE IF NOT EXISTS `credential` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`credential_type` varchar(255) NOT NULL,
`service_id` varchar(255) NOT NULL,
CONSTRAINT `fk_service` FOREIGN KEY (`service_id`) REFERENCES `service` (`id`) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS `endpoint_entry` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`endpoint` varchar(255) NOT NULL,
`type` varchar(100) NOT NULL,
`credential_id` int NOT NULL,
CONSTRAINT `fk_credential` FOREIGN KEY (`credential_id`) REFERENCES `credential` (`id`) ON DELETE CASCADE
);
7 changes: 7 additions & 0 deletions src/main/resources/db/migration/V1_0_2__minimal_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DROP TABLE `service` CASCADE;

CREATE TABLE IF NOT EXISTS `service` (
`id` varchar(255) NOT NULL PRIMARY KEY,
`default_oidc_scope` varchar(255) NOT NULL,
`oidc_scopes` LONGTEXT NOT NULL
);

0 comments on commit f56c653

Please sign in to comment.