Skip to content

Commit

Permalink
Merge pull request #1693 from ballerina-platform/status-code-binding
Browse files Browse the repository at this point in the history
Fix minor issues found in RC testing
  • Loading branch information
lnash94 committed Apr 24, 2024
2 parents f304d88 + b32c1ac commit c1e20e7
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 25 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ apply plugin: "org.javamodularity.moduleplugin"
apply from: "$rootDir/gradle/javaProject.gradle"

ext.clientNativeVersion = project.clientNativeVersion
ext.clientNativePublish = project.clientNativePublish
ext.ballerinaLangVersion = project.ballerinaLangVersion
ext.testngVersion = project.testngVersion
ext.commonsLang3Version = project.commonsLang3Version
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ version=1.9.0-SNAPSHOT

# Client Native Version
clientNativeVersion=1.0.0-SNAPSHOT
# Mark this as false to skip publishing the client native artifacts
clientNativePublish=true

#dependency
ballerinaLangVersion=2201.9.0-20240419-152500-bd530dd2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.ballerina.openapi.bal.tool.Constants.DiagnosticMessages;
import io.ballerina.openapi.core.generators.client.BallerinaClientGenerator;
import io.ballerina.openapi.core.generators.client.BallerinaClientGeneratorWithStatusCodeBinding;
import io.ballerina.openapi.core.generators.client.diagnostic.ClientDiagnostic;
import io.ballerina.openapi.core.generators.client.exception.ClientException;
import io.ballerina.openapi.core.generators.client.model.OASClientConfig;
import io.ballerina.openapi.core.generators.common.TypeHandler;
Expand All @@ -46,6 +47,7 @@
import io.ballerina.toml.validator.SampleNodeGenerator;
import io.ballerina.tools.diagnostics.DiagnosticFactory;
import io.ballerina.tools.diagnostics.DiagnosticInfo;
import io.ballerina.tools.diagnostics.DiagnosticSeverity;
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.tools.text.TextDocument;
import io.ballerina.tools.text.TextDocuments;
Expand Down Expand Up @@ -461,6 +463,19 @@ private static List<GenSrcFile> generateClientFiles(OASClientConfig oasClientCon
String licenseContent = oasClientConfig.getLicense();
BallerinaClientGenerator ballerinaClientGenerator = getClientGenerator(oasClientConfig, statusCodeBinding);
String mainContent = Formatter.format(ballerinaClientGenerator.generateSyntaxTree()).toString();

List<ClientDiagnostic> clientDiagnostic = ballerinaClientGenerator.getDiagnostics();

for (ClientDiagnostic diagnostic : clientDiagnostic) {
createDiagnostics(toolContext, diagnostic.getMessage(), diagnostic.getCode(),
diagnostic.getDiagnosticSeverity(), location);
}

if (clientDiagnostic.stream().anyMatch(
diagnostic -> diagnostic.getDiagnosticSeverity() == DiagnosticSeverity.ERROR)) {
throw new ClientException("Error occurred while generating client");
}

sourceFiles.add(new GenSrcFile(GenSrcFile.GenFileType.GEN_SRC, null, CLIENT_FILE_NAME,
licenseContent == null || licenseContent.isBlank() ? mainContent :
licenseContent + System.lineSeparator() + mainContent));
Expand Down Expand Up @@ -570,6 +585,12 @@ private static void createDiagnostics(ToolContext toolContext, DiagnosticMessage
toolContext.reportDiagnostic(DiagnosticFactory.createDiagnostic(diagnosticInfo, location));
}

private static void createDiagnostics(ToolContext toolContext, String diagnosticMessage, String diagnosticCode,
DiagnosticSeverity severity, Location location) {
DiagnosticInfo diagnosticInfo = new DiagnosticInfo(diagnosticCode, diagnosticMessage, severity);
toolContext.reportDiagnostic(DiagnosticFactory.createDiagnostic(diagnosticInfo, location));
}

/**
* This method uses to write the generated sources into the given output path.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,16 @@ public void perform(SyntaxNodeAnalysisContext context) {
.anyMatch(d -> DiagnosticSeverity.ERROR.equals(d.diagnosticInfo().severity()));

// if there are any compilation errors, do not proceed
if (!isErrorPrinted && hasErrors) {
setIsWarningPrinted();
PrintStream outStream = System.out;
outStream.println("openapi contract generation is skipped because of the following compilation " +
"error(s) in the ballerina package:");
return;
}

if (hasErrors) {

if (!isErrorPrinted) {
setIsWarningPrinted();
PrintStream outStream = System.out;
outStream.println("openapi contract generation is skipped because of the following compilation " +
"error(s) in the ballerina package:");
}
return;
}

Path outPath = project.targetDir();
Optional<Path> path = currentPackage.project().documentPath(context.documentId());
Path inputPath = path.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,8 @@ public void generateClientAndService(String definitionPath, String serviceName,
//display diagnostics- client
List<ClientDiagnostic> clientDiagnostic = clientGenerator.getDiagnostics();

if (!clientDiagnostic.isEmpty()) {
for (ClientDiagnostic diagnostic : clientDiagnostic) {
outStream.println(diagnostic.getDiagnosticSeverity() + ":" + diagnostic.getMessage());
}
for (ClientDiagnostic diagnostic : clientDiagnostic) {
outStream.println(diagnostic.getDiagnosticSeverity() + ":" + diagnostic.getMessage());
}
printDiagnostic(diagnostics);
writeGeneratedSources(newGenFiles, srcPath, implPath, GEN_BOTH);
Expand Down Expand Up @@ -434,10 +432,8 @@ private List<GenSrcFile> generateClientFiles(Path openAPI, Filter filter, boolea
}

List<ClientDiagnostic> clientDiagnostic = clientGenerator.getDiagnostics();
if (!clientDiagnostic.isEmpty()) {
for (ClientDiagnostic diagnostic : clientDiagnostic) {
outStream.println(diagnostic.getDiagnosticSeverity() + ":" + diagnostic.getMessage());
}
for (ClientDiagnostic diagnostic : clientDiagnostic) {
outStream.println(diagnostic.getDiagnosticSeverity() + ":" + diagnostic.getMessage());
}
printDiagnostic(diagnosticList);
return sourceFiles;
Expand Down
3 changes: 2 additions & 1 deletion openapi-client-native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ publishing {
tasks.withType(PublishToMavenRepository) {
onlyIf {
(repository == publishing.repositories.GitHub && publication == publishing.publications.GitHub) ||
(repository == publishing.repositories.WSO2Nexus && publication == publishing.publications.WSO2Nexus)
(repository == publishing.repositories.WSO2Nexus && publication == publishing.publications.WSO2Nexus
&& (clientNativeVersion.endsWith('-SNAPSHOT') || clientNativePublish))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -995,24 +995,26 @@ public static Schema getHeadersTypeSchema(ApiResponse apiResponse) {
}

Map<String, Schema> properties = new HashMap<>();
List<String> requiredField = new ArrayList<>();
List<String> requiredFields = new ArrayList<>();
for (Map.Entry<String, Header> headerEntry : headers.entrySet()) {
String headerName = headerEntry.getKey();
Header header = headerEntry.getValue();
Schema headerTypeSchema = getValidatedHeaderSchema(header.getSchema());
properties.put(headerName, headerTypeSchema);
if (header.getRequired() != null && header.getRequired()) {
requiredField.add(headerName);
requiredFields.add(headerName);
}
}

if (properties.isEmpty() || requiredField.isEmpty()) {
if (properties.isEmpty()) {
return null;
}

ObjectSchema headersSchema = new ObjectSchema();
headersSchema.setProperties(properties);
headersSchema.setRequired(requiredField);
if (!requiredFields.isEmpty()) {
headersSchema.setRequired(requiredFields);
}
headersSchema.setAdditionalProperties(false);
return headersSchema;
}
Expand Down Expand Up @@ -1087,7 +1089,9 @@ public static TypeDescriptorNode generateStatusCodeTypeInclusionRecord(String co
generatedTypes.put(mediaTypeToken.toString(), mediaTypeToken);
}
} else {
diagnosticList.add(new CommonDiagnostic(OAS_COMMON_101));
if (!bodyTypeSchema.isEmpty()) {
diagnosticList.add(new CommonDiagnostic(OAS_COMMON_101));
}
return TypeHandler.getInstance().createTypeInclusionRecord(code, null,
TypeHandler.getInstance().generateHeaderType(headersTypeSchema), method);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
public enum CommonDiagnosticMessages {
OAS_COMMON_101("OAS_COMMON_101", "Responses with 204 status code cannot have a body.",
DiagnosticSeverity.WARNING),
OAS_COMMON_102("OAS_COMMON_102", "Responses with 204 cannot have a body.",
DiagnosticSeverity.WARNING),
OAS_COMMON_201("OAS_COMMON_201", "Invalid reference value : %s\nBallerina " +
"only supports local reference values.", DiagnosticSeverity.ERROR),
OAS_COMMON_202("OAS_COMMON_202", "Unsupported OAS data type `%s`", DiagnosticSeverity.ERROR),
Expand Down

0 comments on commit c1e20e7

Please sign in to comment.