Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
added more test openapi beans, reduced repeating code and changed nes…
Browse files Browse the repository at this point in the history
…ted enums to take on the name of the parent object

Signed-off-by: Holly Hunt <holly.hunt1101@hotmail.com>
  • Loading branch information
Wyvinar committed Apr 16, 2024
1 parent 6d4e582 commit 0c303cc
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 39 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ galasabuild-image/galasabld-linux-amd64
openapi2beans/build

**/.vscode
**/generated/*
**/target
*.jar
93 changes: 93 additions & 0 deletions openapi2beans/JavaChecker/src/main/resources/test-reference.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ components:
description: an enum with 2 values to test against.
enum: [string1, string2]

BeanWithIntegerEnumProperty:
type: object
description: bean with an integer enum property
properties:
anEnumProperty:
type: integer
description: an enum with 2 values to test against.
enum: [1, 2, 3, 4]

BeanWithConstantProperty:
type: object
description: bean with a constant property
Expand All @@ -137,6 +146,90 @@ components:
aStringVariable:
type: string

BeanWithMultipleRequiredPrimitiveProperties:
type: object
required:
- aStringVariable
- anIntVariable
- aBooleanVariable
- aNumberVariable
properties:
aStringVariable:
type: string
anIntVariable:
type: integer
aBooleanVariable:
type: boolean
aNumberVariable:
type: number

BeanWithMixOfRequiredPrimitiveProperties:
type: object
required:
- aStringVariable
- anIntVariable
properties:
aStringVariable:
type: string
anIntVariable:
type: integer
aBooleanVariable:
type: boolean
aNumberVariable:
type: number

BeanWithMultipleRequiredArrayProperties:
type: object
required:
- aStringVariable
- anIntVariable
- aBooleanVariable
- aNumberVariable
properties:
aStringArray:
items:
type: string
anIntArray:
items:
type: integer
aBooleanArray:
items:
type: boolean
aNumberArray:
items:
type: number

BeanWithMixOfRequiredArrayProperties:
type: object
required:
- aStringVariable
- anIntVariable
properties:
aStringArray:
type: array
items:
type: string
anIntArray:
type: array
items:
type: integer
aBooleanArray:
type: array
items:
type: boolean
aNumberArray:
type: array
items:
type: number

BeanWithRequiredEnumProperty:
type: object
required: [aStringVariable]
properties:
aStringEnum:
type: string
enum: ["smthn", "or", "other"]

BeanWithRequiredReferencedObject:
type: object
description: A bean with a required property referencing another object
Expand Down
24 changes: 12 additions & 12 deletions openapi2beans/pkg/generator/yaml2java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ func assertVariableSetCorrectly(t *testing.T, generatedFile string, description
}
}

func assertVariableMatchesGetter(t *testing.T, generatedFile string, name string, camelName string, javaExpectedVar string) {
func assertVariableMatchesGetter(t *testing.T, generatedFile string, name string, camelName string, javaExpectedVarType string) {
getterLiteral := ` public %s get%s() {
return this.%s;
}`
getter := fmt.Sprintf(getterLiteral, javaExpectedVar, camelName, name)
getter := fmt.Sprintf(getterLiteral, javaExpectedVarType, camelName, name)
assert.Contains(t, generatedFile, getter)
}

func assertVariableMatchesSetter(t *testing.T, generatedFile string, name string, camelName string, javaExpectedVar string) {
func assertVariableMatchesSetter(t *testing.T, generatedFile string, name string, camelName string, javaExpectedVarType string) {
setterLiteral := ` public void set%s(%s %s) {
this.%s = %s;
}`
setter := fmt.Sprintf(setterLiteral, camelName, javaExpectedVar, name, name, name)
setter := fmt.Sprintf(setterLiteral, camelName, javaExpectedVarType, name, name, name)
assert.Contains(t, generatedFile, setter)
}

Expand Down Expand Up @@ -644,10 +644,10 @@ components:
assert.Nil(t, err)
generatedClassFile := openGeneratedFile(t, mockFileSystem, generatedCodeFilePath)
assertClassFileGeneratedOk(t, generatedClassFile, objectName)
assertVariableMatchesGetter(t, generatedClassFile, "myEnum", "MyEnum", "MyEnum")
assertVariableMatchesSetter(t, generatedClassFile, "myEnum", "MyEnum", "MyEnum")
assertVariableSetCorrectly(t, generatedClassFile, []string{}, "myEnum", "MyEnum")
assert.Contains(t, generatedClassFile, ` public MyBeanName (MyEnum myEnum) {
assertVariableMatchesGetter(t, generatedClassFile, "myEnum", "MyEnum", "MyBeanNameMyEnum")
assertVariableMatchesSetter(t, generatedClassFile, "myEnum", "MyEnum", "MyBeanNameMyEnum")
assertVariableSetCorrectly(t, generatedClassFile, []string{}, "myEnum", "MyBeanNameMyEnum")
assert.Contains(t, generatedClassFile, ` public MyBeanName (MyBeanNameMyEnum myEnum) {
this.myEnum = myEnum;
}`)
generatedEnumFile := openGeneratedFile(t, mockFileSystem, getGeneratedCodeFilePathWithPackage(storeFilepath, packageName, "MyEnum"))
Expand Down Expand Up @@ -683,10 +683,10 @@ components:
assert.Nil(t, err)
generatedClassFile := openGeneratedFile(t, mockFileSystem, generatedCodeFilePath)
assertClassFileGeneratedOk(t, generatedClassFile, objectName)
assertVariableMatchesGetter(t, generatedClassFile, "myEnum", "MyEnum", "MyEnum")
assertVariableMatchesSetter(t, generatedClassFile, "myEnum", "MyEnum", "MyEnum")
assertVariableSetCorrectly(t, generatedClassFile, []string{}, "myEnum", "MyEnum")
assert.NotContains(t, generatedClassFile, ` public MyBeanName (MyEnum myEnum) {
assertVariableMatchesGetter(t, generatedClassFile, "myEnum", "MyEnum", "MyBeanNameMyEnum")
assertVariableMatchesSetter(t, generatedClassFile, "myEnum", "MyEnum", "MyBeanNameMyEnum")
assertVariableSetCorrectly(t, generatedClassFile, []string{}, "myEnum", "MyBeanNameMyEnum")
assert.NotContains(t, generatedClassFile, ` public MyBeanName (MyBeanNameMyEnum myEnum) {
this.myEnum = myEnum;
}`)
generatedEnumFile := openGeneratedFile(t, mockFileSystem, getGeneratedCodeFilePathWithPackage(storeFilepath, packageName, "MyEnum"))
Expand Down
32 changes: 8 additions & 24 deletions openapi2beans/pkg/generator/yaml2schematypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package generator

import (
"fmt"
"log"
"strings"

Expand Down Expand Up @@ -101,15 +102,18 @@ func retrieveSchemaComponentsFromMap(
assignPropertyToSchemaType(parentPath, apiSchemaPartPath, property, schemaTypes)

var schemaType *SchemaType
if typeName == "object" {

if typeName == "object" || property.IsEnum() {
if parentPath != OPENAPI_YAML_SCHEMAS_PATH {
varName = resolveNestedObjectName(varName, parentPath)
}
schemaType = assignSchemaTypeToSchemaTypesMap(subMap, apiSchemaPartPath, varName, description, property, schemaTypes, properties, errMap)
} else if property.IsEnum() {
schemaType = NewSchemaType(convertToPascalCase(varName), description, property, nil)
property.SetResolvedType(schemaType)
schemaTypes[apiSchemaPartPath] = schemaType
if typeName == "object" {
retrieveNestedProperties(subMap, apiSchemaPartPath, schemaTypes, properties, errMap)
resolvePropertiesMinCardinalities(subMap, schemaType.properties, apiSchemaPartPath)
}
}

properties[apiSchemaPartPath] = property
Expand Down Expand Up @@ -224,33 +228,13 @@ func retrievePossibleValues(varMap map[string]interface{}) (possibleValues map[s
if isEnumPresent {
enums := enumObj.([]interface{})
for _, enum := range enums {
enumName := enum.(string)
enumName := fmt.Sprintf("%v", enum)
possibleValues[enumName] = enumName
}
}
return possibleValues
}

func assignSchemaTypeToSchemaTypesMap(
schemaTypeMap map[string]interface{},
apiSchemaPartPath string,
varName string,
description string,
ownProperty *Property,
schemaTypes map[string]*SchemaType,
properties map[string]*Property,
errMap map[string]error) *SchemaType {

resolvedType := NewSchemaType(convertToPascalCase(varName), description, ownProperty, nil)

ownProperty.SetResolvedType(resolvedType)
schemaTypes[apiSchemaPartPath] = resolvedType

retrieveNestedProperties(schemaTypeMap, apiSchemaPartPath, schemaTypes, properties, errMap)
resolvePropertiesMinCardinalities(schemaTypeMap, resolvedType.properties, apiSchemaPartPath)
return resolvedType
}

func assignPropertyToSchemaType(parentPath string, apiSchemaPartPath string, property *Property, schemaTypes map[string]*SchemaType) {
schemaType, isPropertyPartOfSchemaType := schemaTypes[parentPath]
if isPropertyPartOfSchemaType {
Expand Down
4 changes: 2 additions & 2 deletions openapi2beans/pkg/generator/yaml2schematypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ components:
property1, propertyExists := schemaType.GetProperties()[propertyPath]
assert.True(t, propertyExists)
assert.Equal(t, true, property1.IsEnum())
assert.Equal(t, "MyEnum", property1.resolvedType.name)
assert.Equal(t, "MyBeanNameMyEnum", property1.resolvedType.name)
assert.Equal(t, "string", property1.typeName)
posValue1, posValueExists := property1.GetPossibleValues()["randValue1"]
assert.True(t, posValueExists)
Expand All @@ -766,7 +766,7 @@ components:
assert.Equal(t, "randValue2", posValue2)
enumSchemaType, enumSchemaTypeExists := schemaTypes[propertyPath]
assert.Equal(t, true, enumSchemaTypeExists)
assert.Equal(t, "MyEnum", enumSchemaType.name)
assert.Equal(t, "MyBeanNameMyEnum", enumSchemaType.name)
assert.Equal(t, "MyEnum", enumSchemaType.ownProperty.name)
}

Expand Down

0 comments on commit 0c303cc

Please sign in to comment.