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

fixed-content-type for java #600

Open
wants to merge 1 commit into
base: main
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
19 changes: 19 additions & 0 deletions src/main/java/com/twilio/oai/api/ApiResourceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.twilio.oai.PathUtils;
import com.twilio.oai.StringHelper;
import com.twilio.oai.common.ApplicationConstants;
import com.twilio.oai.common.EnumConstants;
import com.twilio.oai.common.Utility;
import com.twilio.oai.resolver.Resolver;
import com.twilio.oai.resource.Resource;
Expand Down Expand Up @@ -347,4 +348,22 @@ protected boolean updateNestedContent(CodegenOperation co) {
}
return hasNestedRequestBody;
}

// Supported content types for this method are application/json and application/x-www-form-urlencoded.
protected void updateContentType(CodegenOperation co) {
if (co.consumes == null || co.consumes.size() == 0) {
co.vendorExtensions.put("x-www-form-urlencoded", true);
return;
}
for (Map<String, String> map : co.consumes) {
if (EnumConstants.ContentType.APPLICATION_JSON.getValue().equals(map.get("mediaType"))) {
co.vendorExtensions.put(ApplicationConstants.X_JSON, true);
} else if (EnumConstants.ContentType.APPLICATION_FORM_URLENCODED.getValue().equals(map.get("mediaType"))) {
co.vendorExtensions.put(ApplicationConstants.X_WWW_FORM_URLENCODED, true);
} else {
// TODO: GET does not have body thus does not have content type
co.vendorExtensions.put(ApplicationConstants.X_WWW_FORM_URLENCODED, true);
}
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/twilio/oai/api/JavaApiResourceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public ApiResourceBuilder updateOperations(Resolver<CodegenParameter> codegenPar
JsonRequestBodyResolver jsonRequestBodyResolver = new JsonRequestBodyResolver(this, codegenPropertyIResolver);
this.codegenOperationList.forEach(co -> {
updateNestedContent(co);
updateContentType(co);
updateHttpMethod(co);
List<String> filePathArray = new ArrayList<>(Arrays.asList(co.baseName.split(PATH_SEPARATOR_PLACEHOLDER)));
String resourceName = filePathArray.remove(filePathArray.size()-1);
Expand Down Expand Up @@ -667,4 +668,5 @@ private void addUniqueResponseModelEnums(List<CodegenProperty> codegenProperties
public JavaApiResources build() {
return new JavaApiResources(this);
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/twilio/oai/common/ApplicationConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ public class ApplicationConstants {
public static final String PHONE_NUMBER = "phone-number";

public static final Predicate<Integer> SUCCESS = i -> i != null && i >= 200 && i < 400;

public static final String X_JSON = "x-is-json";
public static final String X_WWW_FORM_URLENCODED = "x-www-form-urlencoded";
}
9 changes: 9 additions & 0 deletions src/main/java/com/twilio/oai/common/EnumConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,13 @@ public enum CsharpHttpMethod {

private final String value;
}

@Getter
@RequiredArgsConstructor
public enum ContentType {
APPLICATION_JSON("application/json"),
APPLICATION_FORM_URLENCODED("application/x-www-form-urlencoded");

private final String value;
}
}
10 changes: 9 additions & 1 deletion src/test/java/com/twilio/oai/TwilioGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
public class TwilioGeneratorTest {
@Parameterized.Parameters
public static Collection<Generator> generators() {
return Arrays.asList(Generator.TWILIO_PYTHON);
return Arrays.asList(Generator.TWILIO_CSHARP,
Generator.TWILIO_GO,
Generator.TWILIO_JAVA,
Generator.TWILIO_NODE,
Generator.TWILIO_PHP,
Generator.TWILIO_PYTHON,
Generator.TWILIO_RUBY,
Generator.TWILIO_TERRAFORM);
}

private final Generator generator;
Expand All @@ -41,6 +48,7 @@ public static void setUp() {
@Test
public void launchGenerator() {
final String pathname = "examples/spec/twilio_api_v2010.yaml";
// final String pathname = "examples/twilio_messaging_bulk_v1.yaml";
File filesList[] ;
File directoryPath = new File(pathname);
if (directoryPath.isDirectory()) {
Expand Down
Loading