[Core] Does not recognize the type "ByteArray" as "String" (#10749)

* Does not recognize the type "ByteArray" as "String"

* Change in default codegen

* Removes redundant updateRequestBodyForString

* Adds testByteArrayTypeInSchemas

Co-authored-by: Justin Black <justin.a.black@gmail.com>
This commit is contained in:
Hui Yu 2021-11-02 03:18:21 +08:00 committed by GitHub
parent 7559b53370
commit fe90d9c426
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 32 deletions

View File

@ -3336,6 +3336,7 @@ public class DefaultCodegen implements CodegenConfig {
protected void updatePropertyForString(CodegenProperty property, Schema p) {
if (ModelUtils.isByteArraySchema(p)) {
property.setIsString(false);
property.isByteArray = true;
} else if (ModelUtils.isBinarySchema(p)) {
property.isBinary = true;
@ -4277,6 +4278,7 @@ public class DefaultCodegen implements CodegenConfig {
} else if (ModelUtils.isUUIDSchema(responseSchema)) {
r.isUuid = true;
} else if (ModelUtils.isByteArraySchema(responseSchema)) {
r.setIsString(false);
r.isByteArray = true;
} else if (ModelUtils.isBinarySchema(responseSchema)) {
r.isFile = true; // file = binary in OAS3
@ -6529,6 +6531,7 @@ public class DefaultCodegen implements CodegenConfig {
protected void updateRequestBodyForString(CodegenParameter codegenParameter, Schema schema, Set<String> imports, String bodyParameterName) {
updateRequestBodyForPrimitiveType(codegenParameter, schema, bodyParameterName, imports);
if (ModelUtils.isByteArraySchema(schema)) {
codegenParameter.setIsString(false);
codegenParameter.isByteArray = true;
} else if (ModelUtils.isBinarySchema(schema)) {
codegenParameter.isBinary = true;

View File

@ -1686,38 +1686,6 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
}
}
@Override
protected void updateRequestBodyForString(CodegenParameter codegenParameter, Schema schema, Set<String> imports, String bodyParameterName) {
/**
* we have a custom version of this function to set isString to false for isByteArray
*/
updateRequestBodyForPrimitiveType(codegenParameter, schema, bodyParameterName, imports);
if (ModelUtils.isByteArraySchema(schema)) {
codegenParameter.isByteArray = true;
// custom code
codegenParameter.setIsString(false);
} else if (ModelUtils.isBinarySchema(schema)) {
codegenParameter.isBinary = true;
codegenParameter.isFile = true; // file = binary in OAS3
} else if (ModelUtils.isUUIDSchema(schema)) {
codegenParameter.isUuid = true;
} else if (ModelUtils.isURISchema(schema)) {
codegenParameter.isUri = true;
} else if (ModelUtils.isEmailSchema(schema)) {
codegenParameter.isEmail = true;
} else if (ModelUtils.isDateSchema(schema)) { // date format
codegenParameter.setIsString(false); // for backward compatibility with 2.x
codegenParameter.isDate = true;
} else if (ModelUtils.isDateTimeSchema(schema)) { // date-time format
codegenParameter.setIsString(false); // for backward compatibility with 2.x
codegenParameter.isDateTime = true;
} else if (ModelUtils.isDecimalSchema(schema)) { // type: string, format: number
codegenParameter.isDecimal = true;
codegenParameter.setIsString(false);
}
codegenParameter.pattern = toRegularExpression(schema.getPattern());
}
@Override
protected void updateParameterForString(CodegenParameter codegenParameter, Schema parameterSchema){
/**

View File

@ -3846,4 +3846,29 @@ public class DefaultCodegenTest {
cp = co.queryParams.get(0);
assertTrue(cp.getIsAnyType());
}
@Test
public void testByteArrayTypeInSchemas() {
DefaultCodegen codegen = new DefaultCodegen();
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_10725.yaml");
codegen.setOpenAPI(openAPI);
String path;
CodegenOperation co;
CodegenParameter cp;
path = "/TxRxByteArray";
co = codegen.fromOperation(path, "POST", openAPI.getPaths().get(path).getPost(), null);
cp = co.bodyParam;
assertTrue(cp.isByteArray);
assertFalse(cp.getIsString());
CodegenResponse cr = co.responses.get(0);
assertTrue(cr.isByteArray);
assertFalse(cr.getIsString());
String modelName = "ObjectContainingByteArray";
CodegenModel m = codegen.fromModel(modelName, openAPI.getComponents().getSchemas().get(modelName));
CodegenProperty pr = m.vars.get(0);
assertTrue(pr.isByteArray);
assertFalse(pr.getIsString());
}
}

View File

@ -0,0 +1,46 @@
openapi: 3.0.1
info:
title: OpenAPI Petstore
description: "byteArray schema isX type checks"
license:
name: Apache-2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.0
servers:
- url: http://petstore.swagger.io:80/v2
tags: []
paths:
/TxRxByteArray:
post:
requestBody:
required: true
content:
application/json:
schema:
type: string
format: byte
responses:
'200':
description: ComposedObject
content:
application/json:
schema:
type: string
format: byte
/RxRefObjectContainingByteArray:
get:
responses:
'200':
description: ComposedNumber
content:
application/json:
schema:
$ref: '#/components/schemas/ObjectContainingByteArray'
components:
schemas:
ObjectContainingByteArray:
type: object
properties:
byteArray:
type: string
format: byte