Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ballerina to OAS] HATEOAS Support #1589

Merged
Merged
Show file tree
Hide file tree
Changes from 111 commits
Commits
Show all changes
117 commits
Select commit Hold shift + click to select a range
7d6dfca
Resolve conflicts
lnash94 Dec 14, 2023
0a94431
Initial commit
SachinAkash01 Jan 3, 2024
1e4b3e2
Fix checkstyle errors
SachinAkash01 Jan 3, 2024
804da50
Fix checkstyle errors
SachinAkash01 Jan 3, 2024
e0403dc
Fix checkstyle violations
ayeshLK Jan 3, 2024
5a48390
Implement hateoas meta-info sharing logic
ayeshLK Jan 4, 2024
df44930
Merge branch 'hypermedia_support' into bal-hateoas-to-oas
SachinAkash01 Jan 4, 2024
682aa67
Merge pull request #3 from ayeshLK/bal-hateoas-to-oas
SachinAkash01 Jan 4, 2024
e06bebb
Remove unwanted node-visitor
ayeshLK Jan 4, 2024
7f0688f
Update node-visiting logic to extract hateoas-metadata
ayeshLK Jan 4, 2024
80cb406
Update shared hateoas metadata updating logic
ayeshLK Jan 4, 2024
95b4d66
Mark hateoas resource as a record
ayeshLK Jan 4, 2024
f3a2fb5
Refactor common utility functions and their usage
ayeshLK Jan 4, 2024
527b921
Fix checkstyle violations
ayeshLK Jan 4, 2024
f22e634
Resolve merge conflicts
ayeshLK Jan 4, 2024
8e30086
Merge pull request #4 from ayeshLK/bal-hateoas-to-oas
SachinAkash01 Jan 4, 2024
96d2951
Add hateoas mapper
SachinAkash01 Jan 4, 2024
fbedc37
Implementing hateoas link object
SachinAkash01 Jan 4, 2024
a3a826d
Fix checkstyle violations
SachinAkash01 Jan 4, 2024
4dca3fe
Improve hateoas mapper
SachinAkash01 Jan 5, 2024
82c3cd7
Refactor HateoasLink to SwaggerLink mapping-logic
ayeshLK Jan 5, 2024
4dfc467
Refactor method name
ayeshLK Jan 5, 2024
63865b8
Merge pull request #6 from ayeshLK/bal-hateoas-to-oas-v2
SachinAkash01 Jan 5, 2024
4b57fec
Resolve merge conflicts in response mapper
SachinAkash01 Jan 5, 2024
affe803
Refactor common utilities
ayeshLK Jan 5, 2024
2594e99
Add logic to incorporate hateoas-metadata visitor execution
ayeshLK Jan 5, 2024
96bcb54
Merge pull request #7 from ayeshLK/bal-hateoas-to-oas-v2
SachinAkash01 Jan 5, 2024
36358f3
Add links to Api response
SachinAkash01 Jan 5, 2024
01f814e
Fix checkstyle violations
ayeshLK Jan 5, 2024
4a4ab14
Refactor shared-context usage logic
ayeshLK Jan 5, 2024
e00b24c
Remove unwanted quotations from hateoas resource-name
ayeshLK Jan 5, 2024
1294e6a
Fix swagger-links adding logic
ayeshLK Jan 5, 2024
484029e
Merge pull request #8 from ayeshLK/bal-hateoas-debug
SachinAkash01 Jan 5, 2024
466a545
Add swagger links to Api response
SachinAkash01 Jan 5, 2024
08f04e3
Improve setting swagger links in Api response
SachinAkash01 Jan 7, 2024
0ff1631
Add tests
SachinAkash01 Jan 7, 2024
576af53
Fix failing tests and checkstyle violations
SachinAkash01 Jan 8, 2024
f5fc72b
Add test cases
SachinAkash01 Jan 8, 2024
dae6198
Define the get type name interface
TharmiganK Jan 9, 2024
dfd8b90
Improve default self relation mapping
SachinAkash01 Jan 9, 2024
fe28582
Add snowpeak example to the tests
SachinAkash01 Jan 9, 2024
5ea199c
Revert "Define the get type name interface"
SachinAkash01 Jan 9, 2024
afc589d
Update license headers
SachinAkash01 Jan 9, 2024
a47f83b
Update license header
SachinAkash01 Jan 10, 2024
a625010
Merge branch 'ballerina-platform:restructure-bal-to-oas' into restruc…
SachinAkash01 Jan 11, 2024
96f2e15
Merge branch 'restructure-bal-to-oas' of https://github.com/ballerina…
SachinAkash01 Jan 11, 2024
e3e2682
Refactor naming conventions
ayeshLK Jan 12, 2024
1cd3a68
Merge pull request #9 from ayeshLK/bal-hateoas-to-oas
SachinAkash01 Jan 12, 2024
d997ab9
Add constraint support proposal to the docs
SachinAkash01 Jan 12, 2024
085502f
Merge branch 'ballerina-platform:restructure-bal-to-oas' into restruc…
SachinAkash01 Jan 15, 2024
091e9f8
Merge branch 'restructure-bal-to-oas' of https://github.com/ballerina…
SachinAkash01 Jan 15, 2024
7a77eeb
Merge branch 'restructure-bal-to-oas' of https://github.com/SachinAka…
SachinAkash01 Jan 15, 2024
89e36f0
Commit suggestions
SachinAkash01 Jan 16, 2024
fa22934
Merge pull request #1593 from SachinAkash01/constraint-support-docs
ayeshLK Jan 16, 2024
6954dd1
Resolve merge conflicts
SachinAkash01 Jan 16, 2024
4586bcc
Fix checkstyle errors
SachinAkash01 Jan 16, 2024
6e51648
Fix checkstyle violations
SachinAkash01 Jan 16, 2024
adbcf0b
Resolve checkstyle violations
SachinAkash01 Jan 16, 2024
78e40be
Merge branch 'ballerina-platform:master' into hypermedia_support
SachinAkash01 Jan 16, 2024
1384306
Merge branch 'bal-hateos-to-oas' of https://github.com/ballerina-plat…
SachinAkash01 Jan 16, 2024
f6e213a
Resolve merge conflicts
SachinAkash01 Jan 16, 2024
00d5d21
Delete docs/ballerina-to-oas/proposals/constraint-support.md
SachinAkash01 Jan 16, 2024
b34c246
Resolve checkstyle violations
SachinAkash01 Jan 16, 2024
5db940f
Merge remote-tracking branch 'origin/hypermedia_support' into hyperme…
SachinAkash01 Jan 16, 2024
99ffc68
Fix formats
SachinAkash01 Jan 17, 2024
b6fc12b
Refactor hypermedia support
SachinAkash01 Jan 17, 2024
b9c9f0f
Fix import issues
SachinAkash01 Jan 17, 2024
0ccc583
Merge pull request #10 from SachinAkash01/hypermedia_support_refactor
SachinAkash01 Jan 17, 2024
35157a0
Refactor the usage of semantic-model and package-id
ayeshLK Jan 17, 2024
fc07038
Merge pull request #11 from ayeshLK/bal-hateoas-to-oas-ref
SachinAkash01 Jan 17, 2024
cf982ff
Fix review suggestions
lnash94 Jan 17, 2024
a9eb963
Merge pull request #1587 from lnash94/master-528
lnash94 Jan 17, 2024
58ce513
Merge branch 'bal-hateos-to-oas' of https://github.com/ballerina-plat…
SachinAkash01 Jan 17, 2024
0420bd8
Resolve merge conflicts
SachinAkash01 Jan 17, 2024
fc71806
Merge pull request #12 from ballerina-platform/master
SachinAkash01 Jan 18, 2024
d8b8533
Revert "Merge pull request #12 from ballerina-platform/master"
SachinAkash01 Jan 18, 2024
2a7e86d
Merge branch 'bal-hateos-to-oas' of https://github.com/ballerina-plat…
SachinAkash01 Jan 18, 2024
e7a3e28
Update license header
SachinAkash01 Jan 18, 2024
af850bb
Update ballerina-to-openapi/src/main/java/io/ballerina/openapi/servic…
SachinAkash01 Jan 18, 2024
3c5e534
Fix failing tests
SachinAkash01 Jan 19, 2024
06ff16c
Merge branch 'bal-hateos-to-oas' of https://github.com/ballerina-plat…
SachinAkash01 Jan 19, 2024
dab9951
Remove Hateoas link mapping logic from response mapper
ayeshLK Jan 19, 2024
63ac241
Add new hateoas-mapper implementation
ayeshLK Jan 19, 2024
9b939c7
Add doc-comment to the hateoas-mapper interface
ayeshLK Jan 19, 2024
1ee2599
Refactor code-base
ayeshLK Jan 19, 2024
9f66453
Update doc-comments of the hateoas mapper
ayeshLK Jan 19, 2024
75a0cc3
Merge pull request #13 from ayeshLK/bal-hateoas-to-oas
SachinAkash01 Jan 19, 2024
dd17f5e
Remove unused function parameters
SachinAkash01 Jan 19, 2024
dae465e
Update license headers
SachinAkash01 Jan 22, 2024
7b2befa
Update license headers
SachinAkash01 Jan 22, 2024
4d2f941
Refactor code base
ayeshLK Jan 22, 2024
a9c7016
Update licensing headers and doc-comments
ayeshLK Jan 22, 2024
acfe97d
Merge branch 'hypermedia_support' into bal-hateoas-to-oas
ayeshLK Jan 22, 2024
e303056
Fix licensing headers
ayeshLK Jan 22, 2024
44cf354
Merge branch 'bal-hateoas-to-oas' of github.com:ayeshLK/ballerina-ope…
ayeshLK Jan 22, 2024
55dd203
Merge pull request #14 from ayeshLK/bal-hateoas-to-oas
SachinAkash01 Jan 22, 2024
814f2b9
Update Java doc
SachinAkash01 Jan 22, 2024
d7716d2
Update ballerina-to-openapi/src/main/java/io/ballerina/openapi/servic…
SachinAkash01 Jan 22, 2024
53d24da
Update ballerina-to-openapi/src/main/java/io/ballerina/openapi/servic…
SachinAkash01 Jan 22, 2024
6324c65
Resolve review suggestions
SachinAkash01 Jan 22, 2024
0de1125
Remove unused imports
SachinAkash01 Jan 22, 2024
1461a87
Refactor naming
SachinAkash01 Jan 22, 2024
d60e728
Update ballerina-to-openapi/src/main/java/io/ballerina/openapi/servic…
SachinAkash01 Jan 23, 2024
e458073
Refactor the variable names
SachinAkash01 Jan 23, 2024
dfa0545
Refactor the method for generating operationId
SachinAkash01 Jan 23, 2024
0a979f2
Fix checkstyle violations caused by line lengths
SachinAkash01 Jan 23, 2024
4ec4202
Remove hateoas meta-data extraction logic from single OAS generation …
ayeshLK Jan 24, 2024
e983dad
Merge pull request #15 from ayeshLK/bal-hateoas-to-oas-debug
SachinAkash01 Jan 24, 2024
2cf17b0
Refactor hateoas-metadata visitor logic
ayeshLK Jan 24, 2024
3dcba5e
Merge pull request #16 from ayeshLK/bal-hateoas-to-oas-debug
SachinAkash01 Jan 24, 2024
90eb48d
Remove the visitor usage
SachinAkash01 Jan 31, 2024
4c8bdc3
Refactor the code base
SachinAkash01 Jan 31, 2024
e78ea81
Update license header
SachinAkash01 Jan 31, 2024
58d80c2
Update ballerina-to-openapi/src/main/java/io/ballerina/openapi/servic…
SachinAkash01 Jan 31, 2024
58dc449
Update ballerina-to-openapi/src/main/java/io/ballerina/openapi/servic…
SachinAkash01 Jan 31, 2024
a2e46df
Update license headers
SachinAkash01 Jan 31, 2024
e75d546
Merge remote-tracking branch 'origin/hypermedia_support' into hyperme…
SachinAkash01 Jan 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,28 @@
}
case Constants.OPTIONS -> {
if (pathObject.containsKey(pathName)) {
pathObject.get(pathName).setOptions(operation);

Check warning on line 146 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java#L146

Added line #L146 was not covered by tests
} else {
pathItem.setOptions(operation);
path.addPathItem(pathName, pathItem);

Check warning on line 149 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java#L148-L149

Added lines #L148 - L149 were not covered by tests
}
}

Check warning on line 151 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java#L151

Added line #L151 was not covered by tests
case Constants.PATCH -> {
if (pathObject.containsKey(pathName)) {
pathObject.get(pathName).setPatch(operation);
} else {
pathItem.setPatch(operation);
path.addPathItem(pathName, pathItem);

Check warning on line 157 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java#L156-L157

Added lines #L156 - L157 were not covered by tests
}
}

Check warning on line 159 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java#L159

Added line #L159 was not covered by tests
case Constants.HEAD -> {
if (pathObject.containsKey(pathName)) {
pathObject.get(pathName).setHead(operation);
} else {
pathItem.setHead(operation);
path.addPathItem(pathName, pathItem);

Check warning on line 165 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java#L164-L165

Added lines #L164 - L165 were not covered by tests
}
}

Check warning on line 167 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ResourceMapperImpl.java#L167

Added line #L167 was not covered by tests
default -> { }
}
}
Expand All @@ -174,26 +174,19 @@
*
* @return Operation Adaptor object of given resource
*/
private Optional<OperationInventory> convertResourceToOperation(FunctionDefinitionNode resource, String httpMethod,
String generateRelativePath,
private Optional<OperationInventory> convertResourceToOperation(FunctionDefinitionNode resourceFunction,
String httpMethod, String generateRelativePath,
Components components) {
OperationInventory operationInventory = new OperationInventory();
operationInventory.setHttpOperation(httpMethod);
operationInventory.setPath(generateRelativePath);
/* Set operation id */
String resName = (resource.functionName().text() + "_" +
generateRelativePath).replaceAll("\\{///}", "_");

if (generateRelativePath.equals("/")) {
resName = resource.functionName().text();
}
operationInventory.setOperationId(getOperationId(resName));
operationInventory.setOperationId(getOperationId(resourceFunction));
// Set operation summary
// Map API documentation
Map<String, String> apiDocs = listAPIDocumentations(resource, operationInventory);
Map<String, String> apiDocs = listAPIDocumentations(resourceFunction, operationInventory);
//Add path parameters if in path and query parameters
ParameterMapper parameterMapper = new ParameterMapperImpl(resource, operationInventory, components, apiDocs,
additionalData, treatNilableAsOptional);
ParameterMapper parameterMapper = new ParameterMapperImpl(resourceFunction, operationInventory, components,
apiDocs, additionalData, treatNilableAsOptional);
parameterMapper.setParameters();
List<OpenAPIMapperDiagnostic> diagnostics = additionalData.diagnostics();
if (diagnostics.size() > 1 || (diagnostics.size() == 1 && !diagnostics.get(0).getCode().equals(
Expand All @@ -207,7 +200,7 @@
}
}

ResponseMapper responseMapper = new ResponseMapperImpl(resource, operationInventory, components,
ResponseMapper responseMapper = new ResponseMapperImpl(resourceFunction, operationInventory, components,
additionalData);
responseMapper.setApiResponses();
return Optional.of(operationInventory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import io.ballerina.openapi.service.mapper.diagnostic.DiagnosticMessages;
import io.ballerina.openapi.service.mapper.diagnostic.ExceptionDiagnostic;
import io.ballerina.openapi.service.mapper.diagnostic.OpenAPIMapperDiagnostic;
import io.ballerina.openapi.service.mapper.hateoas.HateoasMapper;
import io.ballerina.openapi.service.mapper.hateoas.HateoasMapperImpl;
import io.ballerina.openapi.service.mapper.hateoas.HateoasMetadataVisitor;
import io.ballerina.openapi.service.mapper.model.AdditionalData;
import io.ballerina.openapi.service.mapper.model.ModuleMemberVisitor;
import io.ballerina.openapi.service.mapper.model.OASGenerationMetaInfo;
Expand Down Expand Up @@ -62,7 +65,7 @@
*
* @since 2.0.0
*/
public class ServiceToOpenAPIMapper {

Check warning on line 68 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ServiceToOpenAPIMapper.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ServiceToOpenAPIMapper.java#L68

Added line #L68 was not covered by tests

/**
* This method will generate openapi definition Map lists with ballerina code.
Expand All @@ -83,11 +86,11 @@
List<OpenAPIMapperDiagnostic> diagnostics = new ArrayList<>();
List<OASResult> outputs = new ArrayList<>();
if (containErrors(semanticModel.diagnostics())) {
DiagnosticMessages messages = DiagnosticMessages.OAS_CONVERTOR_106;
ExceptionDiagnostic error = new ExceptionDiagnostic(messages.getCode(), messages.getDescription(),

Check warning on line 90 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ServiceToOpenAPIMapper.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ServiceToOpenAPIMapper.java#L89-L90

Added lines #L89 - L90 were not covered by tests
null);
diagnostics.add(error);
} else {

Check warning on line 93 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ServiceToOpenAPIMapper.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ServiceToOpenAPIMapper.java#L92-L93

Added lines #L92 - L93 were not covered by tests
ModulePartNode modulePartNode = syntaxTree.rootNode();
extractServiceNodes(serviceName, availableService, servicesToGenerate, modulePartNode, semanticModel);
// If there are no services found for a given mapper name.
Expand All @@ -97,6 +100,8 @@
null, serviceName, availableService.toString());
diagnostics.add(error);
}
// Extract HATEOAS meta-data for the all the service-declarations in the current project
extractHateoasLinkMetadata(project);
// Generating openapi specification for selected services
for (Map.Entry<String, ServiceDeclarationNode> serviceNode : servicesToGenerate.entrySet()) {
String openApiName = getOpenApiFileName(syntaxTree.filePath(), serviceNode.getKey(), needJson);
Expand Down Expand Up @@ -206,12 +211,14 @@
ConstraintMapper constraintMapper = new ConstraintMapperImpl(openapi, moduleMemberVisitor,
diagnostics);
constraintMapper.setConstraints();
HateoasMapper hateoasMapper = new HateoasMapperImpl();
hateoasMapper.setOpenApiLinks(serviceDefinition, openapi);
return new OASResult(openapi, diagnostics);
} else {
return new OASResult(openapi, oasResult.getDiagnostics());

Check warning on line 218 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ServiceToOpenAPIMapper.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ServiceToOpenAPIMapper.java#L218

Added line #L218 was not covered by tests
}
} else {
return oasResult;

Check warning on line 221 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ServiceToOpenAPIMapper.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/ServiceToOpenAPIMapper.java#L221

Added line #L221 was not covered by tests
}
}

Expand Down Expand Up @@ -266,4 +273,17 @@
}
return true;
}

private static void extractHateoasLinkMetadata(Project project) {
String packageId = project.currentPackage().packageId().id().toString();
project.currentPackage().moduleIds().forEach(moduleId -> {
Module module = project.currentPackage().module(moduleId);
SemanticModel semanticModel = project.currentPackage().getCompilation().getSemanticModel(moduleId);
HateoasMetadataVisitor hateoasMetadataVisitor = new HateoasMetadataVisitor(packageId, semanticModel);
ayeshLK marked this conversation as resolved.
Show resolved Hide resolved
module.documentIds().forEach(documentId -> {
SyntaxTree syntaxTreeDoc = module.document(documentId).syntaxTree();
syntaxTreeDoc.rootNode().accept(hateoasMetadataVisitor);
});
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.ballerina.openapi.service.mapper.hateoas;

public class Constants {

Check warning on line 3 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/hateoas/Constants.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/hateoas/Constants.java#L3

Added line #L3 was not covered by tests

public static final String OPENAPI_LINK_DEFAULT_REL = "_self";
public static final String BALLERINA_LINKEDTO_KEYWORD = "linkedTo";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.openapi.service.mapper.hateoas;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* A context-holder to share HATEOAS meta-data information.
*
* @since 1.9.0
*/
public final class HateoasContextHolder {
private static HateoasContextHolder instance;

private final List<Service> hateoasServices;

public HateoasContextHolder() {
this.hateoasServices = new ArrayList<>();
}

public static HateoasContextHolder getHateoasContextHolder() {
synchronized (HateoasContextHolder.class) {
if (Objects.isNull(instance)) {
instance = new HateoasContextHolder();
}
}
return instance;
}

public void updateHateoasResource(String packageId, int serviceId, String resourceName, Resource resource) {
Optional<Service> hateoasService = this.hateoasServices.stream()
.filter(svc -> packageId.equals(svc.getPackageId()) && svc.getServiceId() == serviceId)
.findFirst();
if (hateoasService.isEmpty()) {
Service service = new Service(packageId, serviceId);
service.addResource(resourceName, resource);
this.hateoasServices.add(service);
return;
}
Service service = hateoasService.get();
service.addResource(resourceName, resource);
}

public Optional<Resource> getHateoasResource(String packageId, int serviceId, String resourceName,
String resourceMethod) {
return this.hateoasServices.stream()

Check warning on line 65 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/hateoas/HateoasContextHolder.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/hateoas/HateoasContextHolder.java#L65

Added line #L65 was not covered by tests
.filter(svc -> svc.getPackageId().equals(packageId) && svc.getServiceId() == serviceId)
.findFirst()
.flatMap(svc -> svc.getHateoasResourceMapping().entrySet().stream()
.filter(resources -> resourceName.equals(resources.getKey()))
.findFirst()
).flatMap(hateoasResourceMapping -> hateoasResourceMapping.getValue().stream()
.filter(resource -> resourceMethod.equals(resource.resourceMethod()))
.findFirst()

Check warning on line 73 in ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/hateoas/HateoasContextHolder.java

View check run for this annotation

Codecov / codecov/patch

ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/hateoas/HateoasContextHolder.java#L67-L73

Added lines #L67 - L73 were not covered by tests
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.openapi.service.mapper.hateoas;

public class HateoasLink {
private String resourceName;
private String relation;
private String resourceMethod;

public String getResourceName() {
return resourceName;
}

public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}

public String getRel() {
return relation;
}

public void setRel(String relation) {
this.relation = relation;
}

public String getResourceMethod() {
return resourceMethod;
}

public void setResourceMethod(String resourceMethod) {
this.resourceMethod = resourceMethod;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.openapi.service.mapper.hateoas;

import io.ballerina.compiler.syntax.tree.ServiceDeclarationNode;
import io.swagger.v3.oas.models.OpenAPI;

/**
* This {@link HateoasMapper} uses to set HATEOAS links into the OpenAPI context.
*
* @since 1.9.0
*/
public interface HateoasMapper {

/**
* Sets HATEOAS links into the OpenAPI context.
*
* @param serviceNode Specific service declaration node
* @param openAPI Current OpenAPI context
*/
void setOpenApiLinks(ServiceDeclarationNode serviceNode, OpenAPI openAPI);
}
Loading
Loading