Skip to content

Commit

Permalink
Merge pull request ballerina-platform#6 from thushalya/master
Browse files Browse the repository at this point in the history
Add changes to asyncapi-to-ballerina after changing into RC-3 version
  • Loading branch information
thushalya authored May 6, 2023
2 parents 8d88455 + 24cde90 commit 76a7f5e
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ components:
format: float
item:
type: object
additionalProperties: {}
additionalProperties: true
mapArray:
type: object
additionalProperties:
Expand All @@ -104,7 +104,7 @@ components:
type: object
additionalProperties:
type: object
additionalProperties: {}
additionalProperties: true
arrayItemMap:
type: array
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,26 @@ channels:
publish:
message:
$ref: '#/components/messages/Subscribe'
components:
schemas:
Subscribe:
type: object
required:
- id
- type1
properties:
id:
type: integer
format: int64
type1:
type: string
const: Subscribe
messages:
Subscribe:
payload:
$ref: '#/components/schemas/Subscribe'
x-response:
payload:
type: integer
format: int64
x-response-type: simple-rpc
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,27 @@ channels:
format: int64
publish:
message:
$ref: '#/components/messages/Subscribe'
$ref: '#/components/messages/Subscribe'
components:
schemas:
Subscribe:
type: object
required:
- id
- type1
properties:
id:
type: integer
format: int64
type1:
type: string
const: Subscribe
messages:
Subscribe:
payload:
$ref: '#/components/schemas/Subscribe'
x-response:
payload:
type: integer
format: int64
x-response-type: simple-rpc
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,26 @@ channels:
publish:
message:
$ref: '#/components/messages/Subscribe'

components:
schemas:
Subscribe:
type: object
required:
- id
- type1
properties:
id:
type: integer
format: int64
type1:
type: string
const: Subscribe
messages:
Subscribe:
payload:
$ref: '#/components/schemas/Subscribe'
x-response:
payload:
type: integer
format: int64
x-response-type: simple-rpc
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ public String toString() {
public static final String NO_TYPE_IN_STREAM = "ERROR: No type present in stream";
public static final String NO_SERVICE_CLASS = "ERROR: No service class present";

public static final String UNION_STREAMING_SIMPLE_RPC_ERROR = "Response server streaming types cannot be union with simple rpc types";
public static final String PATH_PARAM_DASH_CONTAIN_ERROR = "Path parameter contains an invalid character '-'";
public static final String UNION_STREAMING_SIMPLE_RPC_ERROR = "ERROR: Response server streaming types cannot be " +
"union with simple rpc types";
public static final String PATH_PARAM_DASH_CONTAIN_ERROR = "ERROR: Path parameter contains an invalid" +
" character '-'";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.ballerina.asyncapi.core.generators.asyncspec.model;


import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* In Apicurio data model they are using JsonNodes for bindings
* therefore we have to convert Java object to a Jackson ObjectNode we have to use new ObjectMapper().
Expand All @@ -14,8 +16,16 @@

public class BalAsyncApi25SchemaImpl extends io.apicurio.datamodels.models.asyncapi.v25.AsyncApi25SchemaImpl {

@JsonIgnore
@Override
public boolean isEntity() {
return false;
return true;
}

@JsonIgnore
@Override
public boolean isSchema() {
return true;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.ballerina.asyncapi.core.generators.asyncspec.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.apicurio.datamodels.models.union.BooleanUnionValueImpl;

/**
* In Apicurio data model ,they are using BooleanSchemaUnion but If use this as they given then we can't get the output.
*
*
* @since 2.0.0
*/
public class BalBooleanSchema extends BooleanUnionValueImpl {

public BalBooleanSchema(Boolean value) {
super(value);
}

@JsonIgnore
@Override
public boolean isBoolean() {
return true;
}

@JsonIgnore
@Override
public Boolean getValue() {
return true;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package io.ballerina.asyncapi.core.generators.asyncspec.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.TextNode;
import io.apicurio.datamodels.models.Schema;
Expand All @@ -28,6 +27,7 @@
import io.ballerina.asyncapi.core.generators.asyncspec.diagnostic.DiagnosticMessages;
import io.ballerina.asyncapi.core.generators.asyncspec.diagnostic.IncompatibleRemoteDiagnostic;
import io.ballerina.asyncapi.core.generators.asyncspec.model.BalAsyncApi25SchemaImpl;
import io.ballerina.asyncapi.core.generators.asyncspec.model.BalBooleanSchema;
import io.ballerina.asyncapi.core.generators.asyncspec.utils.ConverterCommonUtils;
import io.ballerina.compiler.api.symbols.ArrayTypeSymbol;
import io.ballerina.compiler.api.symbols.ConstantSymbol;
Expand Down Expand Up @@ -67,6 +67,7 @@
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.FLOAT;
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.INTEGER;
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.NUMBER;
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.OBJECT;
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.SCHEMA_REFERENCE;
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.STRING;
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.TRUE;
Expand Down Expand Up @@ -133,6 +134,18 @@ public void createComponentSchema(TypeSymbol typeSymbol, String dispatcherValue)
// Handle typeInclusions with allOf type binding
handleRecordTypeSymbol((RecordTypeSymbol) type, componentName, apiDocs, dispatcherValue);
break;

case TYPE_REFERENCE:
schema.setType(OBJECT);
schema.set$ref(ConverterCommonUtils.unescapeIdentifier(
type.getName().orElseThrow().trim()));
// schema.put(componentName, new ObjectSchema().$ref(ConverterCommonUtils.unescapeIdentifier(
// type.getName().orElseThrow().trim())));
components.addSchema(componentName, schema);
// components.setSchemas(schema);
TypeReferenceTypeSymbol referredType = (TypeReferenceTypeSymbol) type;
createComponentSchema(referredType, dispatcherValue);
break;
case STRING:
schema.setType(STRING);
schema.setDescription(typeDoc);
Expand Down Expand Up @@ -169,11 +182,28 @@ public void createComponentSchema(TypeSymbol typeSymbol, String dispatcherValue)
break;
case MAP:
MapTypeSymbol mapTypeSymbol = (MapTypeSymbol) type;
TypeDescKind typeDescKind = mapTypeSymbol.typeParam().typeKind();
BalAsyncApi25SchemaImpl asyncApiSchema = getAsyncApiSchema(typeDescKind.getName());
//TODO : have to check here asyncApiSchema.getType() == null ? true : asyncApiSchema
// BalAsyncApi25SchemaImpl objectSchema= new BalAsyncApi25SchemaImpl();
// objectSchema.setType(AsyncAPIType.RECORD.toString());
TypeSymbol typeParam = mapTypeSymbol.typeParam();
if (typeParam.typeKind() == TypeDescKind.TYPE_REFERENCE) {
TypeReferenceTypeSymbol typeReferenceTypeSymbol = (TypeReferenceTypeSymbol) typeParam;
BalAsyncApi25SchemaImpl objectSchema = new BalAsyncApi25SchemaImpl();
objectSchema.setType(AsyncAPIType.OBJECT.toString());
BalAsyncApi25SchemaImpl objectSchema2 = new BalAsyncApi25SchemaImpl();
objectSchema2.setType(AsyncAPIType.OBJECT.toString());
objectSchema2.set$ref(ConverterCommonUtils.unescapeIdentifier(
typeReferenceTypeSymbol.getName().orElseThrow().trim()));
objectSchema.setAdditionalProperties(objectSchema2);
components.addSchema(componentName, objectSchema);
// schema.put(componentName, new ObjectSchema().additionalProperties(new ObjectSchema()
// .$ref(ConverterCommonUtils.unescapeIdentifier(
// typeReferenceTypeSymbol.getName().orElseThrow().trim()))));
createComponentSchema(typeReferenceTypeSymbol, dispatcherValue);
} else {

TypeDescKind typeDescKind = mapTypeSymbol.typeParam().typeKind();
BalAsyncApi25SchemaImpl asyncApiSchema = getAsyncApiSchema(typeDescKind.getName());
//TODO : have to check here asyncApiSchema.getType() == null ? true : asyncApiSchema
BalAsyncApi25SchemaImpl objectSchema = new BalAsyncApi25SchemaImpl();
objectSchema.setType(AsyncAPIType.OBJECT.toString());
// schema.put(componentName,
// new ObjectSchema().additionalProperties(
// asyncApiSchema.getType() == null ? true : asyncApiSchema)
Expand All @@ -182,11 +212,15 @@ public void createComponentSchema(TypeSymbol typeSymbol, String dispatcherValue)
// if (schemas != null) {
// schemas.putAll(schema);
// } else {
schema.setType(AsyncAPIType.OBJECT.toString());
schema.setDescription(typeDoc);
schema.setAdditionalProperties(asyncApiSchema);
components.addSchema(componentName, schema);
// schema.setType(AsyncAPIType.OBJECT.toString());
// schema.setDescription(typeDoc);
// schema.setAdditionalProperties(asyncApiSchema.getType() == null ?
// new BooleanUnionValueImpl(true) : asyncApiSchema);
objectSchema.setAdditionalProperties(asyncApiSchema.getType() == null ?
new BalBooleanSchema(true) : asyncApiSchema);
components.addSchema(componentName, objectSchema);
// }
}
break;
default:
// Diagnostic for currently unsupported data types.
Expand Down Expand Up @@ -334,7 +368,7 @@ public BalAsyncApi25SchemaImpl generateObjectSchemaFromRecordFields(String compo
// String check=property.getType();
if (property.getType() != null) {
if (property.getType().equals(AsyncAPIType.ARRAY.toString()) && !((property).getItems() != null &&
(property).getItems().has("oneOf"))) {
((BalAsyncApi25SchemaImpl) (property).getItems().asSchema()).getOneOf() != null)) {
BooleanNode booleanNode = null;
if (property.getExtensions() != null) {
booleanNode = (BooleanNode) (property.getExtensions().get(X_NULLABLE));
Expand Down Expand Up @@ -405,7 +439,8 @@ private BalAsyncApi25SchemaImpl handleMapType(String componentName, BalAsyncApi2
// objectSchema.setType(AsyncAPIType.RECORD.toString());
//TODO : Have to consider about asyncApiSchema.getType() == null ? true :
// asyncApiSchema in addtionalProperties
property.setAdditionalProperties(asyncApiSchema);
property.setAdditionalProperties(asyncApiSchema.getType() == null ?
new BalBooleanSchema(true) : asyncApiSchema);
}
return property;
}
Expand Down Expand Up @@ -590,7 +625,7 @@ private BalAsyncApi25SchemaImpl mapArrayToArraySchema(TypeSymbol symbol,
arraySchema.setType(AsyncAPIType.ARRAY.toString());
property.setItems(handleArray(arrayDimensions - 1, symbolProperty, arraySchema));
} else {
property.setItems(ConverterCommonUtils.callObjectMapper().valueToTree(symbolProperty));
property.setItems(symbolProperty);
}
return property;
}
Expand Down Expand Up @@ -648,18 +683,18 @@ private BalAsyncApi25SchemaImpl getSchemaForTypeReferenceSymbol(TypeSymbol array
* Handle nested array.
*/
//TODO : Here needs to check objectMapper.valueToTree(property) because it may contatins entity:true
private JsonNode handleArray(int arrayDimensions, Schema property, BalAsyncApi25SchemaImpl arrayProperty) {
ObjectMapper objectMapper = ConverterCommonUtils.callObjectMapper();
private BalAsyncApi25SchemaImpl handleArray(int arrayDimensions, BalAsyncApi25SchemaImpl property,
BalAsyncApi25SchemaImpl arrayProperty) {

if (arrayDimensions > 1) {
BalAsyncApi25SchemaImpl nArray = new BalAsyncApi25SchemaImpl();
nArray.setType(AsyncAPIType.ARRAY.toString());
arrayProperty.setItems(handleArray(arrayDimensions - 1, property, nArray));
} else if (arrayDimensions == 1) {

arrayProperty.setItems(objectMapper.valueToTree(property));
arrayProperty.setItems(property);
}
return objectMapper.valueToTree(arrayProperty);
return arrayProperty;
}

public TypeSymbol excludeReadonlyIfPresent(TypeSymbol typeSymbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.ATTR_HOST;
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.FALSE;
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.LOCALHOST;
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.PORT;
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.SECURE_SOCKET;
import static io.ballerina.asyncapi.core.generators.asyncspec.Constants.SERVER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
*/
package io.ballerina.asyncapi.core.generators.asyncspec.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import io.ballerina.asyncapi.core.generators.asyncspec.Constants.AsyncAPIType;
import io.ballerina.asyncapi.core.generators.asyncspec.model.BalAsyncApi25SchemaImpl;
Expand Down Expand Up @@ -173,9 +171,9 @@ private void completeHeaderParameter(String headerName, BalAsyncApi25SchemaImpl
if (headerSchema.getDefault() != null) {
arraySchema.setDefault(headerSchema.getDefault());
}
ObjectMapper objectMapper = ConverterCommonUtils.callObjectMapper();
ObjectNode obj = objectMapper.valueToTree(itemSchema);
arraySchema.setItems(obj);
// ObjectMapper objectMapper = ConverterCommonUtils.callObjectMapper();
// ObjectNode obj = objectMapper.valueToTree(itemSchema);
arraySchema.setItems(itemSchema);
bindingHeaderObject.addProperty(headerName, arraySchema);
} else {
bindingHeaderObject.addProperty(headerName, headerSchema);
Expand Down
Loading

0 comments on commit 76a7f5e

Please sign in to comment.