Skip to content

Commit

Permalink
Merge pull request #1689 from dilanSachi/fix-empty-header-name
Browse files Browse the repository at this point in the history
Add default values to record fields
  • Loading branch information
dilanSachi committed Apr 19, 2024
2 parents e90e8dc + 09144e7 commit bf2798b
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,21 @@ public void testDefaultRequired() throws IOException, BallerinaOpenApiException,
GeneratorTestUtils.assertGeneratedSyntaxTreeContainsExpectedSyntaxTree("schema/ballerina/" +
"default_required_field_schema.bal", syntaxTree);
}

@Test(description = "Test for default value without required fields")
public void testDefaultWithoutRequired() throws IOException, BallerinaOpenApiException, FormatterException {
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(RES_DIR.resolve("swagger" +
"/default_non_required_field_schema.yaml"), true);
TypeHandler.createInstance(openAPI, false);
ServiceGenerationHandler serviceGenerationHandler = new ServiceGenerationHandler();
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withNullable(false)
.withFilters(FILTER)
.build();
serviceGenerationHandler.generateServiceFiles(oasServiceMetadata);
syntaxTree = TypeHandler.getInstance().generateTypeSyntaxTree();
GeneratorTestUtils.assertGeneratedSyntaxTreeContainsExpectedSyntaxTree("schema/ballerina/" +
"default_non_required_field_schema.bal", syntaxTree);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public type Pet record {
1|2|3 id = 2;
"red"|"green"|"blue" color = "green";
string name = "Name";
int age = 1;
string 'type?;
boolean isOld = true;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: OpenApi Petstore
license:
name: MIT
servers:
- url: http://petstore.{host}.io/v1
description: The production API server
variables:
host:
default: openapi
description: this value is assigned by the service provider
- url: https://{subdomain}.swagger.io:{port}/{basePath}
description: The production API server
variables:
subdomain:
default: petstore
description: this value is assigned by the service provider
port:
enum:
- '8443'
- '443'
default: '443'
basePath:
default: v2
tags:
- name: pets
description: Pets Tag
- name: list
description: List Tag

paths:
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
components:
schemas:
Pet:
properties:
id:
type: integer
format: int64
enum:
- 1
- 2
- 3
default: 2
color:
type: string
enum:
- red
- green
- blue
default: green
name:
type: string
default: Name
age:
type: integer
default: 1
type:
type: string
isOld:
type: boolean
default: true
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,12 @@ public ImmutablePair<List<Node>, Set<String>> updateRecordFieldListWithImports(
Schema<?> fieldSchema, IdentifierToken fieldName, TypeDescriptorNode fieldTypeName) {
Set<String> imports = new HashSet<>();

if (required != null) {
if (required != null && required.contains(field.getKey().trim())) {
setRequiredFields(required, recordFieldList, field, fieldSchema, fieldName, fieldTypeName);
} else if (fieldSchema.getDefault() != null) {
RecordFieldWithDefaultValueNode recordFieldWithDefaultValueNode =
getRecordFieldWithDefaultValueNode(fieldSchema, fieldName, fieldTypeName);
recordFieldList.add(recordFieldWithDefaultValueNode);
} else {
RecordFieldNode recordFieldNode = NodeFactory.createRecordFieldNode(null, null,
fieldTypeName, fieldName, createToken(QUESTION_MARK_TOKEN), createToken(SEMICOLON_TOKEN));
Expand Down Expand Up @@ -311,13 +315,8 @@ private RecordFieldWithDefaultValueNode getRecordFieldWithDefaultValueNode(Schem
Token defaultValueToken;
String defaultValue = fieldSchema.getDefault().toString().trim();
if (GeneratorUtils.isStringSchema(fieldSchema)) {
if (defaultValue.equals("\"")) {
defaultValueToken = AbstractNodeFactory.createIdentifierToken("\"" + "\\" +
fieldSchema.getDefault().toString() + "\"");
} else {
defaultValueToken = AbstractNodeFactory.createIdentifierToken("\"" +
fieldSchema.getDefault().toString() + "\"");
}
defaultValue = "\"" + defaultValue.replaceAll("\"", "\\\\\"") + "\"";
defaultValueToken = AbstractNodeFactory.createIdentifierToken(defaultValue);
} else if (!defaultValue.matches("^[0-9]*$") && !defaultValue.matches("^(\\d*\\.)?\\d+$")
&& !(defaultValue.startsWith("[") && defaultValue.endsWith("]")) &&
!GeneratorUtils.isBooleanSchema(fieldSchema)) {
Expand Down

0 comments on commit bf2798b

Please sign in to comment.