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

chore: rewrite java generator tests #804

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
exports[`Should be able to render Java Models and should log expected output to console 1`] = `
Array [
"public class Root {
private Object email;
private String email;

public Object getEmail() { return this.email; }
public void setEmail(Object email) { this.email = email; }
public String getEmail() { return this.email; }
public void setEmail(String email) { this.email = email; }
}",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
exports[`Should be able to generate a model to overwrite the Equal methods and should log expected output to console 1`] = `
Array [
"public class Root {
private Object email;
private String email;

public Object getEmail() { return this.email; }
public void setEmail(Object email) { this.email = email; }
public String getEmail() { return this.email; }
public void setEmail(String email) { this.email = email; }

@Override
public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
exports[`Should be able to generate a model to overwrite the hashCode method and should log expected output to console 1`] = `
Array [
"public class Root {
private Object email;
private String email;

public Object getEmail() { return this.email; }
public void setEmail(Object email) { this.email = email; }
public String getEmail() { return this.email; }
public void setEmail(String email) { this.email = email; }

@Override
public int hashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
exports[`Should be able to generate ts data model with marshal und unmarshal functions and should log expected output to console 1`] = `
Array [
"public class Root {
private Object email;
private String email;

public Object getEmail() { return this.email; }
public void setEmail(Object email) { this.email = email; }
public String getEmail() { return this.email; }
public void setEmail(String email) { this.email = email; }

public String marshal() {
List<String> propList = new ArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
exports[`Should be able to generate a model to overwrite the toString method and should log expected output to console 1`] = `
Array [
"public class Root {
private Object email;
private String email;

public Object getEmail() { return this.email; }
public void setEmail(Object email) { this.email = email; }
public String getEmail() { return this.email; }
public void setEmail(String email) { this.email = email; }

@Override
public String toString() {
Expand Down
7 changes: 3 additions & 4 deletions src/generators/java/JavaConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ export const JavaDefaultTypeMapping: TypeMapping<JavaOptions> = {
return 'Object';
},
Float ({constrainedModel}): string {
let type = 'float';
let type = 'Double';
const format = constrainedModel.originalInput && constrainedModel.originalInput['format'];
switch (format) {
case 'double':
case 'number':
type = 'double';
case 'float':
type = 'float';
break;
}
return type;
Expand Down
4 changes: 2 additions & 2 deletions src/generators/java/JavaPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { EnumRenderer, JAVA_DEFAULT_ENUM_PRESET } from './renderers/EnumRenderer
export type ClassPresetType<O> = ClassPreset<ClassRenderer, O>;
export type EnumPresetType<O> = EnumPreset<EnumRenderer, O>;

export type JavaPreset<O = JavaOptions> = Preset<{
export type JavaPreset<O = any> = Preset<{
class: ClassPresetType<O>;
enum: EnumPresetType<O>;
}>;

export const JAVA_DEFAULT_PRESET: JavaPreset = {
export const JAVA_DEFAULT_PRESET: JavaPreset<JavaOptions> = {
class: JAVA_DEFAULT_CLASS_PRESET,
enum: JAVA_DEFAULT_ENUM_PRESET,
};
5 changes: 4 additions & 1 deletion src/generators/java/constrainer/EnumConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export function defaultEnumKeyConstraints(customConstraints?: Partial<ModelEnumK
constrainedEnumKey = constraints.NO_NUMBER_START_CHAR(constrainedEnumKey);
constrainedEnumKey = constraints.NO_EMPTY_VALUE(constrainedEnumKey);
constrainedEnumKey = constraints.NO_RESERVED_KEYWORDS(constrainedEnumKey);
//If the enum key has been manipulated, lets make sure it don't clash with existing keys
if (constrainedEnumKey !== enumKey) {
constrainedEnumKey = constraints.NO_DUPLICATE_KEYS(constrainedEnumModel, enumModel, constrainedEnumKey, constraints.NAMING_FORMATTER!);
}
constrainedEnumKey = constraints.NAMING_FORMATTER(constrainedEnumKey);
constrainedEnumKey = constraints.NO_DUPLICATE_KEYS(constrainedEnumModel, enumModel, constrainedEnumKey, constraints.NAMING_FORMATTER!);
return constrainedEnumKey;
};
}
Expand Down
7 changes: 5 additions & 2 deletions src/generators/java/constrainer/PropertyKeyConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const DefaultPropertyKeyConstraints: PropertyKeyConstraintOptions = {
NO_NUMBER_START_CHAR,
NO_DUPLICATE_PROPERTIES,
NO_EMPTY_VALUE,
NAMING_FORMATTER: FormatHelpers.toPascalCase,
NAMING_FORMATTER: FormatHelpers.toCamelCase,
NO_RESERVED_KEYWORDS: (value: string) => {
return NO_RESERVED_KEYWORDS(value, isReservedJavaKeyword);
}
Expand All @@ -37,8 +37,11 @@ export function defaultPropertyKeyConstraints(customConstraints?: Partial<Proper
constrainedPropertyKey = constraints.NO_NUMBER_START_CHAR(constrainedPropertyKey);
constrainedPropertyKey = constraints.NO_EMPTY_VALUE(constrainedPropertyKey);
constrainedPropertyKey = constraints.NO_RESERVED_KEYWORDS(constrainedPropertyKey);
//If the property name has been manipulated, lets make sure it don't clash with existing properties
if (constrainedPropertyKey !== objectPropertyModel.propertyName) {
constrainedPropertyKey = constraints.NO_DUPLICATE_PROPERTIES(constrainedObjectModel, objectModel, constrainedPropertyKey, constraints.NAMING_FORMATTER);
}
constrainedPropertyKey = constraints.NAMING_FORMATTER(constrainedPropertyKey);
constrainedPropertyKey = constraints.NO_DUPLICATE_PROPERTIES(constrainedObjectModel, objectModel, constrainedPropertyKey, constraints.NAMING_FORMATTER);
return constrainedPropertyKey;
};
}
6 changes: 3 additions & 3 deletions src/generators/java/presets/ConstraintsPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { JavaPreset } from '../JavaPreset';
*
* @implements {JavaPreset}
*/
export const JAVA_CONSTRAINTS_PRESET: JavaPreset<any> = {
export const JAVA_CONSTRAINTS_PRESET: JavaPreset = {
class: {
self({renderer, content}) {
renderer.addDependency('import javax.validation.constraints.*;');
Expand All @@ -19,7 +19,7 @@ export const JAVA_CONSTRAINTS_PRESET: JavaPreset<any> = {
annotations.push(renderer.renderAnnotation('NotNull'));
}
const originalInput = property.property.originalInput;

// string
if (property.property instanceof ConstrainedStringModel) {
const pattern = originalInput['pattern'];
Expand All @@ -32,7 +32,7 @@ export const JAVA_CONSTRAINTS_PRESET: JavaPreset<any> = {
annotations.push(renderer.renderAnnotation('Size', { min: minLength, max: maxLength }));
}
}

// number/integer
if (property.property instanceof ConstrainedFloatModel ||
property.property instanceof ConstrainedIntegerModel) {
Expand Down
2 changes: 1 addition & 1 deletion src/generators/java/presets/JacksonPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const JAVA_JACKSON_PRESET: JavaPreset = {
const isDictionary = property.property instanceof ConstrainedDictionaryModel;
const hasUnwrappedOptions = isDictionary && (property.property as ConstrainedDictionaryModel).serializationType === 'unwrap';
if (!hasUnwrappedOptions) {
const annotation = renderer.renderAnnotation('JsonProperty', `"${property.propertyName}"`);
const annotation = renderer.renderAnnotation('JsonProperty', `"${property.unconstrainedPropertyName}"`);
return renderer.renderBlock([annotation, content]);
}
return renderer.renderBlock([content]);
Expand Down
6 changes: 3 additions & 3 deletions src/generators/java/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ export const JAVA_DEFAULT_CLASS_PRESET: ClassPresetType<JavaOptions> = {
return renderer.defaultSelf();
},
property({ property }) {
return `private ${property.property.type} ${property.property.name};`;
return `private ${property.property.type} ${property.propertyName};`;
},
getter({ property }) {
const getterName = `get${FormatHelpers.toPascalCase(property.propertyName)}`;
return `public ${property.property.type} ${getterName}() { return this.${property.property.name}; }`;
return `public ${property.property.type} ${getterName}() { return this.${property.propertyName}; }`;
},
setter({ property }) {
const setterName = FormatHelpers.toPascalCase(property.propertyName);
return `public void set${setterName}(${property.property.type} ${property.property.name}) { this.${property.property.name} = ${property.property.name}; }`;
return `public void set${setterName}(${property.property.type} ${property.propertyName}) { this.${property.propertyName} = ${property.propertyName}; }`;
}
};
Loading