Skip to content

Commit

Permalink
Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
SachinAkash01 committed Jan 16, 2024
1 parent 1384306 commit f6e213a
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class ResourceMapper implements ResourceMapperInterface {
private final Paths pathObject = new Paths();
private final AdditionalData additionalData;
private final OpenAPI openAPI;
private final SemanticModel semanticModel;
private final List<FunctionDefinitionNode> resources;
private final boolean treatNilableAsOptional;
private final HateoasMapper hateoasMapper;
Expand All @@ -74,6 +75,7 @@ public class ResourceMapper implements ResourceMapperInterface {
ResourceMapper(OpenAPI openAPI, List<FunctionDefinitionNode> resources, SemanticModel semanticModel,
AdditionalData additionalData, boolean treatNilableAsOptional) {
this.openAPI = openAPI;
this.semanticModel = semanticModel;
this.resources = resources;
this.additionalData = additionalData;
this.treatNilableAsOptional = treatNilableAsOptional;
Expand Down Expand Up @@ -109,8 +111,8 @@ private void generateResourceMapping(SemanticModel semanticModel, FunctionDefini
resource.location());
additionalData.diagnostics().add(error);
} else {
Optional<OperationBuilder> operationBuilder = convertResourceToOperation(resource, httpMethod, cleanResourcePath,
components);
Optional<OperationBuilder> operationBuilder = convertResourceToOperation(resource, httpMethod,
cleanResourcePath, components, service);
if (operationBuilder.isPresent()) {
Operation operation = operationBuilder.get().getOperation();
generatePathItem(httpMethod, pathObject, operation, cleanResourcePath);
Expand Down Expand Up @@ -187,7 +189,8 @@ private void generatePathItem(String httpMethod, Paths path, Operation operation
* @return Operation Adaptor object of given resource
*/
private Optional<OperationBuilder> convertResourceToOperation(FunctionDefinitionNode resource, String httpMethod,
String generateRelativePath, Components components) {
String generateRelativePath, Components components,
ServiceDeclarationNode service) {
OperationBuilder operationBuilder = new OperationBuilder();
operationBuilder.setHttpOperation(httpMethod);
operationBuilder.setPath(generateRelativePath);
Expand Down Expand Up @@ -220,6 +223,7 @@ private Optional<OperationBuilder> convertResourceToOperation(FunctionDefinition

ResponseMapperInterface responseMapper = new ResponseMapper(resource, operationBuilder, components,
additionalData);
setSwaggerLinksInApiResponse(semanticModel, service, responseMapper.getApiResponses(), resource);
responseMapper.setApiResponses();
return Optional.of(operationBuilder);
}
Expand Down Expand Up @@ -265,24 +269,4 @@ private Map<String, String> listAPIDocumentations(FunctionDefinitionNode resourc
}
return apiDocs;
}

private String generateRelativePath(FunctionDefinitionNode resource) {

StringBuilder relativePath = new StringBuilder();
relativePath.append("/");
if (!resource.relativeResourcePath().isEmpty()) {
for (Node node: resource.relativeResourcePath()) {
if (node instanceof ResourcePathParameterNode pathNode) {
relativePath.append("{");
relativePath.append(pathNode.paramName().get());
relativePath.append("}");
} else if ((resource.relativeResourcePath().size() == 1) && (node.toString().trim().equals("."))) {
return relativePath.toString();
} else {
relativePath.append(node.toString().trim());
}
}
}
return relativePath.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/
package io.ballerina.openapi.service.mapper;

import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.syntax.tree.ServiceDeclarationNode;

public interface ResourceMapperInterface {

void addMapping();
void addMapping(SemanticModel semanticModel, ServiceDeclarationNode serviceNode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private static void convertServiceToOpenAPI(ServiceDeclarationNode serviceNode,
}
}
AdditionalData additionalData = new AdditionalData(semanticModel, moduleMemberVisitor, diagnostics);
ResourceMapperInterface resourceMapper = new ResourceMapper(openAPI, resources, additionalData,
ResourceMapperInterface resourceMapper = new ResourceMapper(openAPI, resources, semanticModel, additionalData,
isTreatNilableAsOptionalParameter(serviceNode));
resourceMapper.addMapping(semanticModel, serviceNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode;
import io.ballerina.openapi.service.mapper.hateoas.Resource;
import io.ballerina.openapi.service.mapper.parameter.model.HateoasLink;
import io.ballerina.openapi.service.mapper.response.model.HateoasLink;
import io.swagger.v3.oas.models.links.Link;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public void setApiResponses() {
operationBuilder.setApiResponses(apiResponses);
}

public ApiResponses getApiResponses() {
return apiResponses;
}

private TypeSymbol getReturnTypeSymbol(FunctionDefinitionNode resourceNode) {
Optional<Symbol> symbol = semanticModel.symbol(resourceNode);
if (symbol.isEmpty() || !(symbol.get() instanceof ResourceMethodSymbol resourceMethodSymbol)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/
package io.ballerina.openapi.service.mapper.response;

import io.swagger.v3.oas.models.responses.ApiResponses;

public interface ResponseMapperInterface {

void setApiResponses();
ApiResponses getApiResponses();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 Inc. 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.response.model;

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
Expand Up @@ -70,6 +70,7 @@ public static List<OpenAPIMapperDiagnostic> compareWithGeneratedFile(OASContract
ballerinaFilePath, tempDir, null, false);
if (Files.exists(tempDir.resolve("payloadV_openapi.yaml"))) {
String generatedYaml = getStringFromGivenBalFile(tempDir, "payloadV_openapi.yaml");
System.out.println(generatedYaml);
generatedYaml = (generatedYaml.trim()).replaceAll("\\s+", "");
expectedYamlContent = (expectedYamlContent.trim()).replaceAll("\\s+", "");
Assert.assertTrue(generatedYaml.contains(expectedYamlContent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ components:
type: string
method:
type: string
Location:
required:
- address
- id
- name
type: object
properties:
name:
type: string
id:
type: string
address:
type: string
Room:
required:
- category
Expand Down Expand Up @@ -195,15 +208,3 @@ components:
count:
type: integer
format: int64
Location:
required:
- address
- id
- name
type: object
properties:
name:
type: string
id:
type: string
address:

0 comments on commit f6e213a

Please sign in to comment.