Skip to content

Commit 04fa53b

Browse files
authored
python: generate Pydantic v2 + typing complete code (OpenAPITools#16624)
* python: improve type generation with more specific typing * Annotate function parameters * Remove unused imports * remove unused files * remove temporary hack * remove lock file * fix Annotated import * support Python 3.7 * Regenerate code with typing-extensions * Fix setup.py * More Pydantic v2 compatibility * depend on pydantic v2 * fix client_echo tests * fix JSON serialization * Fix references * Skip circular dependency tests for now * Temporarily hide the "float" property The "float" property aliases the "float" type and completely breaks the model: all the properties that were "float" now become the type of the "float" property instead. * Fix errors * Import Literal from typing_extensions * Fix GitHub Action workflows * Fix Python 3.7 failure * Fix quotes * Apply suggestions from code review * Fix tests * split model imports from other modules imports * fix workflow * Comment the array unique items convertion, remove set translation * Replace alias usage
1 parent af352df commit 04fa53b

File tree

219 files changed

+3374
-1635
lines changed

Some content is hidden

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

219 files changed

+3374
-1635
lines changed

.github/workflows/samples-python-pydantic-v1-client-echo-api.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
matrix:
1616
sample:
1717
# clients
18-
- samples/client/echo_api/python
18+
- samples/client/echo_api/python-pydantic-v1/
1919
python-version:
2020
- "3.7"
2121
- "3.8"

.github/workflows/samples-python-pydantic-v1-petstore.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ jobs:
2121
- "3.10"
2222
- "3.11"
2323
sample:
24-
- samples/openapi3/client/petstore/python-aiohttp
25-
- samples/openapi3/client/petstore/python
24+
- samples/openapi3/client/petstore/python-pydantic-v1-aiohttp
25+
- samples/openapi3/client/petstore/python-pydantic-v1
2626
services:
2727
petstore-api:
2828
image: swaggerapi/petstore

bin/configs/python-echo-api.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
generatorName: python
22
outputDir: samples/client/echo_api/python
3-
inputSpec: modules/openapi-generator/src/test/resources/3_0/echo_api.yaml
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/python/echo_api.yaml
44
templateDir: modules/openapi-generator/src/main/resources/python
55
additionalProperties:
66
hideGenerationTimestamp: "true"

bin/generate-samples.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ if [[ ${#files[@]} -eq 1 && "${files[0]}" != *'*'* ]]; then
5353
java ${JAVA_OPTS} -jar "$executable" generate -c ${files[0]} ${args[@]}
5454
else
5555
echo "Please press CTRL+C to stop or the script will continue in 5 seconds."
56-
sleep 5
56+
#sleep 5
5757
if [ ${#files[@]} -eq 0 ]; then
5858
files=("${root}"/bin/configs/*.yaml)
5959
fi

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java

+76
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,82 @@ public void setHasMultipleTypes(boolean hasMultipleTypes) {
999999
this.hasMultipleTypes = hasMultipleTypes;
10001000
}
10011001

1002+
@Override
1003+
public boolean getIsFloat() {
1004+
return isFloat;
1005+
}
1006+
1007+
@Override
1008+
public void setIsFloat(boolean isFloat) {
1009+
this.isFloat = isFloat;
1010+
}
1011+
1012+
@Override
1013+
public boolean getIsDouble() {
1014+
return isDouble;
1015+
}
1016+
1017+
@Override
1018+
public void setIsDouble(boolean isDouble) {
1019+
this.isDouble = isDouble;
1020+
}
1021+
1022+
@Override
1023+
public boolean getIsInteger() {
1024+
return isInteger;
1025+
}
1026+
1027+
@Override
1028+
public void setIsInteger(boolean isInteger) {
1029+
this.isInteger = isInteger;
1030+
}
1031+
1032+
@Override
1033+
public boolean getIsLong() {
1034+
return isLong;
1035+
}
1036+
1037+
@Override
1038+
public void setIsLong(boolean isLong) {
1039+
this.isLong = isLong;
1040+
}
1041+
1042+
@Override
1043+
public boolean getIsBinary() {
1044+
return false;
1045+
}
1046+
1047+
@Override
1048+
public void setIsBinary(boolean isBinary) {}
1049+
1050+
@Override
1051+
public boolean getIsByteArray() {
1052+
return false;
1053+
}
1054+
1055+
@Override
1056+
public void setIsByteArray(boolean isByteArray) {}
1057+
1058+
@Override
1059+
public boolean getIsDecimal() {
1060+
return isDecimal;
1061+
}
1062+
1063+
@Override
1064+
public void setIsDecimal(boolean isDecimal) {
1065+
this.isDecimal = isDecimal;
1066+
}
1067+
1068+
@Override
1069+
public boolean getIsEnum() {
1070+
return isEnum;
1071+
}
1072+
1073+
@Override
1074+
public void setIsEnum(boolean isEnum) {
1075+
this.isEnum = isEnum;
1076+
}
1077+
10021078
@Override
10031079
public boolean equals(Object o) {
10041080
if (this == o) return true;

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java

+107-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717

1818
package org.openapitools.codegen;
1919

20-
import io.swagger.v3.oas.models.examples.Example;
20+
import java.util.ArrayList;
21+
import java.util.HashMap;
22+
import java.util.LinkedHashMap;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.Objects;
2126

22-
import java.util.*;
27+
import io.swagger.v3.oas.models.examples.Example;
2328

2429
/**
2530
* Describes a single operation parameter in the OAS specification.
@@ -997,5 +1002,105 @@ public boolean getSchemaIsFromAdditionalProperties() {
9971002
public void setSchemaIsFromAdditionalProperties(boolean schemaIsFromAdditionalProperties) {
9981003
this.schemaIsFromAdditionalProperties = schemaIsFromAdditionalProperties;
9991004
}
1005+
1006+
@Override
1007+
public String getDataType() {
1008+
return dataType;
1009+
}
1010+
1011+
@Override
1012+
public void setDataType(String dataType) {
1013+
this.dataType = dataType;
1014+
}
1015+
1016+
@Override
1017+
public boolean getIsFloat() {
1018+
return isFloat;
1019+
}
1020+
1021+
@Override
1022+
public void setIsFloat(boolean isFloat) {
1023+
this.isFloat = isFloat;
1024+
}
1025+
1026+
@Override
1027+
public boolean getIsDouble() {
1028+
return isDouble;
1029+
}
1030+
1031+
@Override
1032+
public void setIsDouble(boolean isDouble) {
1033+
this.isDouble = isDouble;
1034+
}
1035+
1036+
@Override
1037+
public boolean getIsInteger() {
1038+
return isInteger;
1039+
}
1040+
1041+
@Override
1042+
public void setIsInteger(boolean isInteger) {
1043+
this.isInteger = isInteger;
1044+
}
1045+
1046+
@Override
1047+
public boolean getIsLong() {
1048+
return isLong;
1049+
}
1050+
1051+
@Override
1052+
public void setIsLong(boolean isLong) {
1053+
this.isLong = isLong;
1054+
}
1055+
1056+
@Override
1057+
public boolean getIsBinary() {
1058+
return isBinary;
1059+
}
1060+
1061+
@Override
1062+
public void setIsBinary(boolean isBinary) {
1063+
this.isBinary = isBinary;
1064+
}
1065+
1066+
@Override
1067+
public boolean getIsByteArray() {
1068+
return isByteArray;
1069+
}
1070+
1071+
@Override
1072+
public void setIsByteArray(boolean isByteArray) {
1073+
this.isByteArray = isByteArray;
1074+
}
1075+
1076+
@Override
1077+
public boolean getIsDecimal() {
1078+
return isDecimal;
1079+
}
1080+
1081+
@Override
1082+
public void setIsDecimal(boolean isDecimal) {
1083+
this.isDecimal = isDecimal;
1084+
}
1085+
1086+
@Override
1087+
public boolean getIsUuid() {
1088+
return isUuid;
1089+
}
1090+
1091+
@Override
1092+
public void setIsUuid(boolean isUuid) {
1093+
this.isUuid = isUuid;
1094+
}
1095+
1096+
@Override
1097+
public boolean getIsEnum() {
1098+
return isEnum;
1099+
}
1100+
1101+
@Override
1102+
public void setIsEnum(boolean isEnum) {
1103+
this.isEnum = isEnum;
1104+
}
10001105
}
10011106

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenProperty.java

+94-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717

1818
package org.openapitools.codegen;
1919

20-
import java.util.*;
20+
import java.util.ArrayList;
21+
import java.util.HashMap;
22+
import java.util.LinkedHashMap;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.Objects;
2126

2227
public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperties {
2328
/**
@@ -317,10 +322,18 @@ public String getDataType() {
317322
return dataType;
318323
}
319324

325+
/**
326+
* @deprecated use {@link #setDataType()} instead.
327+
*/
328+
@Deprecated
320329
public void setDatatype(String datatype) {
321330
this.dataType = datatype;
322331
}
323332

333+
public void setDataType(String dataType) {
334+
this.dataType = dataType;
335+
}
336+
324337
public String getDatatypeWithEnum() {
325338
return datatypeWithEnum;
326339
}
@@ -1027,6 +1040,76 @@ public void setRequiredVarsMap(Map<String, CodegenProperty> requiredVarsMap) {
10271040
this.requiredVarsMap = requiredVarsMap;
10281041
}
10291042

1043+
@Override
1044+
public boolean getIsFloat() {
1045+
return isFloat;
1046+
}
1047+
1048+
@Override
1049+
public void setIsFloat(boolean isFloat) {
1050+
this.isFloat = isFloat;
1051+
}
1052+
1053+
@Override
1054+
public boolean getIsDouble() {
1055+
return isDouble;
1056+
}
1057+
1058+
@Override
1059+
public void setIsDouble(boolean isDouble) {
1060+
this.isDouble = isDouble;
1061+
}
1062+
1063+
@Override
1064+
public boolean getIsInteger() {
1065+
return isInteger;
1066+
}
1067+
1068+
@Override
1069+
public void setIsInteger(boolean isInteger) {
1070+
this.isInteger = isInteger;
1071+
}
1072+
1073+
@Override
1074+
public boolean getIsLong() {
1075+
return isLong;
1076+
}
1077+
1078+
@Override
1079+
public void setIsLong(boolean isLong) {
1080+
this.isLong = isLong;
1081+
}
1082+
1083+
@Override
1084+
public boolean getIsBinary() {
1085+
return isBinary;
1086+
}
1087+
1088+
@Override
1089+
public void setIsBinary(boolean isBinary) {
1090+
this.isBinary = isBinary;
1091+
}
1092+
1093+
@Override
1094+
public boolean getIsByteArray() {
1095+
return isByteArray;
1096+
}
1097+
1098+
@Override
1099+
public void setIsByteArray(boolean isByteArray) {
1100+
this.isByteArray = isByteArray;
1101+
}
1102+
1103+
@Override
1104+
public boolean getIsDecimal() {
1105+
return isDecimal;
1106+
}
1107+
1108+
@Override
1109+
public void setIsDecimal(boolean isDecimal) {
1110+
this.isDecimal = isDecimal;
1111+
}
1112+
10301113
/**
10311114
* Return true if it's an enum (inline or ref)
10321115
*
@@ -1036,6 +1119,16 @@ public boolean getIsEnumOrRef() {
10361119
return isEnum || isEnumRef;
10371120
}
10381121

1122+
@Override
1123+
public boolean getIsEnum() {
1124+
return isEnum;
1125+
}
1126+
1127+
@Override
1128+
public void setIsEnum(boolean isEnum) {
1129+
this.isEnum = isEnum;
1130+
}
1131+
10391132
@Override
10401133
public String toString() {
10411134
final StringBuilder sb = new StringBuilder("CodegenProperty{");

0 commit comments

Comments
 (0)