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

Commit 70676c0

Browse files
committed
Fixes 3 more python tests
1 parent 60b922f commit 70676c0

File tree

986 files changed

+17480
-24017
lines changed

Some content is hidden

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

986 files changed

+17480
-24017
lines changed

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

+143-41
Large diffs are not rendered by default.

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,10 @@ private void generateContent(List<File> files, LinkedHashMap<CodegenKey, Codegen
581581
String templateFile = contentTypeEntry.getKey();
582582
String outputFile = contentTypeEntry.getValue();
583583
String outputFilepath = config.getFilepath(contentTypeJsonPath) + File.separatorChar + outputFile;
584+
HashMap<String, Object> contentTypeTemplateData = new HashMap<>();
585+
contentTypeTemplateData.put("schema", schema);
584586
try {
585-
File written = processTemplateToFile(new HashMap<>(), templateFile, outputFilepath, true, CodegenConstants.CONTENT);
587+
File written = processTemplateToFile(contentTypeTemplateData, templateFile, outputFilepath, true, CodegenConstants.CONTENT);
586588
if (written != null) {
587589
files.add(written);
588590
if (config.isEnablePostProcessFile() && !dryRun) {
@@ -602,8 +604,10 @@ private void generateContent(List<File> files, LinkedHashMap<CodegenKey, Codegen
602604
String contentTemplateFile = contentEntry.getKey();
603605
String outputFile = contentEntry.getValue();
604606
String outputFilepath = config.getFilepath(contentJsonPath) + File.separatorChar + outputFile;
607+
HashMap<String, Object> contentTemplateData = new HashMap<>();
608+
contentTemplateData.put("content", content);
605609
try {
606-
File written = processTemplateToFile(new HashMap<>(), contentTemplateFile, outputFilepath, true, CodegenConstants.CONTENT);
610+
File written = processTemplateToFile(contentTemplateData, contentTemplateFile, outputFilepath, true, CodegenConstants.CONTENT);
607611
if (written != null) {
608612
files.add(written);
609613
if (config.isEnablePostProcessFile() && !dryRun) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public AbstractPythonCodegen() {
6969
"False", "async", "await",
7070
// imports, imports_schema_types.handlebars, include these to prevent name collision
7171
"datetime", "decimal", "functools", "io", "re",
72-
"typing", "typing_extensions", "uuid", "frozendict", "schemas"
72+
"typing", "typing_extensions", "uuid", "immutabledict", "schemas"
7373
));
7474

7575
languageSpecificPrimitives.clear();

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

+76-17
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public PythonClientCodegen() {
257257
"return", "def", "for", "lambda", "try", "self", "nonlocal", "None", "True",
258258
"False", "async", "await",
259259
// types
260-
"float", "int", "str", "bool", "dict", "frozendict", "list", "tuple"));
260+
"float", "int", "str", "bool", "dict", "immutabledict", "list", "tuple"));
261261

262262
regexModifiers = new HashMap<>();
263263
regexModifiers.put('i', "IGNORECASE");
@@ -310,11 +310,11 @@ public PythonClientCodegen() {
310310
GlobalSettings.setProperty("x-disallow-additional-properties-if-not-present", "false");
311311

312312
// this tells users what openapi types turn in to
313-
instantiationTypes.put("object", "frozendict.frozendict");
313+
instantiationTypes.put("object", "immutabledict.immutabledict");
314314
instantiationTypes.put("array", "tuple");
315315
instantiationTypes.put("string", "str");
316-
instantiationTypes.put("number", "decimal.Decimal");
317-
instantiationTypes.put("integer", "decimal.Decimal");
316+
instantiationTypes.put("number", "typing.Union[float, int]");
317+
instantiationTypes.put("integer", "int");
318318
instantiationTypes.put("boolean", "bool");
319319
instantiationTypes.put("null", "None");
320320

@@ -592,13 +592,13 @@ public void processOpts() {
592592
jsonPathTemplateFiles.put(
593593
CodegenConstants.JSON_PATH_LOCATION_TYPE.CONTENT,
594594
new HashMap<String, String>() {{
595-
put("__init__.hbs", File.separatorChar + "__init__.py");
595+
put("__init__content.hbs", File.separatorChar + "__init__.py");
596596
}}
597597
);
598598
jsonPathTemplateFiles.put(
599599
CodegenConstants.JSON_PATH_LOCATION_TYPE.CONTENT_TYPE,
600600
new HashMap<String, String>() {{
601-
put("__init__.hbs", File.separatorChar + "__init__.py");
601+
put("__init__content_type.hbs", File.separatorChar + "__init__.py");
602602
}}
603603
);
604604

@@ -723,7 +723,10 @@ public void processOpts() {
723723
supportingFiles.add(new SupportingFile("api_response.hbs", packagePath(), "api_response.py"));
724724
supportingFiles.add(new SupportingFile("rest.hbs", packagePath(), "rest.py"));
725725
supportingFiles.add(new SupportingFile("schemas/__init__.hbs", packagePath() + File.separator + "schemas", "__init__.py"));
726-
supportingFiles.add(new SupportingFile("schemas/json_schema_types.hbs", packagePath() + File.separator + "schemas", "json_schema_types.py"));
726+
supportingFiles.add(new SupportingFile("schemas/validation.hbs", packagePath() + File.separator + "schemas", "validation.py"));
727+
supportingFiles.add(new SupportingFile("schemas/schema.hbs", packagePath() + File.separator + "schemas", "schema.py"));
728+
supportingFiles.add(new SupportingFile("schemas/schemas.hbs", packagePath() + File.separator + "schemas", "schemas.py"));
729+
supportingFiles.add(new SupportingFile("schemas/format.hbs", packagePath() + File.separator + "schemas", "format.py"));
727730
supportingFiles.add(new SupportingFile("security_schemes.hbs", packagePath(), "security_schemes.py"));
728731
supportingFiles.add(new SupportingFile("server.hbs", packagePath(), "server.py"));
729732

@@ -1859,15 +1862,6 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> dat
18591862
return data;
18601863
}
18611864

1862-
@Override
1863-
protected String getImport(String className, CodegenSchema schema) {
1864-
if (className == null) {
1865-
return "from " + packageName() + ".components.schema import " + schema.refInfo.refModule;
1866-
}
1867-
String[] classPieces = className.split("\\.");
1868-
return "from " + packageName() + ".components.schema import " + classPieces[0];
1869-
}
1870-
18711865
@Override
18721866
protected String getRefClassWithModule(String ref, String sourceJsonPath) {
18731867
String refModule = toRefModule(ref, sourceJsonPath, "schemas");
@@ -1962,14 +1956,79 @@ private String toSchemaRefClass(String ref, String sourceJsonPath) {
19621956
if (sourceJsonPath != null && ref.startsWith(sourceJsonPath + "/")) {
19631957
// internal in-schema reference, no import needed
19641958
// TODO handle this in the future
1965-
return null;
1959+
if (getFilepath(sourceJsonPath).equals(getFilepath(ref))) {
1960+
// TODO ensure that getFilepath returns the same file for somePath/get/QueryParameters
1961+
// TODO ensure that getFilepath returns the same file for schemas/SomeSchema...
1962+
return null;
1963+
}
19661964
}
19671965
// reference is external, import needed
19681966
// module info is stored in refModule
19691967
if (ref.startsWith("#/components/schemas/") && refPieces.length == 4) {
19701968
String schemaName = refPieces[3];
19711969
return toModelName(schemaName, ref);
19721970
}
1971+
if (ref.startsWith("#/components/parameters/")) {
1972+
if (refPieces.length == 5) {
1973+
// #/components/parameters/PathUserName/schema
1974+
String schemaName = refPieces[4];
1975+
return toModelName(schemaName, ref);
1976+
}
1977+
if (refPieces.length == 7) {
1978+
// #/components/parameters/PathUserName/content/mediaType/schema
1979+
String schemaName = refPieces[6];
1980+
return toModelName(schemaName, ref);
1981+
}
1982+
}
1983+
if (ref.startsWith("#/components/headers/")) {
1984+
if (refPieces.length == 5) {
1985+
// #/components/headers/Int32JsonContentTypeHeader/schema
1986+
String schemaName = refPieces[4];
1987+
return toModelName(schemaName, ref);
1988+
}
1989+
if (refPieces.length == 7) {
1990+
// #/components/headers/Int32JsonContentTypeHeader/content/application~1json/schema
1991+
String schemaName = refPieces[6];
1992+
return toModelName(schemaName, ref);
1993+
}
1994+
}
1995+
if (ref.startsWith("#/components/responses/")) {
1996+
if (refPieces.length == 7) {
1997+
// #/components/responses/SuccessInlineContentAndHeader/headers/someHeader/schema
1998+
String schemaName = refPieces[6];
1999+
return toModelName(schemaName, ref);
2000+
}
2001+
if (refPieces.length == 9) {
2002+
// #/components/responses/SuccessInlineContentAndHeader/headers/someHeader/content/application~1json/schema
2003+
String schemaName = refPieces[8];
2004+
return toModelName(schemaName, ref);
2005+
}
2006+
}
2007+
if (ref.startsWith("#/paths/")) {
2008+
if (refPieces.length == 7) {
2009+
// #/paths/~1pet~1{petId}/get/parameters/0/schema
2010+
String schemaName = refPieces[6];
2011+
return toModelName(schemaName, ref);
2012+
} else if (refPieces.length == 8) {
2013+
// #/paths/~1user~1login/get/responses/200/headers/X-Rate-Limit/schema
2014+
String schemaName = refPieces[7];
2015+
return toModelName(schemaName, ref);
2016+
} else if (refPieces.length == 9) {
2017+
// #/paths/~1pet~1{petId}/get/parameters/0/content/mediaType/schema
2018+
// #/paths/~1user~1login/get/responses/200/headers/X-Rate-Limit/schema
2019+
String schemaName = refPieces[8];
2020+
return toModelName(schemaName, ref);
2021+
} else if (refPieces.length == 10) {
2022+
// #/paths/~1user~1login/get/responses/200/headers/X-Rate-Limit/content/application~1json/schema
2023+
String schemaName = refPieces[9];
2024+
return toModelName(schemaName, ref);
2025+
} else if (refPieces.length == 11) {
2026+
// #/paths/~1user~1login/get/responses/200/headers/X-Rate-Limit/content/application~1json/schema
2027+
String schemaName = refPieces[10];
2028+
return toModelName(schemaName, ref);
2029+
}
2030+
2031+
}
19732032
return null;
19742033
}
19752034

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

+25
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,31 @@ public CodegenHeader(String description, String unescapedDescription, String exa
6262
this.refInfo = refInfo;
6363
}
6464

65+
public CodegenHeader getSelfOrDeepestRef() {
66+
CodegenHeader selfOrRefParam = this;
67+
while (selfOrRefParam.refInfo != null) {
68+
selfOrRefParam = selfOrRefParam.refInfo.ref;
69+
}
70+
return selfOrRefParam;
71+
}
72+
73+
public String getSchemaJsonPath() {
74+
CodegenHeader selfOrRefParam = this;
75+
while (selfOrRefParam.refInfo != null) {
76+
selfOrRefParam = selfOrRefParam.refInfo.ref;
77+
}
78+
// parameter is now de-referenced
79+
CodegenSchema schema = null;
80+
if (selfOrRefParam.schema != null) {
81+
schema = selfOrRefParam.schema;
82+
} else {
83+
CodegenKey contentTypeKey = selfOrRefParam.content.keySet().iterator().next();
84+
schema = selfOrRefParam.content.get(contentTypeKey).schema;
85+
}
86+
schema = schema.getSelfOrDeepestRef();
87+
return schema.jsonPath;
88+
}
89+
6590
@Override
6691
public int hashCode() {
6792
return Objects.hash(jsonPathPiece, explode, description, unescapedDescription, style, example, vendorExtensions, deprecated, required, schema, content, refInfo, imports, componentModule);

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

+9-53
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public class CodegenOperation {
3232
public final CodegenRequestBody requestBody;
3333
public final List<CodegenParameter> allParams;
3434
public final List<CodegenParameter> pathParams;
35-
public final boolean hasRequiredPathParams;
35+
public final CodegenSchema pathParameters;
3636
public final List<CodegenParameter> queryParams;
37-
public final boolean hasRequiredQueryParams;
37+
public final CodegenSchema queryParameters;
3838
public final List<CodegenParameter> headerParams;
39-
public final boolean hasRequiredHeaderParams;
39+
public final CodegenSchema headerParameters;
4040
public final List<CodegenParameter> cookieParams;
41-
public final boolean hasRequiredCookieParams;
41+
public final CodegenSchema cookieParameters;
4242
public final boolean hasRequiredParamOrBody;
4343
public final boolean hasOptionalParamOrBody;
4444
public final List<HashMap<String, CodegenSecurityRequirementValue>> security;
@@ -54,7 +54,7 @@ public class CodegenOperation {
5454
public final CodegenKey operationId;
5555
public final CodegenKey jsonPathPiece;
5656

57-
public CodegenOperation(Boolean deprecated, boolean hasErrorResponseObject, String summary, String unescapedDescription, String description, LinkedHashSet<String> produces, List<CodegenServer> servers, CodegenRequestBody requestBody, List<CodegenParameter> allParams, List<CodegenParameter> pathParams, List<CodegenParameter> queryParams, List<CodegenParameter> headerParams, List<CodegenParameter> cookieParams, 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) {
57+
public CodegenOperation(Boolean deprecated, boolean hasErrorResponseObject, String summary, String unescapedDescription, String description, LinkedHashSet<String> produces, List<CodegenServer> servers, CodegenRequestBody requestBody, 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) {
5858
this.deprecated = deprecated;
5959
this.hasErrorResponseObject = hasErrorResponseObject;
6060
this.summary = summary;
@@ -65,57 +65,13 @@ public CodegenOperation(Boolean deprecated, boolean hasErrorResponseObject, Stri
6565
this.requestBody = requestBody;
6666
this.allParams = allParams;
6767
this.pathParams = pathParams;
68-
if (pathParams == null) {
69-
this.hasRequiredPathParams = false;
70-
} else {
71-
boolean val = false;
72-
for (CodegenParameter p: pathParams) {
73-
if (Boolean.TRUE.equals(p.required)) {
74-
val = true;
75-
break;
76-
}
77-
}
78-
this.hasRequiredPathParams = val;
79-
}
68+
this.pathParameters = pathParameters;
8069
this.queryParams = queryParams;
81-
if (queryParams == null) {
82-
this.hasRequiredQueryParams = false;
83-
} else {
84-
boolean val = false;
85-
for (CodegenParameter p: queryParams) {
86-
if (Boolean.TRUE.equals(p.required)) {
87-
val = true;
88-
break;
89-
}
90-
}
91-
this.hasRequiredQueryParams = val;
92-
}
70+
this.queryParameters = queryParameters;
9371
this.headerParams = headerParams;
94-
if (headerParams == null) {
95-
this.hasRequiredHeaderParams = false;
96-
} else {
97-
boolean val = false;
98-
for (CodegenParameter p: headerParams) {
99-
if (Boolean.TRUE.equals(p.required)) {
100-
val = true;
101-
break;
102-
}
103-
}
104-
this.hasRequiredHeaderParams = val;
105-
}
72+
this.headerParameters = headerParameters;
10673
this.cookieParams = cookieParams;
107-
if (cookieParams == null) {
108-
this.hasRequiredCookieParams = false;
109-
} else {
110-
boolean val = false;
111-
for (CodegenParameter p: cookieParams) {
112-
if (Boolean.TRUE.equals(p.required)) {
113-
val = true;
114-
break;
115-
}
116-
}
117-
this.hasRequiredCookieParams = val;
118-
}
74+
this.cookieParameters = cookieParameters;
11975
this.hasRequiredParamOrBody = hasRequiredParamOrBody;
12076
this.hasOptionalParamOrBody = hasOptionalParamOrBody;
12177
this.security = security;

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

+25
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,31 @@ public class CodegenParameter {
4747
public final CodegenRefInfo<CodegenParameter> refInfo;
4848
public final Boolean allowReserved;
4949

50+
public CodegenParameter getSelfOrDeepestRef() {
51+
CodegenParameter selfOrRefParam = this;
52+
while (selfOrRefParam.refInfo != null) {
53+
selfOrRefParam = selfOrRefParam.refInfo.ref;
54+
}
55+
return selfOrRefParam;
56+
}
57+
58+
public String getSchemaJsonPath() {
59+
CodegenParameter selfOrRefParam = this;
60+
while (selfOrRefParam.refInfo != null) {
61+
selfOrRefParam = selfOrRefParam.refInfo.ref;
62+
}
63+
// parameter is now de-referenced
64+
CodegenSchema schema = null;
65+
if (selfOrRefParam.schema != null) {
66+
schema = selfOrRefParam.schema;
67+
} else {
68+
CodegenKey contentTypeKey = selfOrRefParam.content.keySet().iterator().next();
69+
schema = selfOrRefParam.content.get(contentTypeKey).schema;
70+
}
71+
schema = schema.getSelfOrDeepestRef();
72+
return schema.jsonPath;
73+
}
74+
5075
public CodegenParameter(String description, String unescapedDescription, String example, Map<String, Object> vendorExtensions, Boolean required, LinkedHashMap<CodegenKey, CodegenMediaType> content, Set<String> imports, boolean componentModule, CodegenKey jsonPathPiece, Boolean explode, String style, Boolean deprecated, CodegenSchema schema, String in, Boolean allowEmptyValue, String name, CodegenRefInfo<CodegenParameter> refInfo, Boolean allowReserved) {
5176
this.description = description;
5277
this.unescapedDescription = unescapedDescription;

0 commit comments

Comments
 (0)