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

Commit 038d5f8

Browse files
authored
v3 adds mypy (#187)
* Adds lib for types * Fixes type errors in format.py * Adds query_params_suffix to fix type error * Fixes unset type * Fixes type errors in schema_configuration * Adds needed type info in validation.py * Fixes more type errors in validation.py * Adds U+T binding to fix type errors * Improves return type of _get_new_instance_without_conversion * Changes some typing.Type to type * Adds validate_base_ in Schema * Uses Schema.validate_base in api_client * Removes clashing validate overloads from schemas classes * Adds more missing types in schema.py * Adds needed type hint * Fixes some api_client types * Fixes type error in update_params_for_auth * Fixes type errors with vars write_file and new_file * FIxes type error with serialize methods for header classes * Fixes type error with None detection * Type error another none fix * Fixes type error by using literal for key_prefix in get_security_requirement_object * Fixes 2 type errors by using literals in get_server_url * Adds another validate_base signature to fix type errors in dictschema validate invocations * Improves type hints for DictSchema * Fixes type error with tuple detection * Fixes broken python tests by having composed dict schemas use the Schema base class * Eliminates incompatible sequence overloads * Fixes 1 type error * Fixes another type error * Fixes tuple type error * Fixes type error for path_to_schemas * Updates path_to_schemas to store one schema * Adds _instances type * Fixes more type errors * Fixes another type error * Removes unneeded overload * Detects and handles addProps=False and only one property * Removes one sequence usage in template * Removes another sequence usage * Relaces last sequence in templates * Fixes type errors by fixing get_security_requirement_object literal input * Adds exception raising for statuses in operation handling code, reduces type errors * Fixes assertion error, adds storage of error status codes for operation * Adds error status codes to python * Fixes operation return type * Fixes get_server_url invocation, adds literals * Renames all ApiResponse classes to ApiResponse, uses them in operation response * Fixes type of headers_schema * Removes dataclass from OpenApiResponse * Removes unneeded dataclasses from OpenApiResponse and classes it uses * Fixes skip_deserialization overload * Prevents parameter collisions * Adds array output class tuple generic type * Removes typeddict from responses * Adds missing elipses for tuple output type * Adds validate_base sequence back in * Removes getitem from list schemas * Removes typeddict from header classes * Changes order to int float * Do not generate validate with number schema + enum * Controls when composed schema validate methods are written, fixes type errors * Adds type(None) to types definition, fixes type errors * Turns off getitem, adds optional props * Writes property methods * Changes additional_properties to get_additional_property * Adds get_property method implementation * Changes template mode to get_property * Changes property to overloads * Has overloads use elipses * Handles the case where there is one required or optional property * Adds get_additional_property casting * Only cast individual property outputs * Ignores pyright reportWildcardImportFromLibrary * Fixes array self ref type hint * Makes T param bound to sequences, fixes type errors * Adds required and optional keys * Adds unset return for optional property values when there is only one property * Changes name variable to key * Removes unused templates * Adds required property returns in get_property * Fixes get_property implementation * Improves get_property implementation * Changes method name to get_additional_property_ * Adds @Property back * Convers ref properties back into getters * Converts get_property back into get_x methods * Fixes python tests * Adds unset returning in get_additional_property_ * Adds unset output for many cases * Adds unset types on all except for array and object models * Adds array unset return value * Adds unset types for optional map * Writes properties with unmodifed names, only writes them if they are valid * Fixes python tests * Adds mypy to petstore test * Fixes type error * FIxes last mypy errors * Adds py.typed, improves server imports * Adds __all__ definitions * Fixes unset type setting on optional returns * Fixes additiona prperties return type * Adds unset case for binary output * Fixes validate return types * Adds mapValueSchema * Refines types of SchemaDict values, supresses mypy response body type error * Adds get_response method in OpenApiResponse classes * Fixes java test * Samples regenerated * Fixes unit test sample, samples regenerated
1 parent a8b84b6 commit 038d5f8

File tree

1,456 files changed

+19803
-15445
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,456 files changed

+19803
-15445
lines changed

docs/generators/python.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3535
| Type/Alias | Instantiated By |
3636
| ---------- | --------------- |
3737
|array|tuple|
38-
|boolean|schemas.BoolClass|
39-
|integer|decimal.Decimal|
40-
|null|schemas.NoneClass|
41-
|number|decimal.Decimal|
42-
|object|frozendict.frozendict|
38+
|boolean|bool|
39+
|integer|int|
40+
|null|None|
41+
|number|typing.Union[float, int]|
42+
|object|immutabledict.immutabledict|
4343
|string|str|
4444

4545

@@ -85,9 +85,9 @@ These options may be applied as additional-properties (cli) or configOptions (pl
8585
<li>float</li>
8686
<li>for</li>
8787
<li>from</li>
88-
<li>frozendict</li>
8988
<li>global</li>
9089
<li>if</li>
90+
<li>immutabledict</li>
9191
<li>import</li>
9292
<li>in</li>
9393
<li>int</li>

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/DefaultCodegen.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,6 +2338,20 @@ public CodegenSchema fromSchema(Schema p, String sourceJsonPath, String currentJ
23382338
}
23392339
}
23402340
// end of properties that need to be ordered to set correct camelCase jsonPathPieces
2341+
CodegenSchema additionalProperties = property.additionalProperties;
2342+
LinkedHashMapWithContext<CodegenKey, CodegenSchema> properties = property.properties;
2343+
if (additionalProperties != null || properties != null) {
2344+
CodegenSchema mapValueSchema = new CodegenSchema();
2345+
if (additionalProperties != null) {
2346+
mapValueSchema = mapValueSchema.add(additionalProperties);
2347+
}
2348+
if (properties != null) {
2349+
for (CodegenSchema prop: properties.values()) {
2350+
mapValueSchema = prop.add(mapValueSchema);
2351+
}
2352+
}
2353+
property.mapValueSchema = mapValueSchema;
2354+
}
23412355

23422356
if (currentJsonPath != null) {
23432357
String[] pathPieces = currentJsonPath.split("/");
@@ -2514,7 +2528,10 @@ public CodegenOperation fromOperation(Operation operation, String jsonPath) {
25142528
TreeMap<String, CodegenResponse> nonDefaultResponses = null;
25152529
TreeMap<Integer, CodegenResponse> wildcardCodeResponses = null;
25162530
TreeMap<Integer, CodegenResponse> statusCodeResponses = null;
2517-
boolean hasErrorResponseObject = false;
2531+
LinkedHashSet<String> errorStatusCodes = null;
2532+
LinkedHashSet<Integer> errorWildcardStatusCodes = null;
2533+
LinkedHashSet<String> nonErrorStatusCodes = null;
2534+
LinkedHashSet<Integer> nonErrorWildcardStatusCodes = null;
25182535
if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
25192536
responses = new TreeMap<>();
25202537
for (Map.Entry<String, ApiResponse> operationGetResponsesEntry : operation.getResponses().entrySet()) {
@@ -2545,13 +2562,17 @@ public CodegenOperation fromOperation(Operation operation, String jsonPath) {
25452562
}
25462563
int firstNumber = Integer.parseInt(key.substring(0, 1));
25472564
wildcardCodeResponses.put(firstNumber, r);
2548-
if (firstNumber > 3 && r.content != null) {
2549-
for (CodegenMediaType cm: r.content.values()) {
2550-
if (cm.schema != null) {
2551-
hasErrorResponseObject = true;
2552-
break;
2553-
}
2565+
if (firstNumber > 3) {
2566+
// store error response code whether on not it has a body
2567+
if (errorWildcardStatusCodes == null) {
2568+
errorWildcardStatusCodes = new LinkedHashSet<>();
25542569
}
2570+
errorWildcardStatusCodes.add(firstNumber);
2571+
} else {
2572+
if (nonErrorWildcardStatusCodes == null) {
2573+
nonErrorWildcardStatusCodes = new LinkedHashSet<>();
2574+
}
2575+
nonErrorWildcardStatusCodes.add(firstNumber);
25552576
}
25562577
continue;
25572578
}
@@ -2560,13 +2581,17 @@ public CodegenOperation fromOperation(Operation operation, String jsonPath) {
25602581
}
25612582
int statusCode = Integer.parseInt(key);
25622583
statusCodeResponses.put(statusCode, r);
2563-
if (statusCode > 299 && r.content != null) {
2564-
for (CodegenMediaType cm: r.content.values()) {
2565-
if (cm.schema != null) {
2566-
hasErrorResponseObject = true;
2567-
break;
2568-
}
2584+
if (statusCode > 399) {
2585+
// store error response code whether on not it has a body
2586+
if (errorStatusCodes == null) {
2587+
errorStatusCodes = new LinkedHashSet<>();
2588+
}
2589+
errorStatusCodes.add(key);
2590+
} else {
2591+
if (nonErrorStatusCodes == null) {
2592+
nonErrorStatusCodes = new LinkedHashSet<>();
25692593
}
2594+
nonErrorStatusCodes.add(key);
25702595
}
25712596
}
25722597

@@ -2729,7 +2754,10 @@ else if (one.required)
27292754

27302755
return new CodegenOperation(
27312756
deprecated,
2732-
hasErrorResponseObject,
2757+
nonErrorStatusCodes,
2758+
nonErrorWildcardStatusCodes,
2759+
errorStatusCodes,
2760+
errorWildcardStatusCodes,
27332761
summary,
27342762
unescapedDescription,
27352763
description,

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/DefaultGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ private void generatePathItem(List<File> files, CodegenKey pathKey, CodegenPathI
455455
endpointMap.put("pathItem", pathItem);
456456
endpointMap.put("httpMethod", httpMethod);
457457
endpointMap.put("security", security);
458+
endpointMap.put("path", pathKey);
458459
generateXs(files, operationJsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.OPERATION, CodegenConstants.APIS, endpointMap, true);
459460

460461
// operation docs

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/languages/PythonClientCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ public void processOpts() {
675675
supportingFiles.add(new SupportingFile("migration_2_0_0.hbs", "", "migration_2_0_0.md"));
676676
supportingFiles.add(new SupportingFile("migration_other_python_generators.hbs", "", "migration_other_python_generators.md"));
677677
supportingFiles.add(new SupportingFile("__init__package.hbs", packagePath(), "__init__.py"));
678+
supportingFiles.add(new SupportingFile("__init__.hbs", packagePath(), "py.typed"));
678679

679680
if (!generateSourceCodeOnly) {
680681
supportingFiles.add(new SupportingFile("tox.hbs", "", "tox.ini"));
@@ -727,6 +728,7 @@ public void processOpts() {
727728
supportingFiles.add(new SupportingFile("schemas/schema.hbs", packagePath() + File.separator + "schemas", "schema.py"));
728729
supportingFiles.add(new SupportingFile("schemas/schemas.hbs", packagePath() + File.separator + "schemas", "schemas.py"));
729730
supportingFiles.add(new SupportingFile("schemas/format.hbs", packagePath() + File.separator + "schemas", "format.py"));
731+
supportingFiles.add(new SupportingFile("schemas/original_immutabledict.hbs", packagePath() + File.separator + "schemas", "original_immutabledict.py"));
730732
supportingFiles.add(new SupportingFile("security_schemes.hbs", packagePath(), "security_schemes.py"));
731733
supportingFiles.add(new SupportingFile("server.hbs", packagePath(), "server.py"));
732734

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/model/CodegenOperation.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

2626
public class CodegenOperation {
2727
public final Boolean deprecated;
28-
public final boolean hasErrorResponseObject; // if 4xx, 5xx responses have at least one error object defined
28+
public final LinkedHashSet<String> nonErrorStatusCodes; // values like 201
29+
public final LinkedHashSet<Integer> nonErrorWildcardStatusCodes; // values like 2 for @2XX
30+
public final LinkedHashSet<String> errorStatusCodes; // values like 401
31+
public final LinkedHashSet<Integer> errorWildcardStatusCodes; // values like 4 for 4XX
2932
public final String summary, unescapedDescription, description;
3033
public final LinkedHashSet<String> produces;
3134
public final List<CodegenServer> servers;
@@ -55,9 +58,12 @@ public class CodegenOperation {
5558
public final CodegenKey operationId;
5659
public final CodegenKey jsonPathPiece;
5760

58-
public CodegenOperation(Boolean deprecated, boolean hasErrorResponseObject, String summary, String unescapedDescription, String description, LinkedHashSet<String> produces, List<CodegenServer> servers, CodegenRequestBody requestBody, List<CodegenSchema> requestBodySchemas, List<CodegenParameter> allParams, List<CodegenParameter> pathParams, CodegenSchema pathParameters, List<CodegenParameter> queryParams, CodegenSchema queryParameters, List<CodegenParameter> headerParams, CodegenSchema headerParameters, List<CodegenParameter> cookieParams, CodegenSchema cookieParameters, boolean hasRequiredParamOrBody, boolean hasOptionalParamOrBody, List<HashMap<String, CodegenSecurityRequirementValue>> security, Map<String, CodegenTag> tags, TreeMap<String, CodegenResponse> responses, TreeMap<Integer, CodegenResponse> statusCodeResponses, TreeMap<Integer, CodegenResponse> wildcardCodeResponses, TreeMap<String, CodegenResponse> nonDefaultResponses, CodegenResponse defaultResponse, List<CodegenCallback> callbacks, ExternalDocumentation externalDocs, Map<String, Object> vendorExtensions, CodegenKey operationId, CodegenKey jsonPathPiece) {
61+
public CodegenOperation(Boolean deprecated, LinkedHashSet<String> nonErrorStatusCodes, LinkedHashSet<Integer> nonErrorWildcardStatusCodes, LinkedHashSet<String> errorStatusCodes, LinkedHashSet<Integer> errorWildcardStatusCodes, String summary, String unescapedDescription, String description, LinkedHashSet<String> produces, List<CodegenServer> servers, CodegenRequestBody requestBody, List<CodegenSchema> requestBodySchemas, List<CodegenParameter> allParams, List<CodegenParameter> pathParams, CodegenSchema pathParameters, List<CodegenParameter> queryParams, CodegenSchema queryParameters, List<CodegenParameter> headerParams, CodegenSchema headerParameters, List<CodegenParameter> cookieParams, CodegenSchema cookieParameters, boolean hasRequiredParamOrBody, boolean hasOptionalParamOrBody, List<HashMap<String, CodegenSecurityRequirementValue>> security, Map<String, CodegenTag> tags, TreeMap<String, CodegenResponse> responses, TreeMap<Integer, CodegenResponse> statusCodeResponses, TreeMap<Integer, CodegenResponse> wildcardCodeResponses, TreeMap<String, CodegenResponse> nonDefaultResponses, CodegenResponse defaultResponse, List<CodegenCallback> callbacks, ExternalDocumentation externalDocs, Map<String, Object> vendorExtensions, CodegenKey operationId, CodegenKey jsonPathPiece) {
5962
this.deprecated = deprecated;
60-
this.hasErrorResponseObject = hasErrorResponseObject;
63+
this.nonErrorStatusCodes = nonErrorStatusCodes;
64+
this.nonErrorWildcardStatusCodes = nonErrorWildcardStatusCodes;
65+
this.errorStatusCodes = errorStatusCodes;
66+
this.errorWildcardStatusCodes = errorWildcardStatusCodes;
6167
this.summary = summary;
6268
this.unescapedDescription = unescapedDescription;
6369
this.description = description;

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/model/CodegenResponse.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ public class CodegenResponse {
3131
public final TreeSet<String> imports;
3232
public final boolean componentModule;
3333

34+
public CodegenResponse getSelfOrDeepestRef() {
35+
if (refInfo == null) {
36+
return this;
37+
}
38+
CodegenResponse refObject = refInfo.ref;
39+
while (refObject.refInfo != null) {
40+
refObject = refObject.refInfo.ref;
41+
}
42+
return refObject;
43+
}
44+
3445
public CodegenResponse(CodegenKey jsonPathPiece, Map<String, CodegenHeader> headers, CodegenSchema headersObjectSchema, String description, Map<String, Object> vendorExtensions, LinkedHashMap<CodegenKey, CodegenMediaType> content, CodegenRefInfo<CodegenResponse> refInfo, TreeSet<String> imports, boolean componentModule) {
3546
this.jsonPathPiece = jsonPathPiece;
3647
this.headers = headers;

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/model/CodegenSchema.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class CodegenSchema {
8181
public boolean isBooleanSchemaFalse; // supports boolean schemas
8282

8383
// Extra needed fields
84+
public CodegenSchema mapValueSchema;
8485
public boolean componentModule;
8586
public TreeSet<String> imports;
8687
public CodegenKey jsonPathPiece;
@@ -97,6 +98,7 @@ public class CodegenSchema {
9798
* used in getAllSchemas to write type definitions for allOfType/anyOfType/oneOfType/propertiesType
9899
*/
99100
public String instanceType;
101+
// used to store the expanded schemas that define a codegenschema in code file
100102
private ArrayList<CodegenSchema> allSchemas = null;
101103

102104
public boolean hasValidation() {
@@ -134,6 +136,32 @@ public CodegenSchema getSelfOrDeepestRef() {
134136
return refObject;
135137
}
136138

139+
public CodegenSchema add(CodegenSchema other) {
140+
CodegenSchema newSchema = new CodegenSchema();
141+
if (other == null) {
142+
newSchema.types = types;
143+
return newSchema;
144+
}
145+
if (refInfo != null || oneOf != null || anyOf != null || allOf != null || not != null) {
146+
return null;
147+
}
148+
if (other.refInfo != null || other.oneOf != null || other.anyOf != null || other.allOf != null || other.not != null) {
149+
return null;
150+
}
151+
if (types == null) {
152+
newSchema.types = other.types;
153+
return newSchema;
154+
}
155+
if (other.types == null) {
156+
newSchema.types = types;
157+
return newSchema;
158+
}
159+
LinkedHashSet<String> interSectionTypes = new LinkedHashSet<>(types);
160+
interSectionTypes.retainAll(other.types);
161+
newSchema.types = interSectionTypes;
162+
return newSchema;
163+
}
164+
137165
public boolean hasAnyRefs() {
138166
// todo cache this, also pass in sourceJsonPath because one is looking for external refs
139167
if (refInfo != null) {
@@ -329,6 +357,8 @@ private void getAllSchemas(ArrayList<CodegenSchema> schemasBeforeImports, ArrayL
329357
mapOut.requiredProperties = requiredProperties;
330358
mapOut.additionalProperties = additionalProperties;
331359
mapOut.mapOutputJsonPathPiece = mapOutputJsonPathPiece;
360+
mapOut.properties = properties;
361+
mapOut.mapValueSchema = mapValueSchema;
332362
// inputs needed for Schema validate invocation in new method
333363
mapOut.mapInputJsonPathPiece = mapInputJsonPathPiece;
334364
mapOut.jsonPathPiece = jsonPathPiece;
@@ -358,6 +388,8 @@ private void getAllSchemas(ArrayList<CodegenSchema> schemasBeforeImports, ArrayL
358388
mapOut.optionalProperties = optionalProperties;
359389
mapOut.additionalProperties = additionalProperties;
360390
mapOut.mapOutputJsonPathPiece = mapOutputJsonPathPiece;
391+
mapOut.properties = properties;
392+
mapOut.mapValueSchema = mapValueSchema;
361393
// inputs needed for Schema validate invocation in new method
362394
mapOut.mapInputJsonPathPiece = mapInputJsonPathPiece;
363395
mapOut.jsonPathPiece = jsonPathPiece;
@@ -405,8 +437,10 @@ private void getAllSchemas(ArrayList<CodegenSchema> schemasBeforeImports, ArrayL
405437
mapOut.instanceType = "propertiesOutputType";
406438
mapOut.optionalProperties = optionalProperties;
407439
mapOut.requiredProperties = requiredProperties;
440+
mapOut.properties = properties;
408441
mapOut.additionalProperties = additionalProperties;
409442
mapOut.mapOutputJsonPathPiece = mapOutputJsonPathPiece;
443+
mapOut.mapValueSchema = mapValueSchema;
410444
// inputs needed for Schema validate invocation in new method
411445
mapOut.mapInputJsonPathPiece = mapInputJsonPathPiece;
412446
mapOut.jsonPathPiece = jsonPathPiece;

modules/openapi-json-schema-generator/src/main/resources/python/_helper_required_libraries.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
{{#if tornado}}
1313
{{#if quoted}}"{{/if}}tornado >= 4.2{{#if quoted}}",{{/if}}
1414
{{/if}}
15+
{{#if quoted}}"{{/if}}types-python-dateutil{{#if quoted}}",{{/if}}
16+
{{#if quoted}}"{{/if}}types-urllib3{{#if quoted}}",{{/if}}
1517
{{#if quoted}}"{{/if}}typing_extensions ~= 4.5.0{{#if quoted}}",{{/if}}
1618
{{#if quoted}}"{{/if}}urllib3 ~= 2.0.a3{{#if quoted}}",{{/if}}

0 commit comments

Comments
 (0)