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

[master]Fix for changing the OAS byte format from mapping byte[] to string in the Ballerina type #1566

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
org.gradle.caching=true
group=io.ballerina
version=1.8.1-SNAPSHOT
version=1.9.0-SNAPSHOT

#dependency
ballerinaLangVersion=2201.8.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
*Copyright (c) 2023,WSO2 LLC. (https://www.wso2.com)
*
* 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.generators.schema;

import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.openapi.core.GeneratorUtils;
import io.ballerina.openapi.core.exception.BallerinaOpenApiException;
import io.ballerina.openapi.core.generators.schema.BallerinaTypesGenerator;
import io.ballerina.openapi.generators.common.TestUtils;
import io.swagger.v3.oas.models.OpenAPI;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* This includes primitive data types format types tests.
*/
public class TypeFormatTests {
private static final Path RES_DIR = Paths.get("src/test/resources/generators/schema").toAbsolutePath();

@Test
public void stringFormats() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/format/string_formats.yaml");
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(definitionPath, true);
BallerinaTypesGenerator ballerinaSchemaGenerator = new BallerinaTypesGenerator(openAPI);
SyntaxTree syntaxTree = ballerinaSchemaGenerator.generateSyntaxTree();
TestUtils.compareGeneratedSyntaxTreewithExpectedSyntaxTree("schema/ballerina/format/string_formats.bal",
syntaxTree);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import ballerina/constraint;

public type StringObject record {
string name?;
byte[] byteContent?;
@constraint:String {pattern: re `^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$`}
string byteContent?;
record {byte[] fileContent; string fileName;} binaryContent?;
@constraint:String {pattern: re `^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$`}
string uuidContent?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# formats those are defined by the OpenAPI Specification
public type OASStringFormats record {
string name?;
string byteContent?;
record {byte[] fileContent; string fileName;} binaryContent?;
string dateContent?;
string passwordContent?;
string datetimeContent?;
};

# formats those are not defined by the OpenAPI Specification
public type NONOASStringFormats record {
string uuidContent?;
string uriContent?;
string emailContent?;
string hostnameContent?;
string ipv4Content?;
string ipv6Content?;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
openapi: 3.0.0
info:
title: Format REST API
version: 4.0.0
paths:
/projects:
get:
operationId: op1
responses:
'200':
description: Feature flag approval request response
content:
"application/json":
schema:
$ref: '#/components/schemas/OASStringFormats'
servers:
- url: https://app.launchdarkly.com/api/v2
components:
schemas:
OASStringFormats:
type: object
description: formats those are defined by the OpenAPI Specification
properties:
name:
type: string
byteContent:
type: string
format: byte
binaryContent:
type: string
format: binary
dateContent:
type: string
format: date
passwordContent:
type: string
format: password
datetimeContent:
type: string
format: date-time
NONOASStringFormats:
description: formats those are not defined by the OpenAPI Specification
type: object
properties:
uuidContent:
type: string
format: uuid
uriContent:
type: string
format: uri
emailContent:
type: string
format: email
hostnameContent:
type: string
format: hostname
ipv4Content:
type: string
format: ipv4
ipv6Content:
type: string
format: ipv6
1 change: 1 addition & 0 deletions openapi-cli/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ under the License.
<class name="io.ballerina.openapi.generators.schema.IntegerDataTypeTests"/>
<class name="io.ballerina.openapi.generators.schema.EnumGenerationTests"/>
<class name="io.ballerina.openapi.generators.schema.NegativeConstraintTests"/>
<class name="io.ballerina.openapi.generators.schema.TypeFormatTests"/>
<class name="io.ballerina.openapi.generators.testcases.BallerinaTestGeneratorTests"/>
<class name="io.ballerina.openapi.generators.client.OneOfResponsesTests"/>
<class name="io.ballerina.openapi.generators.openapi.DataTypeTests"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public String getValue() {
typeMap.put("double", "decimal");
typeMap.put("float", "float");
typeMap.put("binary", "byte[]");
typeMap.put("byte", "byte[]");
typeMap.put("byte", "string");
typeMap.put("int32", "int:Signed32");
typeMap.put("int64", "int");
OPENAPI_TYPE_TO_BAL_TYPE_MAP = Collections.unmodifiableMap(typeMap);
Expand Down
Loading