Skip to content

Commit

Permalink
Adds inline composition examples to python-experimental (#11420)
Browse files Browse the repository at this point in the history
* Adds component with composition in property example

* Adds endpoint parameter examples

* Adds request body examples

* Adds inpline composition example in response body

* Fixes UNKNOWNBASETYPE import bug

* Updates allof inline schemas to be type object with minProperties

* Removes newline

* Adds test_ObjectWithInlineCompositionProperty

* Updates inline schema to be type string, adds partial test_inline_composition

* Fixes accept_content_types input value

* Adds test_ObjectWithInlineCompositionProperty, adds test_inline_composition and starts deserialization of multipart

* Fixes test_inline_composition, adds simple multipat/form-data deserialization
  • Loading branch information
spacether authored Feb 3, 2022
1 parent dc1df25 commit 4d9a500
Show file tree
Hide file tree
Showing 19 changed files with 1,370 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
* With this customization, we ensure that when schemas are passed to getSchemaType
* - if they have ref in them they are a model
* - if they do not have ref in them they are not a model
* and code is also customized to allow anyType request body schemas
*
* @param codegenParameter the body parameter
* @param name model schema ref key in components
Expand All @@ -936,7 +937,75 @@ protected void addBodyModelSchema(CodegenParameter codegenParameter, String name
forceSimpleRef = true;
}
}
super.addBodyModelSchema(codegenParameter, name, schema, imports, bodyParameterName, forceSimpleRef);

CodegenModel codegenModel = null;
if (StringUtils.isNotBlank(name)) {
schema.setName(name);
codegenModel = fromModel(name, schema);
}
if (codegenModel != null) {
codegenParameter.isModel = true;
}

if (codegenModel != null && (codegenModel.hasVars || forceSimpleRef)) {
if (StringUtils.isEmpty(bodyParameterName)) {
codegenParameter.baseName = codegenModel.classname;
} else {
codegenParameter.baseName = bodyParameterName;
}
codegenParameter.paramName = toParamName(codegenParameter.baseName);
codegenParameter.baseType = codegenModel.classname;
codegenParameter.dataType = getTypeDeclaration(codegenModel.classname);
codegenParameter.description = codegenModel.description;
codegenParameter.isNullable = codegenModel.isNullable;
imports.add(codegenParameter.baseType);
} else {
CodegenProperty codegenProperty = fromProperty("property", schema);

if (codegenProperty != null && codegenProperty.getComplexType() != null && codegenProperty.getComplexType().contains(" | ")) {
List<String> parts = Arrays.asList(codegenProperty.getComplexType().split(" \\| "));
imports.addAll(parts);
String codegenModelName = codegenProperty.getComplexType();
codegenParameter.baseName = codegenModelName;
codegenParameter.paramName = toParamName(codegenParameter.baseName);
codegenParameter.baseType = codegenParameter.baseName;
codegenParameter.dataType = getTypeDeclaration(codegenModelName);
codegenParameter.description = codegenProperty.getDescription();
codegenParameter.isNullable = codegenProperty.isNullable;
} else {
if (ModelUtils.isMapSchema(schema)) {// http body is map
LOGGER.error("Map should be supported. Please report to openapi-generator github repo about the issue.");
} else if (codegenProperty != null) {
String codegenModelName, codegenModelDescription;

if (codegenModel != null) {
codegenModelName = codegenModel.classname;
codegenModelDescription = codegenModel.description;
} else {
codegenModelName = "anyType";
codegenModelDescription = "";
}

if (StringUtils.isEmpty(bodyParameterName)) {
codegenParameter.baseName = codegenModelName;
} else {
codegenParameter.baseName = bodyParameterName;
}

codegenParameter.paramName = toParamName(codegenParameter.baseName);
codegenParameter.baseType = codegenModelName;
codegenParameter.dataType = getTypeDeclaration(codegenModelName);
codegenParameter.description = codegenModelDescription;

if (codegenProperty.complexType != null) {
imports.add(codegenProperty.complexType);
}
}
}

// set nullable
setParameterNullable(codegenParameter, codegenProperty);
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass
from decimal import Decimal
import enum
import email
import json
import os
import io
Expand Down Expand Up @@ -777,6 +778,20 @@ class OpenApiResponse:
else:
return response.data

@staticmethod
def __deserialize_multipart_form_data(
response: urllib3.HTTPResponse
) -> typing.Dict[str, typing.Any]:
msg = email.message_from_bytes(response.data)
return {
part.get_param("name", header="Content-Disposition"): part.get_payload(
decode=True
).decode(part.get_content_charset())
if part.get_content_charset()
else part.get_payload()
for part in msg.get_payload()
}

def deserialize(self, response: urllib3.HTTPResponse, configuration: Configuration) -> ApiResponse:
content_type = response.getheader('content-type')
deserialized_body = unset
Expand All @@ -786,6 +801,9 @@ class OpenApiResponse:
body_data = self.__deserialize_json(response)
elif content_type == 'application/octet-stream':
body_data = self.__deserialize_application_octet_stream(response)
elif content_type.startswith('multipart/form-data'):
body_data = self.__deserialize_multipart_form_data(response)
content_type = 'multipart/form-data'
else:
raise NotImplementedError('Deserialization of {} has not yet been implemented'.format(content_type))
body_schema = self.content[content_type].schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,60 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/HealthCheckResult'
/fake/inlineComposition/:
post:
tags:
- fake
summary: testing composed schemas at inline locations
operationId: inlineComposition
parameters:
- name: compositionAtRoot
in: query
schema:
allOf:
- type: string
minLength: 1
- name: compositionInProperty
in: query
schema:
type: object
properties:
someProp:
allOf:
- type: string
minLength: 1
requestBody:
content:
application/json:
schema:
allOf:
- type: string
minLength: 1
multipart/form-data:
schema:
type: object
properties:
someProp:
allOf:
- type: string
minLength: 1
responses:
200:
description: success
content:
application/json:
schema:
allOf:
- type: string
minLength: 1
multipart/form-data:
schema:
type: object
properties:
someProp:
allOf:
- type: string
minLength: 1
servers:
- url: 'http://{server}.swagger.io:{port}/v2'
description: petstore server
Expand Down Expand Up @@ -2681,4 +2735,11 @@ components:
type: string
format: number
cost:
$ref: '#/components/schemas/Money'
$ref: '#/components/schemas/Money'
ObjectWithInlineCompositionProperty:
type: object
properties:
someProp:
allOf:
- type: string
minLength: 1
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ docs/ComposedNumber.md
docs/ComposedObject.md
docs/ComposedOneOfDifferentTypes.md
docs/ComposedString.md
docs/CompositionInProperty.md
docs/Currency.md
docs/DanishPig.md
docs/DateTimeTest.md
Expand Down Expand Up @@ -95,6 +96,7 @@ docs/ObjectInterface.md
docs/ObjectModelWithRefProps.md
docs/ObjectWithDecimalProperties.md
docs/ObjectWithDifficultlyNamedProps.md
docs/ObjectWithInlineCompositionProperty.md
docs/ObjectWithValidations.md
docs/Order.md
docs/ParentPet.md
Expand Down Expand Up @@ -179,6 +181,7 @@ petstore_api/model/composed_number.py
petstore_api/model/composed_object.py
petstore_api/model/composed_one_of_different_types.py
petstore_api/model/composed_string.py
petstore_api/model/composition_in_property.py
petstore_api/model/currency.py
petstore_api/model/danish_pig.py
petstore_api/model/date_time_test.py
Expand Down Expand Up @@ -230,6 +233,7 @@ petstore_api/model/object_interface.py
petstore_api/model/object_model_with_ref_props.py
petstore_api/model/object_with_decimal_properties.py
petstore_api/model/object_with_difficultly_named_props.py
petstore_api/model/object_with_inline_composition_property.py
petstore_api/model/object_with_validations.py
petstore_api/model/order.py
petstore_api/model/parent_pet.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Class | Method | HTTP request | Description
*FakeApi* | [**fake_health_get**](docs/FakeApi.md#fake_health_get) | **GET** /fake/health | Health check endpoint
*FakeApi* | [**group_parameters**](docs/FakeApi.md#group_parameters) | **DELETE** /fake | Fake endpoint to test group parameters (optional)
*FakeApi* | [**inline_additional_properties**](docs/FakeApi.md#inline_additional_properties) | **POST** /fake/inline-additionalProperties | test inline additionalProperties
*FakeApi* | [**inline_composition**](docs/FakeApi.md#inline_composition) | **POST** /fake/inlineComposition/ | testing composed schemas at inline locations
*FakeApi* | [**json_form_data**](docs/FakeApi.md#json_form_data) | **GET** /fake/jsonFormData | test json serialization of form data
*FakeApi* | [**mammal**](docs/FakeApi.md#mammal) | **POST** /fake/refs/mammal |
*FakeApi* | [**number_with_validations**](docs/FakeApi.md#number_with_validations) | **POST** /fake/refs/number |
Expand Down Expand Up @@ -172,6 +173,7 @@ Class | Method | HTTP request | Description
- [ComposedObject](docs/ComposedObject.md)
- [ComposedOneOfDifferentTypes](docs/ComposedOneOfDifferentTypes.md)
- [ComposedString](docs/ComposedString.md)
- [CompositionInProperty](docs/CompositionInProperty.md)
- [Currency](docs/Currency.md)
- [DanishPig](docs/DanishPig.md)
- [DateTimeTest](docs/DateTimeTest.md)
Expand Down Expand Up @@ -223,6 +225,7 @@ Class | Method | HTTP request | Description
- [ObjectModelWithRefProps](docs/ObjectModelWithRefProps.md)
- [ObjectWithDecimalProperties](docs/ObjectWithDecimalProperties.md)
- [ObjectWithDifficultlyNamedProps](docs/ObjectWithDifficultlyNamedProps.md)
- [ObjectWithInlineCompositionProperty](docs/ObjectWithInlineCompositionProperty.md)
- [ObjectWithValidations](docs/ObjectWithValidations.md)
- [Order](docs/Order.md)
- [ParentPet](docs/ParentPet.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# CompositionInProperty

#### Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**someProp** | **object** | | [optional]
**any string name** | **bool, date, datetime, dict, float, int, list, str, none_type** | any string name can be used but the value must be the correct type | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Loading

0 comments on commit 4d9a500

Please sign in to comment.