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

Add isEnumRef, isEnumOrRef to CodegenProperty #13880

Merged
merged 5 commits into from
Nov 8, 2022
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 @@ -148,8 +148,9 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
public boolean isAnyType;
public boolean isArray;
public boolean isMap;
public boolean isEnum;
public boolean isEnum; // true if the enum is defined inline
public boolean isInnerEnum; // Enums declared inline will be located inside the generic model, changing how the enum is referenced in some cases.
public boolean isEnumRef; // true if it's a reference to an enum
public boolean isReadOnly;
public boolean isWriteOnly;
public boolean isNullable;
Expand Down Expand Up @@ -225,10 +226,14 @@ public void setDependentRequired(LinkedHashMap<String, List<String>> dependentRe
}

@Override
public void setFormat(String format) { this.format = format; }
public void setFormat(String format) {
this.format = format;
}

@Override
public String getFormat() { return format; }
public String getFormat() {
return format;
}

@Override
public boolean getIsBooleanSchemaTrue() {
Expand Down Expand Up @@ -500,7 +505,7 @@ public boolean getRequired() {
return required;
}

public boolean compulsory(){
public boolean compulsory() {
return getRequired() && !isNullable;
}

Expand Down Expand Up @@ -963,15 +968,32 @@ public void setHasMultipleTypes(boolean hasMultipleTypes) {
this.hasMultipleTypes = hasMultipleTypes;
}

public boolean getIsUuid() { return isUuid; }
public boolean getIsUuid() {
return isUuid;
}

public void setIsUuid(boolean isUuid) { this.isUuid = isUuid; }
public void setIsUuid(boolean isUuid) {
this.isUuid = isUuid;
}

@Override
public Map<String, CodegenProperty> getRequiredVarsMap() { return requiredVarsMap; }
public Map<String, CodegenProperty> getRequiredVarsMap() {
return requiredVarsMap;
}

@Override
public void setRequiredVarsMap(Map<String, CodegenProperty> requiredVarsMap) { this.requiredVarsMap=requiredVarsMap; }
public void setRequiredVarsMap(Map<String, CodegenProperty> requiredVarsMap) {
this.requiredVarsMap = requiredVarsMap;
}

/**
* Return true if it's an enum (inline or ref)
*
* @return true if it's an enum (inline or ref)
*/
public boolean getIsEnumOrRef() {
return isEnum || isEnumRef;
}

@Override
public String toString() {
Expand Down Expand Up @@ -1033,6 +1055,7 @@ public String toString() {
sb.append(", isMap=").append(isMap);
sb.append(", isEnum=").append(isEnum);
sb.append(", isInnerEnum=").append(isInnerEnum);
sb.append(", isEnumRef=").append(isEnumRef);
sb.append(", isAnyType=").append(isAnyType);
sb.append(", isReadOnly=").append(isReadOnly);
sb.append(", isWriteOnly=").append(isWriteOnly);
Expand Down Expand Up @@ -1122,6 +1145,7 @@ public boolean equals(Object o) {
isMap == that.isMap &&
isEnum == that.isEnum &&
isInnerEnum == that.isInnerEnum &&
isEnumRef == that.isEnumRef &&
isAnyType == that.isAnyType &&
isReadOnly == that.isReadOnly &&
isWriteOnly == that.isWriteOnly &&
Expand Down Expand Up @@ -1205,7 +1229,7 @@ public int hashCode() {
hasMoreNonReadOnly, isPrimitiveType, isModel, isContainer, isString, isNumeric,
isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isFile,
isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject,
isArray, isMap, isEnum, isInnerEnum, isAnyType, isReadOnly, isWriteOnly, isNullable, isShort,
isArray, isMap, isEnum, isInnerEnum, isEnumRef, isAnyType, isReadOnly, isWriteOnly, isNullable, isShort,
isUnboundedInteger, isSelfReference, isCircularReference, isDiscriminator, _enum,
allowableValues, items, mostInnerItems, additionalProperties, vars, requiredVars,
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInCamelCase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3798,6 +3798,8 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
if (referencedSchema.getEnum() != null && !referencedSchema.getEnum().isEmpty()) {
List<Object> _enum = referencedSchema.getEnum();

property.isEnumRef = true;

Map<String, Object> allowableValues = new HashMap<>();
allowableValues.put("values", _enum);
if (allowableValues.size() > 0) {
Expand Down Expand Up @@ -3868,7 +3870,6 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
updatePropertyForAnyType(property, p);
} else if (!ModelUtils.isNullType(p)) {
// referenced model
;
}
if (p.get$ref() != null) {
property.setRef(p.get$ref());
Expand Down Expand Up @@ -5626,7 +5627,7 @@ protected void addVars(IJsonSchemaValidationProperties m, List<CodegenProperty>
continue;
}
cm.hasOptional = cm.hasOptional || !cp.required;
if (cp.isEnum) {
if (cp.getIsEnumOrRef()) { // isEnum or isEnumRef set to true
// FIXME: if supporting inheritance, when called a second time for allProperties it is possible for
// m.hasEnums to be set incorrectly if allProperties has enumerations but properties does not.
cm.hasEnums = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
{{#pattern}}
[ValidatePattern("{{{.}}}")]
{{/pattern}}
{{#isEnum}}
{{#isEnumOrRef}}
{{#allowableValues}}
[ValidateSet({{#values}}"{{{.}}}"{{^-last}}, {{/-last}}{{/values}})]
{{/allowableValues}}
{{/isEnum}}
{{/isEnumOrRef}}
[{{vendorExtensions.x-powershell-data-type}}]
{{=<% %>=}}
${<%name%>}<%#defaultValue%> = <%&.%><%/defaultValue%><%^vendorExtensions.x-powershell-last-writable%>,<%/vendorExtensions.x-powershell-last-writable%>
Expand All @@ -52,11 +52,11 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
{{#pattern}}
[ValidatePattern("{{{.}}}")]
{{/pattern}}
{{#isEnum}}
{{#isEnumOrRef}}
{{#allowableValues}}
[ValidateSet({{#values}}"{{{.}}}"{{^-last}}, {{/-last}}{{/values}})]
{{/allowableValues}}
{{/isEnum}}
{{/isEnumOrRef}}
[{{vendorExtensions.x-powershell-data-type}}]
{{=<% %>=}}
${<%name%>}<%#defaultValue%> = <%&.%><%/defaultValue%><%^-last%>,<%/-last%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
{{#requiredVars}}
if (!missing(`{{name}}`)) {
{{^isContainer}}
{{#isEnum}}
{{#isEnumOrRef}}
if (!(`{{name}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
stop(paste("Error! \"", `{{name}}`, "\" cannot be assigned to `{{name}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = ""))
}
{{/isEnum}}
{{/isEnumOrRef}}
{{#isInteger}}
if (!(is.numeric(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be an integer:", `{{name}}`))
Expand Down Expand Up @@ -126,11 +126,11 @@
{{#optionalVars}}
if (!is.null(`{{name}}`)) {
{{^isContainer}}
{{#isEnum}}
{{#isEnumOrRef}}
if (!(`{{name}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
stop(paste("Error! \"", `{{name}}`, "\" cannot be assigned to `{{name}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = ""))
}
{{/isEnum}}
{{/isEnumOrRef}}
{{#isInteger}}
if (!(is.numeric(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be an integer:", `{{name}}`))
Expand Down Expand Up @@ -275,11 +275,11 @@
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
{{#isEnum}}
{{#isEnumOrRef}}
if (!is.null(this_object$`{{baseName}}`) && !(this_object$`{{baseName}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
stop(paste("Error! \"", this_object$`{{baseName}}`, "\" cannot be assigned to `{{baseName}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = ""))
}
{{/isEnum}}
{{/isEnumOrRef}}
{{#isUri}}
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`{{baseName}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
Expand Down Expand Up @@ -407,11 +407,11 @@
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
{{#isEnum}}
{{#isEnumOrRef}}
if (!is.null(this_object$`{{baseName}}`) && !(this_object$`{{baseName}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
stop(paste("Error! \"", this_object$`{{baseName}}`, "\" cannot be assigned to `{{baseName}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = ""))
}
{{/isEnum}}
{{/isEnumOrRef}}
{{#isUri}}
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`{{name}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
{{#vars}}**{{name}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}[**{{dataType}}**]({{complexType}}.md){{/isPrimitiveType}} | {{description}} | {{^required}}[optional] {{/required}}{{#isReadOnly}}[readonly] {{/isReadOnly}}{{#defaultValue}}[default to {{.}}] {{/defaultValue}}{{#maximum}}[Max: {{{.}}}] {{/maximum}}{{#minimum}}[Min: {{{.}}}] {{/minimum}}{{#isEnum}}[Enum: {{_enum}}] {{/isEnum}}{{#pattern}}[Pattern: {{.}}] {{/pattern}}{{#maxItems}}[Max. items: {{.}}] {{/maxItems}}{{#minItems}}[Min. items: {{.}}] {{/minItems}}{{#maxLength}}[Max. length: {{.}}] {{/maxLength}}{{#minLength}}[Min. length: {{.}}] {{/minLength}}
{{#vars}}**{{name}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}[**{{dataType}}**]({{complexType}}.md){{/isPrimitiveType}} | {{description}} | {{^required}}[optional] {{/required}}{{#isReadOnly}}[readonly] {{/isReadOnly}}{{#defaultValue}}[default to {{.}}] {{/defaultValue}}{{#maximum}}[Max: {{{.}}}] {{/maximum}}{{#minimum}}[Min: {{{.}}}] {{/minimum}}{{#isEnumOrRef}}[Enum: {{_enum}}] {{/isEnumOrRef}}{{#pattern}}[Pattern: {{.}}] {{/pattern}}{{#maxItems}}[Max. items: {{.}}] {{/maxItems}}{{#minItems}}[Min. items: {{.}}] {{/minItems}}{{#maxLength}}[Max. length: {{.}}] {{/maxLength}}{{#minLength}}[Min. length: {{.}}] {{/minLength}}
{{/vars}}

{{/model}}{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -4253,4 +4253,50 @@ public void testAssigning310SpecWorks() {
codegen.setOpenAPI(openAPI);
assertEquals(openAPI, codegen.openAPI);
}

@Test
public void testReferencedEnumType() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue-5676-enums.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
String modelName = "fakeRequestObjectWithReferencedEnum_request";

Schema schemaWithReferencedEnum = openAPI.getComponents().getSchemas().get(modelName);
CodegenModel modelWithReferencedSchema = codegen.fromModel(modelName, schemaWithReferencedEnum);
CodegenProperty referencedEnumSchemaProperty = modelWithReferencedSchema.vars.get(1);

Assert.assertNotNull(schemaWithReferencedEnum);
Assert.assertTrue(modelWithReferencedSchema.hasEnums);
Assert.assertEquals(referencedEnumSchemaProperty.getName(), "enumType");
Assert.assertFalse(referencedEnumSchemaProperty.isEnum);
Assert.assertTrue(referencedEnumSchemaProperty.getIsEnumOrRef());
Assert.assertTrue(referencedEnumSchemaProperty.isEnumRef);
Assert.assertFalse(referencedEnumSchemaProperty.isInnerEnum);
Assert.assertFalse(referencedEnumSchemaProperty.isString);
Assert.assertFalse(referencedEnumSchemaProperty.isContainer);
Assert.assertFalse(referencedEnumSchemaProperty.isPrimitiveType);
}

@Test
public void testInlineEnumType() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue-5676-enums.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
String modelName = "fakeRequestObjectWithInlineEnum_request";

Schema schemaWithReferencedEnum = openAPI.getComponents().getSchemas().get(modelName);
CodegenModel modelWithReferencedSchema = codegen.fromModel(modelName, schemaWithReferencedEnum);
CodegenProperty inlineEnumSchemaProperty = modelWithReferencedSchema.vars.get(1);

Assert.assertNotNull(schemaWithReferencedEnum);
Assert.assertTrue(modelWithReferencedSchema.hasEnums);
Assert.assertEquals(inlineEnumSchemaProperty.getName(), "enumType");
Assert.assertTrue(inlineEnumSchemaProperty.isEnum);
Assert.assertTrue(inlineEnumSchemaProperty.isInnerEnum);
Assert.assertTrue(inlineEnumSchemaProperty.isEnumRef);
Assert.assertTrue(inlineEnumSchemaProperty.getIsEnumOrRef());
Assert.assertTrue(inlineEnumSchemaProperty.isString);
Assert.assertFalse(inlineEnumSchemaProperty.isContainer);
Assert.assertFalse(inlineEnumSchemaProperty.isPrimitiveType);
}
}
Loading