Skip to content

[Python] handle nullable parameters with None added to allowed_values #2034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class {{classname}}(object):
{{/isNullable}}
{{#isEnum}}
{{#isContainer}}
allowed_values = [{{#allowableValues}}{{#values}}{{#items.isString}}"{{/items.isString}}{{{this}}}{{#items.isString}}"{{/items.isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501
allowed_values = [{{#isNullable}}None,{{/isNullable}}{{#allowableValues}}{{#values}}{{#items.isString}}"{{/items.isString}}{{{this}}}{{#items.isString}}"{{/items.isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501
{{#isListContainer}}
if not set({{{name}}}).issubset(set(allowed_values)):
raise ValueError(
Expand All @@ -126,7 +126,7 @@ class {{classname}}(object):
{{/isMapContainer}}
{{/isContainer}}
{{^isContainer}}
allowed_values = [{{#allowableValues}}{{#values}}{{#isString}}"{{/isString}}{{{this}}}{{#isString}}"{{/isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501
allowed_values = [{{#isNullable}}None,{{/isNullable}}{{#allowableValues}}{{#values}}{{#isString}}"{{/isString}}{{{this}}}{{#isString}}"{{/isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501
if {{{name}}} not in allowed_values:
raise ValueError(
"Invalid value for `{{{name}}}` ({0}), must be one of {1}" # noqa: E501
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.languages.PythonClientCodegen;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Arrays;

public class PythonClientCodegenTest {

@Test
Expand Down Expand Up @@ -58,6 +63,18 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
}

@Test(description = "test enum null/nullable patterns")
public void testEnumNull() {
final OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/issue_1997.yaml", null, new ParseOptions()).getOpenAPI();


StringSchema prop = (StringSchema) openAPI.getComponents().getSchemas().get("Type").getProperties().get("prop");
ArrayList<Object> expected = new ArrayList<>(Arrays.asList("A", "B", "C"));
assert prop.getNullable();
assert prop.getEnum().equals(expected);
}

@Test(description = "test regex patterns")
public void testRegularExpressionOpenAPISchemaVersion3() {
final OpenAPI openAPI = new OpenAPIParser()
Expand Down
23 changes: 23 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue_1997.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
openapi: 3.0.1
paths:
/test:
summary: test
info:
description: test
version: 1.0.0
title: test
components:
schemas:
Type:
properties:
prop:
nullable: true
enum: [A, B, C]
type: string
prop2:
enum: [A, B, C, null]
type: string
prop3:
nullable: true
enum: []
type: string
4 changes: 4 additions & 0 deletions samples/client/petstore/python-asyncio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint


# create an instance of the API class
api_instance = petstore_api.AnotherFakeApi(petstore_api.ApiClient(configuration))
body = petstore_api.Client() # Client | client model
Expand Down Expand Up @@ -158,16 +159,19 @@ Class | Method | HTTP request | Description
- **API key parameter name**: api_key
- **Location**: HTTP header


## api_key_query

- **Type**: API key
- **API key parameter name**: api_key_query
- **Location**: URL query string


## http_basic_test

- **Type**: HTTP basic authentication


## petstore_auth

- **Type**: OAuth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ To test special tags
To test special tags and operation ID starting with number

### Example

```python
from __future__ import print_function
import time
Expand Down
14 changes: 13 additions & 1 deletion samples/client/petstore/python-asyncio/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ creates an XmlItem
this route creates an XmlItem

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -74,6 +75,7 @@ No authorization required
Test serialization of outer boolean types

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -121,6 +123,7 @@ No authorization required
Test serialization of object with outer number type

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -168,6 +171,7 @@ No authorization required
Test serialization of outer number types

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -215,6 +219,7 @@ No authorization required
Test serialization of outer string types

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -262,6 +267,7 @@ No authorization required
For this test, the body for this request much reference a schema named `File`.

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -306,6 +312,7 @@ No authorization required


### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -354,6 +361,7 @@ To test \"client\" model
To test \"client\" model

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -403,7 +411,7 @@ Fake endpoint for testing various parameters 假端點 偽のエンドポイン

### Example

* Basic Authentication (http_basic_test):
* Basic Authentication (http_basic_test):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -481,6 +489,7 @@ To test enum parameters
To test enum parameters

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -542,6 +551,7 @@ Fake endpoint to test group parameters (optional)
Fake endpoint to test group parameters (optional)

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -597,6 +607,7 @@ No authorization required
test inline additionalProperties

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -642,6 +653,7 @@ No authorization required
test json serialization of form data

### Example

```python
from __future__ import print_function
import time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ To test class name in snake case

### Example

* Api Key Authentication (api_key_query):
* Api Key Authentication (api_key_query):
```python
from __future__ import print_function
import time
Expand Down
18 changes: 9 additions & 9 deletions samples/client/petstore/python-asyncio/docs/PetApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Add a new pet to the store

### Example

* OAuth Authentication (petstore_auth):
* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -72,7 +72,7 @@ Deletes a pet

### Example

* OAuth Authentication (petstore_auth):
* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -126,7 +126,7 @@ Multiple status values can be provided with comma separated strings

### Example

* OAuth Authentication (petstore_auth):
* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -179,7 +179,7 @@ Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3

### Example

* OAuth Authentication (petstore_auth):
* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -232,7 +232,7 @@ Returns a single pet

### Example

* Api Key Authentication (api_key):
* Api Key Authentication (api_key):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -285,7 +285,7 @@ Update an existing pet

### Example

* OAuth Authentication (petstore_auth):
* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -335,7 +335,7 @@ Updates a pet in the store with form data

### Example

* OAuth Authentication (petstore_auth):
* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -389,7 +389,7 @@ uploads an image

### Example

* OAuth Authentication (petstore_auth):
* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -444,7 +444,7 @@ uploads an image (required)

### Example

* OAuth Authentication (petstore_auth):
* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down
5 changes: 4 additions & 1 deletion samples/client/petstore/python-asyncio/docs/StoreApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Delete purchase order by ID
For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -66,7 +67,7 @@ Returns a map of status codes to quantities

### Example

* Api Key Authentication (api_key):
* Api Key Authentication (api_key):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -116,6 +117,7 @@ Find purchase order by ID
For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions

### Example

```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -162,6 +164,7 @@ No authorization required
Place an order for a pet

### Example

```python
from __future__ import print_function
import time
Expand Down
Loading