Skip to content

Commit

Permalink
Introduce removeAnnotations lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
jpraet committed Dec 6, 2024
1 parent f6b2b5f commit e632bd0
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
@Getter @Setter
public String datatypeWithEnum;
@Getter @Setter
public String rawDatatypeWithEnum;
@Getter @Setter
public String dataFormat;
/**
* The name of this property in the OpenAPI schema.
Expand Down Expand Up @@ -974,7 +972,6 @@ public String toString() {
sb.append(", description='").append(description).append('\'');
sb.append(", dataType='").append(dataType).append('\'');
sb.append(", datatypeWithEnum='").append(datatypeWithEnum).append('\'');
sb.append(", rawDatatypeWithEnum='").append(rawDatatypeWithEnum).append('\'');
sb.append(", dataFormat='").append(dataFormat).append('\'');
sb.append(", name='").append(name).append('\'');
sb.append(", min='").append(min).append('\'');
Expand Down Expand Up @@ -1164,7 +1161,6 @@ public boolean equals(Object o) {
Objects.equals(description, that.description) &&
Objects.equals(dataType, that.dataType) &&
Objects.equals(datatypeWithEnum, that.datatypeWithEnum) &&
Objects.equals(rawDatatypeWithEnum, that.rawDatatypeWithEnum) &&
Objects.equals(dataFormat, that.dataFormat) &&
Objects.equals(name, that.name) &&
Objects.equals(min, that.min) &&
Expand Down Expand Up @@ -1208,7 +1204,7 @@ public boolean equals(Object o) {
public int hashCode() {

return Objects.hash(openApiType, baseName, complexType, getter, setter, description,
dataType, datatypeWithEnum, rawDatatypeWithEnum, dataFormat, name, min, max, defaultValue,
dataType, datatypeWithEnum, dataFormat, name, min, max, defaultValue,
defaultValueWithParam, baseType, containerType, containerTypeMapped, title, unescapedDescription,
maxLength, minLength, pattern, example, jsonSchema, minimum, maximum,
exclusiveMinimum, exclusiveMaximum, required, deprecated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4176,8 +4176,6 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
property.defaultValue = toDefaultValue(property, p);
property.defaultValueWithParam = toDefaultValueWithParam(name, p);

property.rawDatatypeWithEnum = property.datatypeWithEnum;

LOGGER.debug("debugging from property return: {}", property);
schemaCodegenPropertyCache.put(ns, property);
return property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,15 @@ public void processOpts() {
convertPropertyToBooleanAndWriteBack(CONTAINER_DEFAULT_TO_NULL, this::setContainerDefaultToNull);

additionalProperties.put("sanitizeGeneric", (Mustache.Lambda) (fragment, writer) -> {
String content = removeBeanValidationAnnotations(fragment.execute());
String content = removeAnnotations(fragment.execute());
for (final String s: List.of("<", ">", ",", " ")) {
content = content.replace(s, "");
}
writer.write(content);
});
additionalProperties.put("removeAnnotations", (Mustache.Lambda) (fragment, writer) -> {
writer.write(removeAnnotations(fragment.execute()));
});
}

/**
Expand Down Expand Up @@ -1794,12 +1797,6 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
if (property.dataType != null && property.dataType.equals(property.name) && property.dataType.toUpperCase(Locale.ROOT).equals(property.name)) {
property.name = property.name.toLowerCase(Locale.ROOT);
}

if (useBeanValidation) {
property.rawDatatypeWithEnum = removeBeanValidationAnnotations(property.datatypeWithEnum);
} else {
property.rawDatatypeWithEnum = property.datatypeWithEnum;
}
}

@Override
Expand All @@ -1809,11 +1806,11 @@ public void postProcessResponseWithProperty(CodegenResponse response, CodegenPro
}

// the response data types should not contain bean validation annotations.
property.dataType = removeBeanValidationAnnotations(property.dataType);
response.dataType = removeBeanValidationAnnotations(response.dataType);
property.dataType = removeAnnotations(property.dataType);
response.dataType = removeAnnotations(response.dataType);
}

private String removeBeanValidationAnnotations(String dataType) {
private String removeAnnotations(String dataType) {
if (dataType != null && dataType.contains("@")) {
return dataType.replaceAll("(?:(?i)@[a-z0-9]*+([(].*[)]|\\s*))*+", "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,6 @@ public ModelsMap postProcessModels(ModelsMap objs) {
if (Boolean.TRUE.equals(var.getVendorExtensions().get("x-enum-as-string"))) {
// treat enum string as just string
var.datatypeWithEnum = var.dataType;
var.rawDatatypeWithEnum = var.dataType;

if (StringUtils.isNotEmpty(var.defaultValue)) { // has default value
String defaultValue = var.defaultValue.substring(var.defaultValue.lastIndexOf('.') + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openapitools.codegen.languages;

import static org.openapitools.codegen.CodegenConstants.*;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -30,11 +32,6 @@
import java.util.Set;
import java.util.TreeSet;

import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.servers.Server;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
Expand All @@ -57,7 +54,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.openapitools.codegen.CodegenConstants.SERIALIZATION_LIBRARY;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.servers.Server;
import lombok.Getter;
import lombok.Setter;

public class JavaHelidonClientCodegen extends JavaHelidonCommonCodegen {

Expand Down Expand Up @@ -424,7 +425,6 @@ public ModelsMap postProcessModels(ModelsMap objs) {
if (Boolean.TRUE.equals(var.getVendorExtensions().get("x-enum-as-string"))) {
// treat enum string as just string
var.datatypeWithEnum = var.dataType;
var.rawDatatypeWithEnum = var.datatypeWithEnum;

if (StringUtils.isNotEmpty(var.defaultValue)) { // has default value
String defaultValue = var.defaultValue.substring(var.defaultValue.lastIndexOf('.') + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ public static class Builder {{#parentModel}}extends {{classname}}.Builder {{/par
}

{{#vars}}
public {{classname}}.Builder {{name}}({{{rawDatatypeWithEnum}}} {{name}}) {
public {{classname}}.Builder {{name}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{name}}) {
{{#vendorExtensions.x-is-jackson-optional-nullable}}
this.instance.{{name}} = JsonNullable.<{{{rawDatatypeWithEnum}}}>of({{name}});
this.instance.{{name}} = JsonNullable.<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}>of({{name}});
{{/vendorExtensions.x-is-jackson-optional-nullable}}
{{^vendorExtensions.x-is-jackson-optional-nullable}}
this.instance.{{name}} = {{name}};
{{/vendorExtensions.x-is-jackson-optional-nullable}}
return this;
}
{{#vendorExtensions.x-is-jackson-optional-nullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{{rawDatatypeWithEnum}}}> {{name}}) {
public {{classname}}.Builder {{name}}(JsonNullable<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}> {{name}}) {
this.instance.{{name}} = {{name}};
return this;
}
{{/vendorExtensions.x-is-jackson-optional-nullable}}
{{/vars}}

{{#parentVars}}
public {{classname}}.Builder {{name}}({{{rawDatatypeWithEnum}}} {{name}}) { // inherited: {{isInherited}}
public {{classname}}.Builder {{name}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{name}}) { // inherited: {{isInherited}}
super.{{name}}({{name}});
return this;
}
{{#vendorExtensions.x-is-jackson-optional-nullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{{rawDatatypeWithEnum}}}> {{name}}) {
public {{classname}}.Builder {{name}}(JsonNullable<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}> {{name}}) {
this.instance.{{name}} = {{name}};
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens

public static abstract class {{classname}}Builder<C extends {{classname}}, B extends {{classname}}Builder<C, B>> {{#parent}}extends {{{.}}}Builder<C, B>{{/parent}} {
{{#vars}}
private {{{rawDatatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
private {{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
{{/vars}}
{{^parent}}
protected abstract B self();
Expand All @@ -212,7 +212,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
{{/parent}}

{{#vars}}
public B {{name}}({{{rawDatatypeWithEnum}}} {{name}}) {
public B {{name}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{name}}) {
this.{{name}} = {{name}};
return self();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@
{{#deprecated}}
@Deprecated
{{/deprecated}}
public {{classname}}.Builder {{name}}({{{rawDatatypeWithEnum}}} {{name}}) {
public {{classname}}.Builder {{name}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{name}}) {
this.instance.{{name}}({{name}});
return this;
}
{{#openApiNullable}}{{#isNullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{{rawDatatypeWithEnum}}}> {{name}}) {
public {{classname}}.Builder {{name}}(JsonNullable<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}> {{name}}) {
this.instance.{{name}} = {{name}};
return this;
}
{{/isNullable}}{{/openApiNullable}}
{{/vars}}
{{#parentVars}}
@Override
public {{classname}}.Builder {{name}}({{{rawDatatypeWithEnum}}} {{name}}) {
public {{classname}}.Builder {{name}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{name}}) {
this.instance.{{name}}({{name}});
return this;
}
{{#openApiNullable}}{{#isNullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{{rawDatatypeWithEnum}}}> {{name}}) {
public {{classname}}.Builder {{name}}(JsonNullable<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}> {{name}}) {
this.instance.{{setter}}({{name}});
return this;
}
Expand Down
Loading

0 comments on commit e632bd0

Please sign in to comment.