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

* handle nullable parameters with None added to allowed_values

* update samples

* spec for testing enum with null/nullable
This commit is contained in:
Matthieu Berthomé
2019-02-09 15:08:47 +01:00
committed by William Cheng
parent 0b10092262
commit cf6f10252d
3 changed files with 42 additions and 2 deletions
@@ -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(
@@ -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
@@ -20,13 +20,18 @@ package org.openapitools.codegen.python;
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
@@ -58,6 +63,18 @@ public class PythonClientCodegenTest {
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()
@@ -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