Skip to content

Commit

Permalink
feat(c#, java): change type of PermissionResource.type to String (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar authored Feb 23, 2022
1 parent d61b4d1 commit 7d3f063
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ public boolean supportsStacksTemplates()
return true;
}

@Override
public boolean permissionResourceTypeAsString()
{
return true;
}

@NotNull
@Override
public Collection<String> getTypeAdapterImports()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public interface InfluxGenerator

String toEnumConstructorDefaultValue(String value, String datatype);

String toEnumVarName(String value, String datatype);

@Nullable
String optionalDatatypeKeyword();

Expand All @@ -28,6 +30,8 @@ public interface InfluxGenerator

boolean supportsStacksTemplates();

boolean permissionResourceTypeAsString();

@Nonnull
Collection<String> getTypeAdapterImports();
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ public boolean supportsStacksTemplates()
return false;
}

@Override
public boolean permissionResourceTypeAsString()
{
return true;
}

@NotNull
@Override
public Collection<String> getTypeAdapterImports()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ public boolean supportsStacksTemplates()
return false;
}

@Override
public boolean permissionResourceTypeAsString()
{
return false;
}

@NotNull
@Override
public Collection<String> getTypeAdapterImports()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ public boolean supportsStacksTemplates()
return true;
}

@Override
public boolean permissionResourceTypeAsString()
{
return false;
}

@NotNull
@Override
public Collection<String> getTypeAdapterImports()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,25 @@ void postProcessOpenAPI()
}
}

//
// Change PermissionResource type to String schema - fixup to avoid exception for non-existing item
//
if (generator.permissionResourceTypeAsString())
{
StringSchema oldSchema = (StringSchema) openAPI.getComponents().getSchemas().get("Resource").getProperties().get("type");
StringSchema newSchema = new StringSchema();
List<Map<String, String>> staticValues = oldSchema.getEnum().stream().map(item -> {
Map<String, String> map = new HashMap<>();
map.put("title", generator.toEnumVarName("type_" + org.openapitools.codegen.utils.StringUtils.camelize(item), oldSchema.getType()));
map.put("value", item);

return map;
}).collect(Collectors.toList());
newSchema.addExtension("x-static-values", staticValues);
newSchema.addExtension("x-has-static-values", true);
changePropertySchema("type", "Resource", newSchema);
}

//
// Drop supports for Geo
//
Expand Down
6 changes: 6 additions & 0 deletions openapi-generator/src/main/resources/Java/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public class {{classname}}{{#vendorExtensions.x-has-generic-type}}{{{vendorExten
{{/isContainer}}
{{/isXmlAttribute}}
{{/withXml}}
{{#vendorExtensions.x-has-static-values}}
// Possible values for {{name}} property:
{{/vendorExtensions.x-has-static-values}}
{{#vendorExtensions.x-static-values}}
public static String {{title}} = "{{value}}";
{{/vendorExtensions.x-static-values}}
{{#gson}}
public static final String SERIALIZED_NAME_{{nameInSnakeCase}} = "{{baseName}}";
@SerializedName(SERIALIZED_NAME_{{nameInSnakeCase}})
Expand Down
13 changes: 10 additions & 3 deletions openapi-generator/src/main/resources/csharp/modelGeneric.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@
{{^isInherited}}
{{^isReadOnly}}
{{#required}}
// to ensure "{{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}}" is required (not null)
{{^isEnum}}
{{^vendorExtensions.x-has-static-values}}// to ensure "{{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}}" is required (not null){{/vendorExtensions.x-has-static-values}}
{{^isEnum}}{{^vendorExtensions.x-has-static-values}}
if ({{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}} == null)
{
throw new InvalidDataException("{{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}} is a required property for {{classname}} and cannot be null");
}
{{/isEnum}}
{{/vendorExtensions.x-has-static-values}}{{/isEnum}}
this.{{name}} = {{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}};
{{/required}}
{{/isReadOnly}}
Expand Down Expand Up @@ -99,6 +99,13 @@ this.{{name}} = {{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}};
{{#vars}}
{{^isInherited}}
{{^isEnum}}
{{#vendorExtensions.x-has-static-values}}
// Possible values for {{name}} property:
{{#vendorExtensions.x-static-values}}
public const string {{title}} = "{{value}}";
{{/vendorExtensions.x-static-values}}

{{/vendorExtensions.x-has-static-values}}
/// <summary>
/// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{description}}{{/description}}
/// </summary>{{#description}}
Expand Down

0 comments on commit 7d3f063

Please sign in to comment.