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

fix: convert sid_key ints into strings #57

Merged
merged 2 commits into from
Jun 15, 2021
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
<version>${openapi-generator-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
33 changes: 25 additions & 8 deletions src/main/java/com/twilio/oai/TwilioTerraformGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.responses.ApiResponse;
import lombok.Value;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
Expand Down Expand Up @@ -50,8 +52,14 @@ public void processOpenAPI(final OpenAPI openAPI) {
.getInstancePath(name, openAPI.getPaths().keySet())
.map(PathUtils::getLastPathPart)
.map(PathUtils::removeBraces)
.filter(param -> containsResponseProperty(operation, param))
.ifPresentOrElse(param -> operation.addExtension("x-sid-key", param), () -> path.setPost(null));
.flatMap(param -> getResponsePropertySchema(operation, param))
.ifPresentOrElse(propertySchema -> {
operation.addExtension("x-sid-key", propertySchema.getPropertyName());

if ("integer".equals(propertySchema.getSchema().getType())) {
operation.addExtension("x-sid-key-conversion-func", "Int32ToString");
}
}, () -> path.setPost(null));
}
}));

Expand All @@ -61,20 +69,29 @@ public void processOpenAPI(final OpenAPI openAPI) {
}
}

private boolean containsResponseProperty(final Operation operation, final String propertyName) {
private Optional<PropertySchema> getResponsePropertySchema(final Operation operation, final String propertyName) {
return operation
.getResponses()
.values()
.stream()
.anyMatch(response -> response
.flatMap(response -> response
.getContent()
.values()
.stream()
.map(MediaType::getSchema)
.map(schema -> ModelUtils.getReferencedSchema(this.openAPI, schema))
.map(Schema::getProperties)
.map(Map::keySet)
.anyMatch(properties -> properties.contains(StringUtils.underscore(propertyName))));
.map(properties -> properties.get(StringUtils.underscore(propertyName)))
.filter(Objects::nonNull)
.map(Schema.class::cast)
.map(schema -> new PropertySchema(propertyName, schema)))
.findFirst();
}

@Value
private static class PropertySchema {
String propertyName;
Schema schema;
}

@SuppressWarnings("unchecked")
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/terraform-provider-twilio/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func {{#vendorExtensions.x-is-create-operation}}create{{/vendorExtensions.x-is-c
return diag.FromErr(err)
}

d.SetId(*r.{{vendorExtensions.x-sid-key}})
d.SetId({{vendorExtensions.x-sid-key-conversion-func}}(*r.{{vendorExtensions.x-sid-key}}))
err = MarshalSchema(d, r)
{{/vendorExtensions.x-is-create-operation}}
{{#vendorExtensions.x-is-read-operation}}
Expand Down