Skip to content

Commit

Permalink
python: generate Pydantic v2 + typing complete code (#16624)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
multani authored Sep 28, 2023
1 parent af352df commit 04fa53b
Show file tree
Hide file tree
Showing 219 changed files with 3,374 additions and 1,635 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
matrix:
sample:
# clients
- samples/client/echo_api/python
- samples/client/echo_api/python-pydantic-v1/
python-version:
- "3.7"
- "3.8"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/samples-python-pydantic-v1-petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
- "3.10"
- "3.11"
sample:
- samples/openapi3/client/petstore/python-aiohttp
- samples/openapi3/client/petstore/python
- samples/openapi3/client/petstore/python-pydantic-v1-aiohttp
- samples/openapi3/client/petstore/python-pydantic-v1
services:
petstore-api:
image: swaggerapi/petstore
Expand Down
2 changes: 1 addition & 1 deletion bin/configs/python-echo-api.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generatorName: python
outputDir: samples/client/echo_api/python
inputSpec: modules/openapi-generator/src/test/resources/3_0/echo_api.yaml
inputSpec: modules/openapi-generator/src/test/resources/3_0/python/echo_api.yaml
templateDir: modules/openapi-generator/src/main/resources/python
additionalProperties:
hideGenerationTimestamp: "true"
2 changes: 1 addition & 1 deletion bin/generate-samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if [[ ${#files[@]} -eq 1 && "${files[0]}" != *'*'* ]]; then
java ${JAVA_OPTS} -jar "$executable" generate -c ${files[0]} ${args[@]}
else
echo "Please press CTRL+C to stop or the script will continue in 5 seconds."
sleep 5
#sleep 5
if [ ${#files[@]} -eq 0 ]; then
files=("${root}"/bin/configs/*.yaml)
fi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,82 @@ public void setHasMultipleTypes(boolean hasMultipleTypes) {
this.hasMultipleTypes = hasMultipleTypes;
}

@Override
public boolean getIsFloat() {
return isFloat;
}

@Override
public void setIsFloat(boolean isFloat) {
this.isFloat = isFloat;
}

@Override
public boolean getIsDouble() {
return isDouble;
}

@Override
public void setIsDouble(boolean isDouble) {
this.isDouble = isDouble;
}

@Override
public boolean getIsInteger() {
return isInteger;
}

@Override
public void setIsInteger(boolean isInteger) {
this.isInteger = isInteger;
}

@Override
public boolean getIsLong() {
return isLong;
}

@Override
public void setIsLong(boolean isLong) {
this.isLong = isLong;
}

@Override
public boolean getIsBinary() {
return false;
}

@Override
public void setIsBinary(boolean isBinary) {}

@Override
public boolean getIsByteArray() {
return false;
}

@Override
public void setIsByteArray(boolean isByteArray) {}

@Override
public boolean getIsDecimal() {
return isDecimal;
}

@Override
public void setIsDecimal(boolean isDecimal) {
this.isDecimal = isDecimal;
}

@Override
public boolean getIsEnum() {
return isEnum;
}

@Override
public void setIsEnum(boolean isEnum) {
this.isEnum = isEnum;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@

package org.openapitools.codegen;

import io.swagger.v3.oas.models.examples.Example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import java.util.*;
import io.swagger.v3.oas.models.examples.Example;

/**
* Describes a single operation parameter in the OAS specification.
Expand Down Expand Up @@ -997,5 +1002,105 @@ public boolean getSchemaIsFromAdditionalProperties() {
public void setSchemaIsFromAdditionalProperties(boolean schemaIsFromAdditionalProperties) {
this.schemaIsFromAdditionalProperties = schemaIsFromAdditionalProperties;
}

@Override
public String getDataType() {
return dataType;
}

@Override
public void setDataType(String dataType) {
this.dataType = dataType;
}

@Override
public boolean getIsFloat() {
return isFloat;
}

@Override
public void setIsFloat(boolean isFloat) {
this.isFloat = isFloat;
}

@Override
public boolean getIsDouble() {
return isDouble;
}

@Override
public void setIsDouble(boolean isDouble) {
this.isDouble = isDouble;
}

@Override
public boolean getIsInteger() {
return isInteger;
}

@Override
public void setIsInteger(boolean isInteger) {
this.isInteger = isInteger;
}

@Override
public boolean getIsLong() {
return isLong;
}

@Override
public void setIsLong(boolean isLong) {
this.isLong = isLong;
}

@Override
public boolean getIsBinary() {
return isBinary;
}

@Override
public void setIsBinary(boolean isBinary) {
this.isBinary = isBinary;
}

@Override
public boolean getIsByteArray() {
return isByteArray;
}

@Override
public void setIsByteArray(boolean isByteArray) {
this.isByteArray = isByteArray;
}

@Override
public boolean getIsDecimal() {
return isDecimal;
}

@Override
public void setIsDecimal(boolean isDecimal) {
this.isDecimal = isDecimal;
}

@Override
public boolean getIsUuid() {
return isUuid;
}

@Override
public void setIsUuid(boolean isUuid) {
this.isUuid = isUuid;
}

@Override
public boolean getIsEnum() {
return isEnum;
}

@Override
public void setIsEnum(boolean isEnum) {
this.isEnum = isEnum;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

package org.openapitools.codegen;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperties {
/**
Expand Down Expand Up @@ -317,10 +322,18 @@ public String getDataType() {
return dataType;
}

/**
* @deprecated use {@link #setDataType()} instead.
*/
@Deprecated
public void setDatatype(String datatype) {
this.dataType = datatype;
}

public void setDataType(String dataType) {
this.dataType = dataType;
}

public String getDatatypeWithEnum() {
return datatypeWithEnum;
}
Expand Down Expand Up @@ -1027,6 +1040,76 @@ public void setRequiredVarsMap(Map<String, CodegenProperty> requiredVarsMap) {
this.requiredVarsMap = requiredVarsMap;
}

@Override
public boolean getIsFloat() {
return isFloat;
}

@Override
public void setIsFloat(boolean isFloat) {
this.isFloat = isFloat;
}

@Override
public boolean getIsDouble() {
return isDouble;
}

@Override
public void setIsDouble(boolean isDouble) {
this.isDouble = isDouble;
}

@Override
public boolean getIsInteger() {
return isInteger;
}

@Override
public void setIsInteger(boolean isInteger) {
this.isInteger = isInteger;
}

@Override
public boolean getIsLong() {
return isLong;
}

@Override
public void setIsLong(boolean isLong) {
this.isLong = isLong;
}

@Override
public boolean getIsBinary() {
return isBinary;
}

@Override
public void setIsBinary(boolean isBinary) {
this.isBinary = isBinary;
}

@Override
public boolean getIsByteArray() {
return isByteArray;
}

@Override
public void setIsByteArray(boolean isByteArray) {
this.isByteArray = isByteArray;
}

@Override
public boolean getIsDecimal() {
return isDecimal;
}

@Override
public void setIsDecimal(boolean isDecimal) {
this.isDecimal = isDecimal;
}

/**
* Return true if it's an enum (inline or ref)
*
Expand All @@ -1036,6 +1119,16 @@ public boolean getIsEnumOrRef() {
return isEnum || isEnumRef;
}

@Override
public boolean getIsEnum() {
return isEnum;
}

@Override
public void setIsEnum(boolean isEnum) {
this.isEnum = isEnum;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("CodegenProperty{");
Expand Down
Loading

0 comments on commit 04fa53b

Please sign in to comment.