Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Updates __new__ to take one arg input #183

Merged
merged 36 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
0fa6656
Adds section for required property type writing
spacether Jun 8, 2023
4b2f51b
Adds required property classes
spacether Jun 8, 2023
b87ae9d
Adds template adjustment for optional properties
spacether Jun 8, 2023
5352f73
Adds template to write optional properties typeddict
spacether Jun 8, 2023
288c248
Successfully generates required and optional typeddicts
spacether Jun 8, 2023
b2fa239
Writes DictInput class to store required and optional input properties
spacether Jun 8, 2023
fea5f20
Changes new dict inptut type for arg_ and changes args_* to arg_
spacether Jun 8, 2023
e677ee7
Update new signature
spacether Jun 8, 2023
c229116
Removes unsets from optional properties, allows self in when optional…
spacether Jun 8, 2023
c9b81c1
Typeddicts only generated when addProps is false
spacether Jun 10, 2023
7f0e206
Tweaks when reqanoptprops are generated
spacether Jun 10, 2023
6c5bd9e
Adds types to dictchema input
spacether Jun 11, 2023
575e952
Adds many use cases object inputs
spacether Jun 11, 2023
88fb582
Adds req property mapping writing when addprops set
spacether Jun 11, 2023
37a1e78
Adds last required object type definition
spacether Jun 11, 2023
3a85c43
Fixes allAreInline for DictInput
spacether Jun 11, 2023
223f2b9
Adds optional properties type hint when addProps unset
spacether Jun 11, 2023
73b0752
Adds and uses new partial
spacether Jun 11, 2023
6b0716f
Adds mapping type for required + opt + addProps
spacether Jun 11, 2023
74ef432
Writes all property input mapping types
spacether Jun 11, 2023
c2b2c04
Updates new signature to use DictInput for all object use cases
spacether Jun 11, 2023
f37cd4d
Updates 3 test files
spacether Jun 12, 2023
c124b08
Fixes more python tests
spacether Jun 12, 2023
e8f1458
Fixes manual tests
spacether Jun 12, 2023
853be64
Fixes python tests
spacether Jun 12, 2023
2f864a5
Converts example properties into example keys, fixes schema naming bug
spacether Jun 12, 2023
6847473
Adds braces aroudn object input
spacether Jun 12, 2023
0a8e8b5
Fixes example addProp key
spacether Jun 12, 2023
d0e826a
arg_ -> arg
spacether Jun 12, 2023
0481983
Schema input configuration_ -> configuration
spacether Jun 12, 2023
aebb488
Removes 'from_openapi_data_'
spacether Jun 12, 2023
6223163
Fixes java tests
spacether Jun 12, 2023
1221263
Samples regenerated
spacether Jun 12, 2023
1edcd48
Unit test sample updated
spacether Jun 12, 2023
7ed8a9f
Fixes bug with allof/anyOf/oneOf type names
spacether Jun 13, 2023
9a58534
Fixes python non compliant discriminator test
spacether Jun 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ public void processOpts() {
this.setEnumUnknownDefaultCase(Boolean.parseBoolean(additionalProperties
.get(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE).toString()));
}
requiredAddPropUnsetSchema = fromSchema(new Schema(), null, null);

}

/***
Expand Down Expand Up @@ -1560,7 +1562,7 @@ public String toModelName(final String name, String jsonPath) {
Map<String, CodegenParameter> codegenParameterCache = new HashMap<>();
HashMap<String, HashMap<String, Integer>> sourceJsonPathToKeyToQty = new HashMap<>();
Map<String, CodegenTag> codegenTagCache = new HashMap<>();
private final CodegenSchema requiredAddPropUnsetSchema = fromSchema(new Schema(), null, null);
private CodegenSchema requiredAddPropUnsetSchema = null;

protected void updateModelForComposedSchema(CodegenSchema m, Schema schema, String sourceJsonPath) {
final ComposedSchema composed = (ComposedSchema) schema;
Expand Down Expand Up @@ -2265,6 +2267,8 @@ public CodegenSchema fromSchema(Schema p, String sourceJsonPath, String currentJ
setSchemaLocationInfo(null, sourceJsonPath, currentJsonPath, property);
HashMap<String, CodegenKey> requiredAndOptionalProperties = new HashMap<>();
property.properties = getProperties(((Schema<?>) p).getProperties(), sourceJsonPath, currentJsonPath, requiredAndOptionalProperties);
LinkedHashSet<String> required = p.getRequired() == null ? new LinkedHashSet<>()
: new LinkedHashSet<>(((Schema<?>) p).getRequired());
List<Schema> oneOfs = ((Schema<?>) p).getOneOf();
if (oneOfs != null && !oneOfs.isEmpty()) {
property.oneOf = getComposedProperties(oneOfs, "oneOf", sourceJsonPath, currentJsonPath);
Expand All @@ -2286,6 +2290,21 @@ public CodegenSchema fromSchema(Schema p, String sourceJsonPath, String currentJ
property.allOf = getComposedProperties(allOfs, "allOf", sourceJsonPath, currentJsonPath);
}
property.additionalProperties = getAdditionalProperties(p, sourceJsonPath, currentJsonPath);
// ideally requiredProperties would come before properties
property.requiredProperties = getRequiredProperties(required, property.properties, property.additionalProperties, requiredAndOptionalProperties, sourceJsonPath, ((Schema<?>) p).getProperties());
property.optionalProperties = getOptionalProperties(property.properties, required, sourceJsonPath);
if ((property.types == null || property.types.contains("object")) && sourceJsonPath != null) {
// only set mapInputJsonPathPiece when object input is possible
if (property.requiredProperties != null && property.optionalProperties == null) {
property.mapInputJsonPathPiece = property.requiredProperties.jsonPathPiece();
} else if (property.requiredProperties == null && property.optionalProperties != null) {
property.mapInputJsonPathPiece = property.optionalProperties.jsonPathPiece();
} else if (property.requiredProperties != null && property.optionalProperties != null) {
property.mapInputJsonPathPiece = getKey("DictInput", "schemaProperty", sourceJsonPath);
} else {
property.mapInputJsonPathPiece = getKey("DictInput", "schemaProperty", sourceJsonPath);
}
}
// end of properties that need to be ordered to set correct camelCase jsonPathPieces

if (currentJsonPath != null) {
Expand Down Expand Up @@ -2352,10 +2371,6 @@ public CodegenSchema fromSchema(Schema p, String sourceJsonPath, String currentJ
}
}
property.patternInfo = getPatternInfo(p.getPattern());
LinkedHashSet<String> required = p.getRequired() == null ? new LinkedHashSet<>()
: new LinkedHashSet<>(((Schema<?>) p).getRequired());
property.optionalProperties = getOptionalProperties(property.properties, required);
property.requiredProperties = getRequiredProperties(required, property.properties, property.additionalProperties, requiredAndOptionalProperties);

property.example = toExampleValue(p);
if (addSchemaImportsFromV3SpecLocations && sourceJsonPath != null && sourceJsonPath.equals(currentJsonPath)) {
Expand Down Expand Up @@ -3295,26 +3310,41 @@ protected LinkedHashMapWithContext<CodegenKey, CodegenSchema> getProperties(Map<
* @param required a set of required properties' name
* @return the optional properties
*/
protected LinkedHashMap<CodegenKey, CodegenSchema> getOptionalProperties(LinkedHashMap<CodegenKey, CodegenSchema> properties, Set<String> required) {
protected LinkedHashMapWithContext<CodegenKey, CodegenSchema> getOptionalProperties(LinkedHashMapWithContext<CodegenKey, CodegenSchema> properties, Set<String> required, String sourceJsonPath) {
if (properties == null) {
return null;
}
if (required.size() == properties.size()) {
return null;
}
if (required.isEmpty()) {
return properties;
// all properties optional and there is no required
LinkedHashMapWithContext<CodegenKey, CodegenSchema> optionalProperties = new LinkedHashMapWithContext<>();
optionalProperties.putAll(properties);
optionalProperties.setAllAreInline(properties.allAreInline());
CodegenKey jsonPathPiece = getKey("DictInput", "schemaProperty", sourceJsonPath);
optionalProperties.setJsonPathPiece(jsonPathPiece);
return optionalProperties;
}

LinkedHashMap<CodegenKey, CodegenSchema> optionalProperties = new LinkedHashMap<>();
// required exists and it can come from addProps or props
LinkedHashMapWithContext<CodegenKey, CodegenSchema> optionalProperties = new LinkedHashMapWithContext<>();
boolean allAreInline = true;
for (Map.Entry<CodegenKey, CodegenSchema> entry : properties.entrySet()) {
final CodegenKey key = entry.getKey();
if (required.contains(key.original)) {
continue;
}
final CodegenSchema prop = entry.getValue();
if (prop.refInfo != null) {
allAreInline = false;
}
optionalProperties.put(key, prop);
}
if (optionalProperties.isEmpty()) {
// no optional props, all required properties came from props
return null;
}
optionalProperties.setAllAreInline(allAreInline);
CodegenKey jsonPathPiece = getKey("OptionalDictInput", "schemaProperty", sourceJsonPath);
optionalProperties.setJsonPathPiece(jsonPathPiece);
return optionalProperties;
}

Expand Down Expand Up @@ -4280,8 +4310,8 @@ public CodegenRequestBody fromRequestBody(RequestBody requestBody, String source
}

@Override
public CodegenKey getKey(String key, String expectedComponentType) {
return getKey(key, expectedComponentType, null);
public CodegenKey getKey(String key, String keyType) {
return getKey(key, keyType, null);
}

public CodegenKey getKey(String key, String keyType, String sourceJsonPath) {
Expand All @@ -4299,9 +4329,13 @@ public CodegenKey getKey(String key, String keyType, String sourceJsonPath) {
if (!sourceJsonPathToKeyToQty.containsKey(sourceJsonPath)) {
sourceJsonPathToKeyToQty.put(sourceJsonPath, keyToQty);
}
Integer qty = keyToQty.getOrDefault(usedKey, 0);
/*
saw use case with component named Client and nested property named client
lowercase to ensure they increment the same key
*/
Integer qty = keyToQty.getOrDefault(usedKey.toLowerCase(Locale.ROOT), 0);
qty += 1;
keyToQty.put(usedKey, qty);
keyToQty.put(usedKey.toLowerCase(Locale.ROOT), qty);
if (qty > 1) {
suffix = qty.toString();
}
Expand Down Expand Up @@ -4352,38 +4386,61 @@ public CodegenKey getKey(String key, String keyType, String sourceJsonPath) {
);
}

protected LinkedHashMap<CodegenKey, CodegenSchema> getRequiredProperties(LinkedHashSet<String> required, LinkedHashMap<CodegenKey, CodegenSchema> properties, CodegenSchema additionalProperties, HashMap<String, CodegenKey> requiredAndOptionalProperties) {
protected LinkedHashMapWithContext<CodegenKey, CodegenSchema> getRequiredProperties(LinkedHashSet<String> required, LinkedHashMap<CodegenKey, CodegenSchema> properties, CodegenSchema additionalProperties, HashMap<String, CodegenKey> requiredAndOptionalProperties, String sourceJsonPath, Map<String, Schema> schemaProperties) {
if (required.isEmpty()) {
return null;
}
/*
requiredProperties use cases:
- no required properties: null or empty list
- requiredProperties + optionalProperties (properties must exist)
- requiredProperties + no optionalProperties
- 1. requiredPropsWithDefAllFromProp - required all come from properties
- 2. requiredPropsWithDefAllFromAddProp - required all come from addProp and there is no properties
- 3. required consume all properties and remainder from addProps
this should be called after vars and additionalProperties are set
Features added by storing codegenProperty values:
- refClass stores reference to additionalProperties definition
- baseName stores original name (can be invalid in a programming language)
- nameInSnakeCase can store valid name for a programming language
*/
LinkedHashMap<CodegenKey, CodegenSchema> requiredProperties = new LinkedHashMap<>();
boolean requiredPropsWithDefAllFromProp = true;
boolean requiredPropsWithDefAllFromAddProp = true;
int propReqProps = 0;
int addPropReqProps = 0;
int reqPropsWithDef = 0;
LinkedHashMapWithContext<CodegenKey, CodegenSchema> requiredProperties = new LinkedHashMapWithContext<>();
boolean allAreInline = true;
for (String requiredPropertyName: required) {
// required property is defined in properties, value is that CodegenSchema
if (properties != null && requiredAndOptionalProperties.containsKey(requiredPropertyName)) {
CodegenKey key = requiredAndOptionalProperties.get(requiredPropertyName);
// get cp from property
CodegenSchema prop = properties.get(key);
if (prop != null) {
requiredPropsWithDefAllFromAddProp = false;
requiredProperties.put(key, prop);
reqPropsWithDef++;
propReqProps++;
if (prop.refInfo != null) {
allAreInline = false;
}
} else {
throw new RuntimeException("Property " + requiredPropertyName + " is missing from getVars");
}
} else if (additionalProperties != null && additionalProperties.isBooleanSchemaFalse) {
// required property is not defined in properties, and additionalProperties is false, value is null
CodegenKey key = getKey(requiredPropertyName, "schemas");
// no schema definition: error use case?
CodegenKey key = getKey(requiredPropertyName, "schemas", null);
requiredProperties.put(key, null);
requiredAndOptionalProperties.put(requiredPropertyName, key);
} else {
// required property is not defined in properties
if (supportsAdditionalPropertiesWithComposedSchema && !disallowAdditionalPropertiesIfNotPresent) {
CodegenSchema prop;
requiredPropsWithDefAllFromProp = false;
reqPropsWithDef++;
addPropReqProps++;
if (additionalProperties == null) {
// additionalProperties is null
// there is NO schema definition for this so the json paths are null
Expand All @@ -4394,13 +4451,28 @@ protected LinkedHashMap<CodegenKey, CodegenSchema> getRequiredProperties(LinkedH
} else {
// additionalProperties is schema
prop = additionalProperties;
if (prop.refInfo != null) {
allAreInline = false;
}
}
CodegenKey key = getKey(requiredPropertyName, "schemas");
CodegenKey key = getKey(requiredPropertyName, "schemas", sourceJsonPath);
requiredProperties.put(key, prop);
requiredAndOptionalProperties.put(requiredPropertyName, key);
}
}
}
String keyName;
boolean onlyReqPropsCase1 = (requiredPropsWithDefAllFromProp && properties != null && requiredProperties.size() == properties.size());
boolean onlyReqPropsCase2 = (requiredPropsWithDefAllFromAddProp && properties == null);
boolean onlyReqPropsCase3 = (propReqProps != 0 && addPropReqProps != 0 && propReqProps + addPropReqProps == reqPropsWithDef && schemaProperties != null && required.containsAll(schemaProperties.keySet()));
if (onlyReqPropsCase1 || onlyReqPropsCase2 || onlyReqPropsCase3) {
keyName = "DictInput";
} else {
keyName = "RequiredDictInput";
}
requiredProperties.setAllAreInline(allAreInline);
CodegenKey jsonPathPiece = getKey(keyName, "schemaProperty", sourceJsonPath);
requiredProperties.setJsonPathPiece(jsonPathPiece);
return requiredProperties;
}

Expand Down Expand Up @@ -4783,7 +4855,7 @@ private ArrayListWithContext<CodegenSchema> getComposedProperties(List<Schema> x
i += 1;
}
xOf.setAllAreInline(allAreInline);
CodegenKey jsonPathPiece = getKey(collectionName, "schemaProperty");
CodegenKey jsonPathPiece = getKey(collectionName, "schemaProperty", sourceJsonPath);
xOf.setJsonPathPiece(jsonPathPiece);
return xOf;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,10 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
if (modelName != null) {
openChars = modelName + "(";
closeChars = ")";
if (ModelUtils.isTypeObjectSchema(schema)) {
openChars = openChars + "{";
closeChars = "}" + closeChars;
}
}

String fullPrefix = currentIndentation + prefix + openChars;
Expand Down Expand Up @@ -1361,7 +1365,7 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
String schemaName = getSchemaName(mm.modelName);
Schema modelSchema = getModelNameToSchemaCache().get(schemaName);
CodegenSchema cp = new CodegenSchema();
cp.jsonPathPiece = getKey(disc.propertyName.original, "misc");
cp.jsonPathPiece = getKey(disc.propertyName.original, "misc", null);
cp.example = discPropNameValue;
return exampleForObjectModel(modelSchema, fullPrefix, closeChars, cp, indentationLevel, exampleLine, closingIndentation, includedSchemas);
}
Expand Down Expand Up @@ -1483,8 +1487,8 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
}
} else if (ModelUtils.isTypeObjectSchema(schema)) {
if (modelName == null) {
fullPrefix += "dict(";
closeChars = ")";
fullPrefix += "{";
closeChars = "}";
}
if (cycleFound) {
return fullPrefix + closeChars;
Expand Down Expand Up @@ -1528,7 +1532,7 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
String schemaName = getSchemaName(mm.modelName);
Schema modelSchema = getModelNameToSchemaCache().get(schemaName);
CodegenSchema cp = new CodegenSchema();
cp.jsonPathPiece = getKey(disc.propertyName.original, "misc");
cp.jsonPathPiece = getKey(disc.propertyName.original, "misc", null);
cp.example = discPropNameValue;
return exampleForObjectModel(modelSchema, fullPrefix, closeChars, cp, indentationLevel, exampleLine, closingIndentation, includedSchemas);
}
Expand All @@ -1544,10 +1548,7 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
key = addPropsSchema.getEnum().get(0).toString();
}
addPropsExample = exampleFromStringOrArraySchema(addPropsSchema, addPropsExample, key);
String addPropPrefix = key + "=";
if (modelName == null) {
addPropPrefix = ensureQuotes(key) + ": ";
}
String addPropPrefix = ensureQuotes(key) + ": ";
String addPropsModelName = getRefClassWithRefModule(addPropsSchema);
if(includedSchemas.contains(schema)) {
return "";
Expand Down Expand Up @@ -1602,7 +1603,7 @@ private String exampleForObjectModel(Schema schema, String fullPrefix, String cl
propSchema,
propExample,
indentationLevel + 1,
propName + "=",
"\"" + propName + "\": ",
exampleLine + 1,
includedSchemas)).append(",\n");
}
Expand Down
Loading