Add isJson, isXml to consumes, produces (#16085)

* add isJson, isXml to consumes, produces

* code cleanup, update samples
This commit is contained in:
William Cheng
2023-07-14 16:02:59 +08:00
committed by GitHub
parent b7e7314d8e
commit 80e2c05bad
5 changed files with 75 additions and 24 deletions
@@ -201,6 +201,7 @@ public class DefaultCodegen implements CodegenConfig {
protected int removeOperationIdPrefixCount = 1;
protected boolean skipOperationExample;
protected final static Pattern XML_MIME_PATTERN = Pattern.compile("(?i)application\\/(.*)[+]?xml(;.*)?");
protected final static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application\\/json(;.*)?");
protected final static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application\\/vnd.(.*)+json(;.*)?");
private static final Pattern COMMON_PREFIX_ENUM_NAME = Pattern.compile("[a-zA-Z0-9]+\\z");
@@ -6716,6 +6717,11 @@ public class DefaultCodegen implements CodegenConfig {
continue;
} else {
mediaType.put("mediaType", escapeQuotationMark(key));
if (isJsonMimeType(key)) {
mediaType.put("isJson", "true");
} else if (isXmlMimeType(key)) {
mediaType.put("isXml", "true");
}
}
mediaTypeList.add(mediaType);
}
@@ -6786,6 +6792,11 @@ public class DefaultCodegen implements CodegenConfig {
if (!existingMediaTypes.contains(encodedKey)) {
Map<String, String> mediaType = new HashMap<>();
mediaType.put("mediaType", encodedKey);
if (isJsonMimeType(encodedKey)) {
mediaType.put("isJson", "true");
} else if (isXmlMimeType(encodedKey)) {
mediaType.put("isXml", "true");
}
codegenOperation.produces.add(mediaType);
codegenOperation.hasProduces = Boolean.TRUE;
}
@@ -8011,10 +8022,14 @@ public class DefaultCodegen implements CodegenConfig {
* @param mime MIME string
* @return true if the input matches the JSON MIME
*/
protected static boolean isJsonMimeType(String mime) {
public static boolean isJsonMimeType(String mime) {
return mime != null && (JSON_MIME_PATTERN.matcher(mime).matches());
}
public static boolean isXmlMimeType(String mime) {
return mime != null && (XML_MIME_PATTERN.matcher(mime).matches());
}
/**
* Check if the given MIME is a JSON Vendor MIME.
* JSON MIME examples:
@@ -144,12 +144,6 @@ public class JavaCamelServerCodegen extends SpringCodegen implements BeanValidat
if (!APPLICATION_JSON.equals(mediaType) && !APPLICATION_XML.equals(mediaType)) {
bindingModeOff = true;
}
if (APPLICATION_JSON.equals(mediaType)) {
produces.put("isJson", "true");
}
if (APPLICATION_XML.equals(mediaType)) {
produces.put("isXml", "true");
}
}
}
if (co.hasConsumes) {
@@ -158,12 +152,6 @@ public class JavaCamelServerCodegen extends SpringCodegen implements BeanValidat
if (!APPLICATION_JSON.equals(mediaType) && !APPLICATION_XML.equals(mediaType)) {
bindingModeOff = true;
}
if (APPLICATION_JSON.equals(mediaType)) {
consumes.put("isJson", "true");
}
if (APPLICATION_XML.equals(mediaType)) {
consumes.put("isXml", "true");
}
}
}
co.vendorExtensions.put(CAMEL_REST_BINDING_MODE, bindingModeOff);
@@ -4772,4 +4772,10 @@ public class DefaultCodegenTest {
CodegenMediaType mt = content.get("application/json");
assertNotNull(mt.getExample());
}
@Test void testIsXML() {
final DefaultCodegen codegen = new DefaultCodegen();
Assert.assertTrue(codegen.isXmlMimeType("application/xml"));
Assert.assertTrue(codegen.isXmlMimeType("application/rss+xml"));
}
}
@@ -25,6 +25,8 @@ import org.openapitools.codegen.languages.JavascriptClientCodegen;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.Map;
public class JavascriptClientCodegenTest {
@Test
@@ -127,4 +129,44 @@ public class JavascriptClientCodegenTest {
Assert.assertEquals(codegenParameter.collectionFormat, "passthrough");
}
@Test(description = "test isJson, isXml")
public void testIsJsonIsXmlInConsumes() throws Exception {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
Operation textOperation = openAPI.getPaths().get("/pet").getPost();
CodegenOperation coText = codegen.fromOperation("/user", "post", textOperation, null);
for (Map<String, String> consume: coText.consumes) {
if ("application/json".equals(consume.get("mediaType"))) {
Assert.assertEquals(consume.get("isJson"), "true");
}
if ("application/xml".equals(consume.get("mediaType"))) {
Assert.assertEquals(consume.get("isXml"), "true");
}
}
}
@Test(description = "test isJson, isXml")
public void testIsJsonIsXmlInProduces() throws Exception {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
Operation textOperation = openAPI.getPaths().get("/pet/{petId}").getGet();
CodegenOperation coText = codegen.fromOperation("/user", "get", textOperation, null);
for (Map<String, String> consume: coText.produces) {
if ("application/json".equals(consume.get("mediaType"))) {
Assert.assertEquals(consume.get("isJson"), "true");
}
if ("application/xml".equals(consume.get("mediaType"))) {
Assert.assertEquals(consume.get("isXml"), "true");
}
}
}
}
@@ -27,7 +27,7 @@ public interface PathHandlerInterface {
* <ul>
* </ul>
*
* <p><b>Consumes</b>: [{mediaType=application/json}, {mediaType=application/xml}]</p>
* <p><b>Consumes</b>: [{isJson=true, mediaType=application/json}, {isXml=true, mediaType=application/xml}]</p>
* <p><b>Payload</b>: {@link Pet} (<i>required: true</i>)</p>
*
*
@@ -94,7 +94,7 @@ public interface PathHandlerInterface {
* </li>
* </ul>
*
* <p><b>Produces</b>: [{mediaType=application/xml}, {mediaType=application/json}]</p>
* <p><b>Produces</b>: [{isXml=true, mediaType=application/xml}, {isJson=true, mediaType=application/json}]</p>
* <p><b>Returns</b>: {@link java.util.List List} of {@link Pet}</p>
*
* <p><b>Responses</b>:</p>
@@ -126,7 +126,7 @@ public interface PathHandlerInterface {
* </li>
* </ul>
*
* <p><b>Produces</b>: [{mediaType=application/xml}, {mediaType=application/json}]</p>
* <p><b>Produces</b>: [{isXml=true, mediaType=application/xml}, {isJson=true, mediaType=application/json}]</p>
* <p><b>Returns</b>: {@link java.util.List List} of {@link Pet}</p>
*
* <p><b>Responses</b>:</p>
@@ -159,7 +159,7 @@ public interface PathHandlerInterface {
* </li>
* </ul>
*
* <p><b>Produces</b>: [{mediaType=application/xml}, {mediaType=application/json}]</p>
* <p><b>Produces</b>: [{isXml=true, mediaType=application/xml}, {isJson=true, mediaType=application/json}]</p>
* <p><b>Returns</b>: {@link Pet}</p>
*
* <p><b>Responses</b>:</p>
@@ -181,7 +181,7 @@ public interface PathHandlerInterface {
* <ul>
* </ul>
*
* <p><b>Consumes</b>: [{mediaType=application/json}, {mediaType=application/xml}]</p>
* <p><b>Consumes</b>: [{isJson=true, mediaType=application/json}, {isXml=true, mediaType=application/xml}]</p>
* <p><b>Payload</b>: {@link Pet} (<i>required: true</i>)</p>
*
*
@@ -280,7 +280,7 @@ public interface PathHandlerInterface {
*
* <p><b>Consumes</b>: [{mediaType=multipart/form-data}]</p>
*
* <p><b>Produces</b>: [{mediaType=application/json}]</p>
* <p><b>Produces</b>: [{isJson=true, mediaType=application/json}]</p>
* <p><b>Returns</b>: {@link ModelApiResponse}</p>
*
* <p><b>Responses</b>:</p>
@@ -328,7 +328,7 @@ public interface PathHandlerInterface {
*
* <p><b>Endpoint</b>: {@link Methods#GET GET} "/v2/store/inventory" (<i>privileged: true</i>)</p>
*
* <p><b>Produces</b>: [{mediaType=application/json}]</p>
* <p><b>Produces</b>: [{isJson=true, mediaType=application/json}]</p>
* <p><b>Returns</b>: {@link java.util.Map Map} of {@link Integer}</p>
*
* <p><b>Responses</b>:</p>
@@ -359,7 +359,7 @@ public interface PathHandlerInterface {
* </li>
* </ul>
*
* <p><b>Produces</b>: [{mediaType=application/xml}, {mediaType=application/json}]</p>
* <p><b>Produces</b>: [{isXml=true, mediaType=application/xml}, {isJson=true, mediaType=application/json}]</p>
* <p><b>Returns</b>: {@link Order}</p>
*
* <p><b>Responses</b>:</p>
@@ -381,7 +381,7 @@ public interface PathHandlerInterface {
* <ul>
* </ul>
*
* <p><b>Produces</b>: [{mediaType=application/xml}, {mediaType=application/json}]</p>
* <p><b>Produces</b>: [{isXml=true, mediaType=application/xml}, {isJson=true, mediaType=application/json}]</p>
* <p><b>Returns</b>: {@link Order}</p>
*
* <p><b>Responses</b>:</p>
@@ -497,7 +497,7 @@ public interface PathHandlerInterface {
* </li>
* </ul>
*
* <p><b>Produces</b>: [{mediaType=application/xml}, {mediaType=application/json}]</p>
* <p><b>Produces</b>: [{isXml=true, mediaType=application/xml}, {isJson=true, mediaType=application/json}]</p>
* <p><b>Returns</b>: {@link User}</p>
*
* <p><b>Responses</b>:</p>
@@ -544,7 +544,7 @@ public interface PathHandlerInterface {
"format" : "date-time"
}', minimum='null', maximum='null', exclusiveMinimum=false, exclusiveMaximum=false, required=false, deprecated=false, hasMoreNonReadOnly=false, isPrimitiveType=false, isModel=false, isContainer=false, isString=false, isNumeric=false, isInteger=false, isShort=false, isLong=false, isUnboundedInteger=false, isNumber=false, isFloat=false, isDouble=false, isDecimal=false, isByteArray=false, isBinary=false, isFile=false, isBoolean=false, isDate=false, isDateTime=true, isUuid=false, isUri=false, isEmail=false, isPassword=false, isFreeFormObject=false, isArray=false, isMap=false, isEnum=false, isInnerEnum=false, isEnumRef=false, isAnyType=false, isReadOnly=false, isWriteOnly=false, isNullable=false, isSelfReference=false, isCircularReference=false, isDiscriminator=false, isNew=false, isOverridden=null, _enum=null, allowableValues=null, items=null, additionalProperties=null, vars=[], requiredVars=[], mostInnerItems=null, vendorExtensions={}, hasValidation=false, isInherited=false, discriminatorValue='null', nameInCamelCase='XExpiresAfter', nameInSnakeCase='X_EXPIRES_AFTER', enumName='null', maxItems=null, minItems=null, maxProperties=null, minProperties=null, uniqueItems=false, uniqueItemsBoolean=null, multipleOf=null, isXmlAttribute=false, xmlPrefix='null', xmlName='null', xmlNamespace='null', isXmlWrapped=false, isNull=false, isVoid=false, getAdditionalPropertiesIsAnyType=false, getHasVars=false, getHasRequired=false, getHasDiscriminatorWithNonEmptyMapping=false, composedSchemas=null, hasMultipleTypes=false, requiredVarsMap=null, ref=null, schemaIsFromAdditionalProperties=false, isBooleanSchemaTrue=false, isBooleanSchemaFalse=false, format=date-time, dependentRequired=null, contains=null}]</p>
*
* <p><b>Produces</b>: [{mediaType=application/xml}, {mediaType=application/json}]</p>
* <p><b>Produces</b>: [{isXml=true, mediaType=application/xml}, {isJson=true, mediaType=application/json}]</p>
* <p><b>Returns</b>: {@link String}</p>
*
* <p><b>Responses</b>:</p>