Adds isDate to CodegenModel, adds test, adds isDate getter and setter to IJsonSchemaValidationProperties (#7652)

This commit is contained in:
Justin Black 2020-10-11 11:06:47 -07:00 committed by GitHub
parent b208a311db
commit ee2a7352a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 173 additions and 1 deletions

View File

@ -64,7 +64,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
public String defaultValue;
public String arrayModelType;
public boolean isAlias; // Is this effectively an alias of another simple type
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble;
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate;
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>(); // all properties (without parent's properties)
public List<CodegenProperty> allVars = new ArrayList<CodegenProperty>(); // all properties (with parent's properties)
public List<CodegenProperty> requiredVars = new ArrayList<CodegenProperty>(); // a list of required properties
@ -570,6 +570,14 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
this.isModel = isModel;
}
@Override
public boolean getIsDate() { return isDate; }
@Override
public void setIsDate(boolean isDate) {
this.isDate = isDate;
}
// indicates if the model component has validation on the root level schema
// this will be true when minItems or minProperties is set
public boolean hasValidation() {
@ -670,6 +678,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
isNumeric == that.isNumeric &&
isFloat == that.isFloat &&
isDouble == that.isDouble &&
isDate == that.isDate &&
hasVars == that.hasVars &&
emptyVars == that.emptyVars &&
hasMoreModels == that.hasMoreModels &&
@ -746,6 +755,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
getDescription(), getClassVarName(), getModelJson(), getDataType(), getXmlPrefix(), getXmlNamespace(),
getXmlName(), getClassFilename(), getUnescapedDescription(), getDiscriminator(), getDefaultValue(),
getArrayModelType(), isAlias, isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble,
isDate,
getVars(), getAllVars(), getRequiredVars(), getOptionalVars(), getReadOnlyVars(), getReadWriteVars(),
getParentVars(), getAllowableValues(), getMandatory(), getAllMandatory(), getImports(), hasVars,
isEmptyVars(), hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArrayModel,
@ -791,6 +801,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
sb.append(", isNumeric=").append(isNumeric);
sb.append(", isFloat=").append(isFloat);
sb.append(", isDouble=").append(isDouble);
sb.append(", isDate=").append(isDate);
sb.append(", vars=").append(vars);
sb.append(", allVars=").append(allVars);
sb.append(", requiredVars=").append(requiredVars);

View File

@ -495,5 +495,13 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
public void setIsModel(boolean isModel) {
this.isModel = isModel;
}
@Override
public boolean getIsDate() { return isDate; }
@Override
public void setIsDate(boolean isDate) {
this.isDate = isDate;
}
}

View File

@ -469,6 +469,14 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
this.isModel = isModel;
}
@Override
public boolean getIsDate() { return isDate; }
@Override
public void setIsDate(boolean isDate) {
this.isDate = isDate;
}
public Map<String, Object> getVendorExtensions() {
return vendorExtensions;
}

View File

@ -290,6 +290,14 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
this.isModel = isModel;
}
@Override
public boolean getIsDate() { return isDate; }
@Override
public void setIsDate(boolean isDate) {
this.isDate = isDate;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("CodegenResponse{");

View File

@ -2520,6 +2520,11 @@ public class DefaultCodegen implements CodegenConfig {
} else { // int32 format
m.isInteger = Boolean.TRUE;
}
} else if (ModelUtils.isDateSchema(schema)) {
// NOTE: Date schemas as CodegenModel is a rare use case and may be removed at a later date.
// Sync of properties is done for consistency with other data types like CodegenParameter/CodegenProperty.
ModelUtils.syncValidationProperties(schema, m);
m.isDate = Boolean.TRUE;
} else if (ModelUtils.isStringSchema(schema)) {
// NOTE: String schemas as CodegenModel is a rare use case and may be removed at a later date.
// Sync of properties is done for consistency with other data types like CodegenParameter/CodegenProperty.

View File

@ -60,4 +60,8 @@ public interface IJsonSchemaValidationProperties {
boolean getIsModel();
void setIsModel(boolean isModel);
boolean getIsDate();
void setIsDate(boolean isDate);
}

View File

@ -2341,4 +2341,50 @@ public class DefaultCodegenTest {
assertEquals(co.responses.get(0).getItems().getMaximum(), "7");
}
@Test
public void testIsXPresence() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7651.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
String modelName;
Schema sc;
CodegenModel cm;
modelName = "DateWithValidation";
sc = openAPI.getComponents().getSchemas().get(modelName);
cm = codegen.fromModel(modelName, sc);
assertEquals(cm.isString, false);
assertEquals(cm.isDate, true);
modelName = "ObjectWithDateWithValidation";
sc = openAPI.getComponents().getSchemas().get(modelName);
cm = codegen.fromModel(modelName, sc);
assertEquals(cm.getVars().get(0).isString, false);
assertEquals(cm.getVars().get(0).isDate, true);
String path;
Operation operation;
CodegenOperation co;
path = "/ref_date_with_validation/{date}";
operation = openAPI.getPaths().get(path).getPost();
co = codegen.fromOperation(path, "POST", operation, null);
assertEquals(co.pathParams.get(0).isString, false);
assertEquals(co.pathParams.get(0).isDate, true);
assertEquals(co.bodyParams.get(0).isString, false);
assertEquals(co.bodyParams.get(0).isDate, true);
assertEquals(co.responses.get(0).isString, false);
assertEquals(co.responses.get(0).isDate, true);
path = "/date_with_validation/{date}";
operation = openAPI.getPaths().get(path).getPost();
co = codegen.fromOperation(path, "POST", operation, null);
assertEquals(co.pathParams.get(0).isString, false);
assertEquals(co.pathParams.get(0).isDate, true);
assertEquals(co.bodyParams.get(0).isString, false);
assertEquals(co.bodyParams.get(0).isDate, true);
assertEquals(co.responses.get(0).isString, false);
assertEquals(co.responses.get(0).isDate, true);
}
}

View File

@ -0,0 +1,82 @@
openapi: 3.0.1
info:
title: OpenAPI Petstore
description: "sample spec"
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:
- name: isX
description: an api that ensures that isX properties are present on Schema classes
paths:
/ref_date_with_validation/{date}:
post:
tags:
- isX
operationId: refDateWithValidation
parameters:
- name: date
in: path
required: true
schema:
$ref: '#/components/schemas/DateWithValidation'
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/DateWithValidation'
required: true
responses:
200:
description: success
content:
application/json:
schema:
$ref: '#/components/schemas/DateWithValidation'
/date_with_validation/{date}:
post:
tags:
- isX
operationId: dateWithValidation
parameters:
- name: date
in: path
required: true
schema:
type: string
format: date
pattern: '^2020.*'
requestBody:
content:
application/json:
schema:
type: string
format: date
pattern: '^2020.*'
required: true
responses:
200:
description: success
content:
application/json:
schema:
type: string
format: date
pattern: '^2020.*'
components:
schemas:
DateWithValidation:
type: string
format: date
pattern: '^2020.*'
ObjectWithDateWithValidation:
type: object
properties:
dateWithValidation:
type: string
format: date
pattern: '^2020.*'
securitySchemes: {}