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

Added support deprecated extension for enum values. #1781

Open
wants to merge 1 commit into
base: 6.12.x
Choose a base branch
from
Open
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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.openapi.generator;

/**
* Schema type utils and constants.
*
* @since 6.12.4
*/
public final class MnSchemaTypeUtil {

public static final String TYPE_CHAR = "char";
public static final String TYPE_CHARACTER = "character";
public static final String TYPE_BYTE = "byte";
public static final String TYPE_SHORT = "short";
public static final String TYPE_INT = "int";
public static final String TYPE_LONG = "long";
public static final String TYPE_FLOAT = "float";
public static final String TYPE_DOUBLE = "double";

public static final String FORMAT_INT8 = "int8";
public static final String FORMAT_INT16 = "int16";
public static final String FORMAT_SHORT = "short";

private MnSchemaTypeUtil() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,16 @@ private static String genericAnnotations(CodegenProperty prop, boolean isGenerat
result.append(") ");
}
}
if (prop.isNullable) {
if (isGenerateHardNullable) {
result.append("@Nullable(inherited = true) ");
} else {
result.append("@Nullable ");
if (!(Boolean) prop.vendorExtensions.get("isPrimitive")) {
if (prop.isNullable) {
if (isGenerateHardNullable) {
result.append("@Nullable(inherited = true) ");
} else {
result.append("@Nullable ");
}
} else if (!containsNotEmpty) {
result.append("@NotNull ");
}
} else if (!containsNotEmpty) {
result.append("@NotNull ");
}
if (StringUtils.isNotEmpty(prop.minimum)) {
try {
Expand Down Expand Up @@ -270,9 +272,8 @@ private static boolean isPrimitive(String type) {
return false;
}
return switch (type) {
case "array", "string", "boolean", "byte", "uri", "url", "uuid", "email", "integer", "long", "float",
"double",
"number", "partial-time", "date", "date-time", "bigdecimal", "biginteger" -> true;
case "array", "char", "character", "string", "boolean", "byte", "short", "int", "integer", "long", "uri", "url", "uuid", "email", "float",
"double", "number", "partial-time", "date", "date-time", "bigdecimal", "biginteger" -> true;
default -> false;
};
}
Expand All @@ -297,6 +298,12 @@ public static void addStrValueToEnum(List<Object> enumVars, boolean isNumeric) {
for (var enumVar : enumVars) {
var varMap = (Map<String, Object>) enumVar;
var value = varMap.get("value").toString();
if (value.startsWith("(short)")) {
value = value.replace("(short) ", "");
} else if (value.startsWith("(byte)")) {
value = value.replace("(byte) ", "");
}
value = value.replace("'", "");
if (isNumeric) {
var argPos = value.indexOf('(');
// case for BigDecimal
Expand All @@ -309,9 +316,9 @@ public static void addStrValueToEnum(List<Object> enumVars, boolean isNumeric) {
|| upperValue.endsWith("D")) {
value = value.substring(0, value.length() - 1);
}
if (!value.contains("\"")) {
value = "\"" + value + "\"";
}
}
if (!value.contains("\"")) {
value = "\"" + value + "\"";
}
varMap.put("strValue", value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,7 @@ public String toAllOfName(List<String> names, Schema composedSchema) {
if (exts != null && exts.containsKey("x-all-of-name")) {
return (String) exts.get("x-all-of-name");
}
if (names.size() == 0) {
if (names.isEmpty()) {
LOGGER.error("allOf has no member defined: {}. Default to ERROR_ALLOF_SCHEMA", composedSchema);
return "ERROR_ALLOF_SCHEMA";
} else if (names.size() == 1) {
Expand Down Expand Up @@ -5290,7 +5290,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
parameterModelName = getParameterDataType(parameter, parameterSchema);
CodegenProperty prop;
if (this instanceof RustServerCodegen) {
// for rust server, we need to do somethings special as it uses
// for rust server, we need to do something special as it uses
// $ref (e.g. #components/schemas/Pet) to determine whether it's a model
prop = fromProperty(parameter.getName(), parameterSchema, false);
} else if (getUseInlineModelResolver()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ More information can be found inside [Inversion of Control guide section](https:
{{#allParams}}{{#-last}}### Parameters
| Name | Type | Description | Notes |
|------------- | ------------- | ------------- | -------------|
{{#allParams}}| **{{paramName}}** | {{#isPrimitiveType}}`{{dataType}}`{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}`{{dataType}}`{{/isFile}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} |{{^required}} [optional parameter]{{/required}}{{#defaultValue}} [default to `{{defaultValue}}`]{{/defaultValue}}{{#allowableValues}} [enum: {{#values}}`{{{.}}}`{{^-last}}, {{/-last}}{{/values}}]{{/allowableValues}} |
{{#allParams}}| **{{paramName}}** | {{#isPrimitiveType}}`{{vendorExtensions.baseType}}`{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}`{{dataType}}`{{/isFile}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} |{{^required}} [optional parameter]{{/required}}{{#defaultValue}} [default to `{{defaultValue}}`]{{/defaultValue}}{{#allowableValues}} [enum: {{#values}}`{{{.}}}`{{^-last}}, {{/-last}}{{/values}}]{{/allowableValues}} |
{{/allParams}}{{/-last}}{{/allParams}}

{{#returnType}}### Return type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{^isDate}}{{^isDateTime}}{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{/isDateTime}}{{/isDate}}
{{#isDateTime}}{{#vendorExtensions.formatPattern}}@Format("{{{vendorExtensions.formatPattern}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateTimeFormat}}@Format("{{{dateTimeFormat}}}"){{/dateTimeFormat}}{{/vendorExtensions.formatPattern}} {{{dataType}}} {{/isDateTime}}
{{#isDate}}{{#vendorExtensions.formatPattern}}@Format("{{{vendorExtensions.formatPattern}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateFormat}}@Format("{{{dateFormat}}}"){{/dateFormat}}{{/vendorExtensions.formatPattern}} {{{dataType}}} {{/isDate}}
{{#isDateTime}}{{#vendorExtensions.formatPattern}}@Format("{{{vendorExtensions.formatPattern}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateTimeFormat}}@Format("{{{dateTimeFormat}}}"){{/dateTimeFormat}}{{/vendorExtensions.formatPattern}} {{{vendorExtensions.baseType}}} {{/isDateTime}}
{{#isDate}}{{#vendorExtensions.formatPattern}}@Format("{{{vendorExtensions.formatPattern}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateFormat}}@Format("{{{dateFormat}}}"){{/dateFormat}}{{/vendorExtensions.formatPattern}} {{{vendorExtensions.baseType}}} {{/isDate}}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class {{classname}}Spec extends Specification {
void '{{operationId}}() test'() {
given:
{{#allParams}}
{{{dataType}}} {{paramName}} = {{{vendorExtensions.groovyExample}}}
{{{vendorExtensions.baseType}}} {{paramName}} = {{{vendorExtensions.groovyExample}}}
{{/allParams}}

when:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class {{classname}}Test {
void {{operationId}}Test() {
// given
{{#allParams}}
{{{dataType}}} {{paramName}} = {{{example}}};
{{{vendorExtensions.baseType}}} {{paramName}} = {{{example}}};
{{/allParams}}

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
{{#withXml}}
@XmlType(name="{{datatypeWithEnum}}")
@XmlEnum({{dataType}}.class)
@XmlEnum({{vendorExtensions.baseType}}.class)
{{/withXml}}
{{#lombok}}
@RequiredArgsConstructor
Expand All @@ -30,6 +30,9 @@
{{#withXml}}
@XmlEnumValue({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
{{/withXml}}
{{#deprecated}}
@Deprecated
{{/deprecated}}
@JsonProperty({{{strValue}}})
{{{name}}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
Expand All @@ -38,10 +41,10 @@
{{#formatSingleLine}}public final static Map<{{{dataType}}}, {{>common/model/enumName}}> VALUE_MAPPING = Map.copyOf(Arrays.stream(values()){{/formatSingleLine}}
.collect(Collectors.toMap(v -> v.value{{#isString}}{{#useEnumCaseInsensitive}}.toLowerCase(){{/useEnumCaseInsensitive}}{{/isString}}, Function.identity())));

private final {{{dataType}}} value;
private final {{{vendorExtensions.baseType}}} value;
{{^lombok}}

{{#formatSingleLine}}{{>common/model/enumName}}{{/formatSingleLine}}({{{dataType}}} value) {
{{#formatSingleLine}}{{>common/model/enumName}}{{/formatSingleLine}}({{{vendorExtensions.baseType}}} value) {
this.value = value;
}

Expand All @@ -51,7 +54,7 @@
{{#jackson}}
@JsonValue
{{/jackson}}
public {{{dataType}}} getValue() {
public {{{vendorExtensions.baseType}}} getValue() {
return value;
}
{{/lombok}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
{{/vendorExtensions.x-field-extra-annotation}}
{{#vendorExtensions.x-is-jackson-optional-nullable}}
{{#isContainer}}
private JsonNullable<{{{datatypeWithEnum}}}> {{name}} = JsonNullable.<{{{datatypeWithEnum}}}>undefined();
private JsonNullable<{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}> {{name}} = JsonNullable.<{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}>undefined();
{{/isContainer}}
{{^isContainer}}
private JsonNullable<{{{datatypeWithEnum}}}> {{name}} = JsonNullable.<{{{datatypeWithEnum}}}>{{#defaultValue}}of({{{.}}}){{/defaultValue}}{{^defaultValue}}undefined(){{/defaultValue}};
private JsonNullable<{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}> {{name}} = JsonNullable.<{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}>{{#defaultValue}}of({{{.}}}){{/defaultValue}}{{^defaultValue}}undefined(){{/defaultValue}};
{{/isContainer}}
{{/vendorExtensions.x-is-jackson-optional-nullable}}
{{^vendorExtensions.x-is-jackson-optional-nullable}}
{{#isContainer}}
private {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}} {{name}}{{#required}}{{^requiredPropertiesInConstructor}}{{#vendorExtensions.defaultValueIsNotNull}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{/vendorExtensions.defaultValueIsNotNull}}{{/requiredPropertiesInConstructor}}{{/required}};
{{/isContainer}}
{{^isContainer}}
{{#isDiscriminator}}protected{{/isDiscriminator}}{{^isDiscriminator}}private{{/isDiscriminator}} {{{datatypeWithEnum}}} {{name}}{{#vendorExtensions.defaultValueIsNotNull}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{/vendorExtensions.defaultValueIsNotNull}};
{{#isDiscriminator}}protected{{/isDiscriminator}}{{^isDiscriminator}}private{{/isDiscriminator}} {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}} {{name}}{{#vendorExtensions.defaultValueIsNotNull}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{/vendorExtensions.defaultValueIsNotNull}};
{{/isContainer}}
{{/vendorExtensions.x-is-jackson-optional-nullable}}
{{/formatNoEmptyLines}}
Expand Down Expand Up @@ -335,7 +335,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
public {{classname}} put{{nameInCamelCase}}Item(String key, {{{items.datatypeWithEnum}}} {{name}}Item) {
{{#vendorExtensions.x-is-jackson-optional-nullable}}
if ({{name}} == null || !{{name}}.isPresent()) {
{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}});
{{name}} = JsonNullable.<{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}>of({{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}});
}
try {
{{name}}.get().put(key, {{name}}Item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{{!The body is generated to verify that example values are passed correctly}}
{{#allParams}}
{{^isFile}}
{{{dataType}}} {{paramName}}Expected = {{{example}}};
{{{vendorExtensions.baseType}}} {{paramName}}Expected = {{{example}}};
assert {{paramName}}.equals({{paramName}}Expected) : "The parameter {{paramName}} was expected to match its example value";
{{/isFile}}
{{/allParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
{{{vendorExtensions.typeWithGenericAnnotations}}}
{{/isDate}}{{/isDateTime}}{{/isEnum}}
{{#isDateTime}}
{{#vendorExtensions.formatPattern}}@Format("{{{.}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateTimeFormat}}@Format("{{{.}}}"){{/dateTimeFormat}}{{/vendorExtensions.formatPattern}} {{{dataType}}}
{{#vendorExtensions.formatPattern}}@Format("{{{.}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateTimeFormat}}@Format("{{{.}}}"){{/dateTimeFormat}}{{/vendorExtensions.formatPattern}} {{#isEnum}}{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{/isEnum}}{{^isEnum}}{{{vendorExtensions.typeWithGenericAnnotations}}}{{/isEnum}}
{{/isDateTime}}
{{#isDate}}
{{#vendorExtensions.formatPattern}}@Format("{{{.}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateFormat}}@Format("{{{.}}}"){{/dateFormat}}{{/vendorExtensions.formatPattern}} {{{dataType}}}
{{#vendorExtensions.formatPattern}}@Format("{{{.}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateFormat}}@Format("{{{.}}}"){{/dateFormat}}{{/vendorExtensions.formatPattern}} {{#isEnum}}{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{/isEnum}}{{^isEnum}}{{{vendorExtensions.typeWithGenericAnnotations}}}{{/isEnum}}
{{/isDate}}
{{#isEnum}}
{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class {{classname}}Spec extends Specification {
def '{{operationId}}() method test'() {
given:
{{#allParams}}
{{{dataType}}} {{paramName}} = {{{vendorExtensions.groovyExample}}}
{{{vendorExtensions.baseType}}} {{paramName}} = {{{vendorExtensions.groovyExample}}}
{{/allParams}}

when:
Expand Down Expand Up @@ -94,7 +94,7 @@ class {{classname}}Spec extends Specification {
given:
{{!Create the body}}
{{#bodyParam}}
{{{dataType}}} body = {{{vendorExtensions.groovyExample}}}
{{{vendorExtensions.baseType}}} body = {{{vendorExtensions.groovyExample}}}
{{/bodyParam}}
{{#formParams.0}}
var form = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class {{classname}}Test {
void {{operationId}}MethodTest() {
// given
{{#allParams}}
{{{dataType}}} {{paramName}} = {{{example}}};
{{{vendorExtensions.baseType}}} {{paramName}} = {{{example}}};
{{/allParams}}

// when
Expand Down Expand Up @@ -103,7 +103,7 @@ class {{classname}}Test {
// given
{{!Create the body}}
{{#bodyParam}}
{{{dataType}}} body = {{{example}}};
{{{vendorExtensions.baseType}}} body = {{{example}}};
{{/bodyParam}}
{{#formParams.0}}
var form = new HashMap<String, Object>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
{{#withXml}}
@XmlEnumValue({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
{{/withXml}}
{{#deprecated}}
@Deprecated("")
{{/deprecated}}
@JsonProperty({{{strValue}}})
{{{name}}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
Expand Down
Loading
Loading