Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -624,6 +624,14 @@ public String toDefaultValue(Schema p) {
else
return "True";
}
// include fallback to example, default defined as server only
// example is not defined as server only
if (p.getExample() != null) {
if (p.getExample().toString().equalsIgnoreCase("false"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using Boolean.valueOf instead of using equalsIgnoreCase?

Copy link
Contributor Author

@spacether spacether Jan 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boolean.valueOf(string) will always return True or False,
so Boolean.valueOf("5") would return False.
When I pass in "5" as a boolean the code should return null. Code updated use equalsIgnoreCase to check for "true" and "false" otherwise return null.
I'd prefer matching on true false strings rather than truthy "0" "5" "1.234" "[1,2,3]" because truthiness differs from language.

equalsIgnoreCase lets us only look for a string match to true or false ignoring case.

Copy link
Member

@wing328 wing328 Jan 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I pass in "5" as a boolean the code should return null

If someone puts down 5 as the example value and assume it will be treated false in boolean, it's the user's issue (not something we need to handle) and I don't think it should return null (clearly there's a value defined)

We've seen spec using 1 or 0 to as example value for boolean type (not entirely correct as they should be using "true" or "false" instead) but still we want to able to handle these cases correctly as some languages (e.g. Perl) treat 1 as true.

Copy link
Contributor Author

@spacether spacether Jan 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coercing it to a boolean gives the user back some boolean which is usefull. I will change it to Boolean.valueOf. This behavior may surprise python users though because they will be using a python client with java casting of string truthiness.
"1" -> True in Python, false in Java
"0" -> True in Python, false in Java

return "False";
else
return "True";
}
} else if (ModelUtils.isDateSchema(p)) {
// TODO
} else if (ModelUtils.isDateTimeSchema(p)) {
Expand All @@ -632,17 +640,48 @@ public String toDefaultValue(Schema p) {
if (p.getDefault() != null) {
return p.getDefault().toString();
}
// default numbers are not yet returned by v2 spec openAPI results
// https://github.com/swagger-api/swagger-parser/issues/971
// include fallback to example, default defined as server only
// example is not defined as server only
if (p.getExample() != null) {
return p.getExample().toString();
}
} else if (ModelUtils.isIntegerSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
}
// default integers are not yet returned by v2 spec openAPI results
// https://github.com/swagger-api/swagger-parser/issues/971
// include fallback to example, default defined as server only
// example is not defined as server only
if (p.getExample() != null) {
return p.getExample().toString();
}
} else if (ModelUtils.isStringSchema(p)) {
if (p.getDefault() != null) {
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find())
return "'''" + p.getDefault() + "'''";
else
return "'" + p.getDefault() + "'";
}
// include fallback to example, default defined as server only
// example is not defined as server only
if (p.getExample() != null) {
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getExample()).find())
return "'''" + p.getExample() + "'''";
else
return "'" + p.getExample() + "'";
}
} else if (ModelUtils.isArraySchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
}
// include fallback to example, default defined as server only
// example is not defined as server only
if (p.getExample() != null) {
return p.getExample().toString();
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1574,3 +1574,63 @@ definitions:
sourceURI:
description: 'Test capitalization'
type: string
TypeHolderDefault:
type: object
required:
- string_item
- number_item
- integer_item
- bool_item
- array_item
properties:
string_item:
type: string
default: what
number_item:
type: number
default: 1.234
integer_item:
type: integer
default: -2
bool_item:
type: boolean
default: true
array_item:
type: array
items:
type: integer
default:
- 0
- 1
- 2
- 3
TypeHolderExample:
type: object
required:
- string_item
- number_item
- integer_item
- bool_item
- array_item
properties:
string_item:
type: string
example: what
number_item:
type: number
example: 1.234
integer_item:
type: integer
example: -2
bool_item:
type: boolean
example: true
array_item:
type: array
items:
type: integer
example:
- 0
- 1
- 2
- 3
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.4-SNAPSHOT
4.0.0-SNAPSHOT
4 changes: 2 additions & 2 deletions samples/client/petstore-security-test/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ from pprint import pprint

# create an instance of the API class
api_instance = petstore_api.FakeApi(petstore_api.ApiClient(configuration))
unknown_base_type = petstore_api.UNKNOWN_BASE_TYPE() # UNKNOWN_BASE_TYPE | (optional)
test_code_inject____end____rn_n_r = 'test_code_inject____end____rn_n_r_example' # str | To test code injection */ ' \\\" =end -- \\\\r\\\\n \\\\n \\\\r (optional)

try:
# To test code injection */ ' \" =end -- \\r\\n \\n \\r
api_instance.test_code_inject____end__rn_n_r(unknown_base_type=unknown_base_type)
api_instance.test_code_inject____end__rn_n_r(test_code_inject____end____rn_n_r=test_code_inject____end____rn_n_r)
except ApiException as e:
print("Exception when calling FakeApi->test_code_inject____end__rn_n_r: %s\n" % e)

Expand Down
10 changes: 5 additions & 5 deletions samples/client/petstore-security-test/python/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Method | HTTP request | Description


# **test_code_inject____end__rn_n_r**
> test_code_inject____end__rn_n_r(unknown_base_type=unknown_base_type)
> test_code_inject____end__rn_n_r(test_code_inject____end____rn_n_r=test_code_inject____end____rn_n_r)

To test code injection */ ' \" =end -- \\r\\n \\n \\r

Expand All @@ -24,11 +24,11 @@ from pprint import pprint

# create an instance of the API class
api_instance = petstore_api.FakeApi()
unknown_base_type = petstore_api.UNKNOWN_BASE_TYPE() # UNKNOWN_BASE_TYPE | (optional)
test_code_inject____end____rn_n_r = 'test_code_inject____end____rn_n_r_example' # str | To test code injection */ ' \\\" =end -- \\\\r\\\\n \\\\n \\\\r (optional)

try:
# To test code injection */ ' \" =end -- \\r\\n \\n \\r
api_instance.test_code_inject____end__rn_n_r(unknown_base_type=unknown_base_type)
api_instance.test_code_inject____end__rn_n_r(test_code_inject____end____rn_n_r=test_code_inject____end____rn_n_r)
except ApiException as e:
print("Exception when calling FakeApi->test_code_inject____end__rn_n_r: %s\n" % e)
```
Expand All @@ -37,7 +37,7 @@ except ApiException as e:

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**unknown_base_type** | [**UNKNOWN_BASE_TYPE**](UNKNOWN_BASE_TYPE.md)| | [optional]
**test_code_inject____end____rn_n_r** | **str**| To test code injection */ ' \\\" =end -- \\\\r\\\\n \\\\n \\\\r | [optional]

### Return type

Expand All @@ -49,7 +49,7 @@ No authorization required

### HTTP request headers

- **Content-Type**: application/json, */ \" =end --
- **Content-Type**: application/x-www-form-urlencoded, */ \" =end --
- **Accept**: Not defined

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_code_inject____end__rn_n_r(self, **kwargs): # noqa: E501
>>> result = thread.get()

:param async_req bool
:param UNKNOWN_BASE_TYPE unknown_base_type:
:param str test_code_inject____end____rn_n_r: To test code injection */ ' \\\" =end -- \\\\r\\\\n \\\\n \\\\r
:return: None
If the method is called asynchronously,
returns the request thread.
Expand All @@ -65,15 +65,15 @@ def test_code_inject____end__rn_n_r_with_http_info(self, **kwargs): # noqa: E50
>>> result = thread.get()

:param async_req bool
:param UNKNOWN_BASE_TYPE unknown_base_type:
:param str test_code_inject____end____rn_n_r: To test code injection */ ' \\\" =end -- \\\\r\\\\n \\\\n \\\\r
:return: None
If the method is called asynchronously,
returns the request thread.
"""

local_var_params = locals()

all_params = ['unknown_base_type'] # noqa: E501
all_params = ['test_code_inject____end____rn_n_r'] # noqa: E501
all_params.append('async_req')
all_params.append('_return_http_data_only')
all_params.append('_preload_content')
Expand All @@ -98,13 +98,13 @@ def test_code_inject____end__rn_n_r_with_http_info(self, **kwargs): # noqa: E50

form_params = []
local_var_files = {}
if 'test_code_inject____end____rn_n_r' in local_var_params:
form_params.append(('test code inject */ ' " =end -- \r\n \n \r', local_var_params['test_code_inject____end____rn_n_r'])) # noqa: E501

body_params = None
if 'unknown_base_type' in local_var_params:
body_params = local_var_params['unknown_base_type']
# HTTP header `Content-Type`
header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501
['application/json', '*/ \" =end -- ']) # noqa: E501
['application/x-www-form-urlencoded', '*/ \" =end -- ']) # noqa: E501

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ApiClient(object):
_pool = None

def __init__(self, configuration=None, header_name=None, header_value=None,
cookie=None, pool_threads=None):
cookie=None, pool_threads=1):
if configuration is None:
configuration = Configuration()
self.configuration = configuration
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/csharp/OpenAPIClient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ Class | Method | HTTP request | Description
- [Model.Return](docs/Return.md)
- [Model.SpecialModelName](docs/SpecialModelName.md)
- [Model.Tag](docs/Tag.md)
- [Model.TypeHolderDefault](docs/TypeHolderDefault.md)
- [Model.TypeHolderExample](docs/TypeHolderExample.md)
- [Model.User](docs/User.md)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Org.OpenAPITools.Model.TypeHolderDefault
## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**StringItem** | **string** | | [default to "what"]
**NumberItem** | **decimal?** | |
**IntegerItem** | **int?** | |
**BoolItem** | **bool?** | | [default to true]
**ArrayItem** | **List<int?>** | |

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Org.OpenAPITools.Model.TypeHolderExample
## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**StringItem** | **string** | |
**NumberItem** | **decimal?** | |
**IntegerItem** | **int?** | |
**BoolItem** | **bool?** | |
**ArrayItem** | **List<int?>** | |

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* OpenAPI Petstore
*
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
*
* Generated by: https://github.com/openapitools/openapi-generator.git
*/


using NUnit.Framework;

using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using Org.OpenAPITools.Api;
using Org.OpenAPITools.Model;
using Org.OpenAPITools.Client;
using System.Reflection;
using Newtonsoft.Json;

namespace Org.OpenAPITools.Test
{
/// <summary>
/// Class for testing TypeHolderDefault
/// </summary>
/// <remarks>
/// This file is automatically generated by OpenAPI Generator (https://openapi-generator.tech).
/// Please update the test case below to test the model.
/// </remarks>
[TestFixture]
public class TypeHolderDefaultTests
{
// TODO uncomment below to declare an instance variable for TypeHolderDefault
//private TypeHolderDefault instance;

/// <summary>
/// Setup before each test
/// </summary>
[SetUp]
public void Init()
{
// TODO uncomment below to create an instance of TypeHolderDefault
//instance = new TypeHolderDefault();
}

/// <summary>
/// Clean up after each test
/// </summary>
[TearDown]
public void Cleanup()
{

}

/// <summary>
/// Test an instance of TypeHolderDefault
/// </summary>
[Test]
public void TypeHolderDefaultInstanceTest()
{
// TODO uncomment below to test "IsInstanceOfType" TypeHolderDefault
//Assert.IsInstanceOfType<TypeHolderDefault> (instance, "variable 'instance' is a TypeHolderDefault");
}


/// <summary>
/// Test the property 'StringItem'
/// </summary>
[Test]
public void StringItemTest()
{
// TODO unit test for the property 'StringItem'
}
/// <summary>
/// Test the property 'NumberItem'
/// </summary>
[Test]
public void NumberItemTest()
{
// TODO unit test for the property 'NumberItem'
}
/// <summary>
/// Test the property 'IntegerItem'
/// </summary>
[Test]
public void IntegerItemTest()
{
// TODO unit test for the property 'IntegerItem'
}
/// <summary>
/// Test the property 'BoolItem'
/// </summary>
[Test]
public void BoolItemTest()
{
// TODO unit test for the property 'BoolItem'
}
/// <summary>
/// Test the property 'ArrayItem'
/// </summary>
[Test]
public void ArrayItemTest()
{
// TODO unit test for the property 'ArrayItem'
}

}

}
Loading