forked from loafle/openapi-generator-original
[Go] Fix up json check regexp and support vendor-specific mime types (#2322)
* [Go] Fix up mimetype regexps * [Go] Support vendor-specific JSON mime types * [Go] Tidy up unmarshalling content type checks * [Go] Generate sample clients
This commit is contained in:
committed by
William Cheng
parent
f2ff473155
commit
065bbc7bdb
@@ -25,8 +25,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
jsonCheck = regexp.MustCompile("(?i:[application|text]/json)")
|
||||
xmlCheck = regexp.MustCompile("(?i:[application|text]/xml)")
|
||||
jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`)
|
||||
xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`)
|
||||
)
|
||||
|
||||
// APIClient manages communication with the {{appName}} API v{{version}}
|
||||
@@ -317,17 +317,18 @@ func (c *APIClient) prepareRequest(
|
||||
}
|
||||
|
||||
func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
|
||||
if strings.Contains(contentType, "application/xml") {
|
||||
if err = xml.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
} else if strings.Contains(contentType, "application/json") {
|
||||
if err = json.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
if xmlCheck.MatchString(contentType) {
|
||||
if err = xml.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if jsonCheck.MatchString(contentType) {
|
||||
if err = json.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New("undefined response type")
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
3.3.1-SNAPSHOT
|
||||
4.0.0-SNAPSHOT
|
||||
@@ -29,7 +29,7 @@ paths:
|
||||
operationId: testCodeInject */ ' " =end -- \r\n \n \r
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
application/x-www-form-urlencoded:
|
||||
schema:
|
||||
properties:
|
||||
test code inject */ ' " =end -- \r\n \n \r:
|
||||
|
||||
@@ -31,11 +31,11 @@ FakeApiService To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
|
||||
To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
|
||||
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
|
||||
* @param optional nil or *TestCodeInjectEndRnNROpts - Optional Parameters:
|
||||
* @param "UNKNOWNBASETYPE" (optional.Interface of UNKNOWN_BASE_TYPE) -
|
||||
* @param "TestCodeInjectEndRnNR" (optional.String) - To test code injection *_/ ' \\\" =end -- \\\\r\\\\n \\\\n \\\\r
|
||||
*/
|
||||
|
||||
type TestCodeInjectEndRnNROpts struct {
|
||||
UNKNOWNBASETYPE optional.Interface
|
||||
TestCodeInjectEndRnNR optional.String
|
||||
}
|
||||
|
||||
func (a *FakeApiService) TestCodeInjectEndRnNR(ctx context.Context, localVarOptionals *TestCodeInjectEndRnNROpts) (*http.Response, error) {
|
||||
@@ -55,7 +55,7 @@ func (a *FakeApiService) TestCodeInjectEndRnNR(ctx context.Context, localVarOpti
|
||||
localVarFormParams := url.Values{}
|
||||
|
||||
// to determine the Content-Type header
|
||||
localVarHttpContentTypes := []string{"application/json", "*_/ ' =end -- "}
|
||||
localVarHttpContentTypes := []string{"application/x-www-form-urlencoded", "*_/ ' =end -- "}
|
||||
|
||||
// set Content-Type header
|
||||
localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
|
||||
@@ -71,15 +71,9 @@ func (a *FakeApiService) TestCodeInjectEndRnNR(ctx context.Context, localVarOpti
|
||||
if localVarHttpHeaderAccept != "" {
|
||||
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
|
||||
}
|
||||
// body params
|
||||
if localVarOptionals != nil && localVarOptionals.UNKNOWNBASETYPE.IsSet() {
|
||||
localVarOptionalUNKNOWNBASETYPE, localVarOptionalUNKNOWNBASETYPEok := localVarOptionals.UNKNOWNBASETYPE.Value().(UNKNOWN_BASE_TYPE)
|
||||
if !localVarOptionalUNKNOWNBASETYPEok {
|
||||
return nil, reportError("uNKNOWNBASETYPE should be UNKNOWN_BASE_TYPE")
|
||||
}
|
||||
localVarPostBody = &localVarOptionalUNKNOWNBASETYPE
|
||||
if localVarOptionals != nil && localVarOptionals.TestCodeInjectEndRnNR.IsSet() {
|
||||
localVarFormParams.Add("test code inject */ ' " =end -- \r\n \n \r", parameterToString(localVarOptionals.TestCodeInjectEndRnNR.Value(), ""))
|
||||
}
|
||||
|
||||
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -34,8 +34,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
jsonCheck = regexp.MustCompile("(?i:[application|text]/json)")
|
||||
xmlCheck = regexp.MustCompile("(?i:[application|text]/xml)")
|
||||
jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`)
|
||||
xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`)
|
||||
)
|
||||
|
||||
// APIClient manages communication with the OpenAPI Petstore *_/ ' \" =end -- \\r\\n \\n \\r API v1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
|
||||
@@ -314,17 +314,18 @@ func (c *APIClient) prepareRequest(
|
||||
}
|
||||
|
||||
func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
|
||||
if strings.Contains(contentType, "application/xml") {
|
||||
if err = xml.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
} else if strings.Contains(contentType, "application/json") {
|
||||
if err = json.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
if xmlCheck.MatchString(contentType) {
|
||||
if err = xml.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if jsonCheck.MatchString(contentType) {
|
||||
if err = json.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New("undefined response type")
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ Optional parameters are passed through a pointer to a TestCodeInjectEndRnNROpts
|
||||
|
||||
Name | Type | Description | Notes
|
||||
------------- | ------------- | ------------- | -------------
|
||||
**uNKNOWNBASETYPE** | [**optional.Interface of UNKNOWN_BASE_TYPE**](UNKNOWN_BASE_TYPE.md)| |
|
||||
**testCodeInjectEndRnNR** | **optional.String**| To test code injection *_/ ' \\\" =end -- \\\\r\\\\n \\\\n \\\\r |
|
||||
|
||||
### Return type
|
||||
|
||||
@@ -37,7 +37,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)
|
||||
|
||||
@@ -33,8 +33,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
jsonCheck = regexp.MustCompile("(?i:[application|text]/json)")
|
||||
xmlCheck = regexp.MustCompile("(?i:[application|text]/xml)")
|
||||
jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`)
|
||||
xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`)
|
||||
)
|
||||
|
||||
// APIClient manages communication with the OpenAPI Petstore API v1.0.0
|
||||
@@ -328,17 +328,18 @@ func (c *APIClient) prepareRequest(
|
||||
}
|
||||
|
||||
func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
|
||||
if strings.Contains(contentType, "application/xml") {
|
||||
if err = xml.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
} else if strings.Contains(contentType, "application/json") {
|
||||
if err = json.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
if xmlCheck.MatchString(contentType) {
|
||||
if err = xml.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if jsonCheck.MatchString(contentType) {
|
||||
if err = json.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New("undefined response type")
|
||||
}
|
||||
|
||||
|
||||
@@ -108,6 +108,9 @@ Class | Method | HTTP request | Description
|
||||
- [Order](docs/Order.md)
|
||||
- [OuterComposite](docs/OuterComposite.md)
|
||||
- [OuterEnum](docs/OuterEnum.md)
|
||||
- [OuterEnumDefaultValue](docs/OuterEnumDefaultValue.md)
|
||||
- [OuterEnumInteger](docs/OuterEnumInteger.md)
|
||||
- [OuterEnumIntegerDefaultValue](docs/OuterEnumIntegerDefaultValue.md)
|
||||
- [Pet](docs/Pet.md)
|
||||
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
|
||||
- [Return](docs/Return.md)
|
||||
@@ -140,6 +143,17 @@ auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
|
||||
})
|
||||
r, err := client.Service.Operation(auth, args)
|
||||
```
|
||||
## bearer_test
|
||||
- **Type**: HTTP basic authentication
|
||||
|
||||
Example
|
||||
```golang
|
||||
auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
|
||||
UserName: "username",
|
||||
Password: "password",
|
||||
})
|
||||
r, err := client.Service.Operation(auth, args)
|
||||
```
|
||||
## http_basic_test
|
||||
- **Type**: HTTP basic authentication
|
||||
|
||||
|
||||
@@ -81,6 +81,9 @@ paths:
|
||||
summary: Update an existing pet
|
||||
tags:
|
||||
- pet
|
||||
servers:
|
||||
- url: http://petstore.swagger.io/v2
|
||||
- url: http://path-server-test.petstore.local/v2
|
||||
/pet/findByStatus:
|
||||
get:
|
||||
description: Multiple status values can be provided with comma separated strings
|
||||
@@ -650,6 +653,8 @@ paths:
|
||||
responses:
|
||||
400:
|
||||
description: Someting wrong
|
||||
security:
|
||||
- bearer_test: []
|
||||
summary: Fake endpoint to test group parameters (optional)
|
||||
tags:
|
||||
- fake
|
||||
@@ -1460,6 +1465,7 @@ components:
|
||||
format: date-time
|
||||
type: string
|
||||
uuid:
|
||||
example: 72f98069-206d-4f12-9f12-3d1e525a8e84
|
||||
format: uuid
|
||||
type: string
|
||||
password:
|
||||
@@ -1516,6 +1522,12 @@ components:
|
||||
type: number
|
||||
outerEnum:
|
||||
$ref: '#/components/schemas/OuterEnum'
|
||||
outerEnumInteger:
|
||||
$ref: '#/components/schemas/OuterEnumInteger'
|
||||
outerEnumDefaultValue:
|
||||
$ref: '#/components/schemas/OuterEnumDefaultValue'
|
||||
outerEnumIntegerDefaultValue:
|
||||
$ref: '#/components/schemas/OuterEnumIntegerDefaultValue'
|
||||
required:
|
||||
- enum_string_required
|
||||
type: object
|
||||
@@ -1676,10 +1688,32 @@ components:
|
||||
- approved
|
||||
- delivered
|
||||
type: string
|
||||
OuterEnumInteger:
|
||||
enum:
|
||||
- 0
|
||||
- 1
|
||||
- 2
|
||||
format: int32
|
||||
type: integer
|
||||
OuterEnumDefaultValue:
|
||||
default: placed
|
||||
enum:
|
||||
- placed
|
||||
- approved
|
||||
- delivered
|
||||
type: string
|
||||
OuterEnumIntegerDefaultValue:
|
||||
default: 0
|
||||
enum:
|
||||
- 0
|
||||
- 1
|
||||
- 2
|
||||
format: int32
|
||||
type: integer
|
||||
OuterComposite:
|
||||
example:
|
||||
my_string: my_string
|
||||
my_number: 0.80082819046101150206595775671303272247314453125
|
||||
my_number: 0.8008281904610115
|
||||
my_boolean: true
|
||||
properties:
|
||||
my_number:
|
||||
@@ -1903,3 +1937,7 @@ components:
|
||||
http_basic_test:
|
||||
scheme: basic
|
||||
type: http
|
||||
bearer_test:
|
||||
bearerFormat: JWT
|
||||
scheme: bearer
|
||||
type: http
|
||||
|
||||
@@ -79,7 +79,6 @@ func (a *FakeClassnameTags123ApiService) TestClassname(ctx context.Context, clie
|
||||
localVarQueryParams.Add("api_key_query", key)
|
||||
}
|
||||
}
|
||||
|
||||
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
|
||||
if err != nil {
|
||||
return localVarReturnValue, nil, err
|
||||
|
||||
@@ -400,7 +400,6 @@ func (a *PetApiService) GetPetById(ctx context.Context, petId int64) (Pet, *http
|
||||
localVarHeaderParams["api_key"] = key
|
||||
}
|
||||
}
|
||||
|
||||
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
|
||||
if err != nil {
|
||||
return localVarReturnValue, nil, err
|
||||
|
||||
@@ -144,7 +144,6 @@ func (a *StoreApiService) GetInventory(ctx context.Context) (map[string]int32, *
|
||||
localVarHeaderParams["api_key"] = key
|
||||
}
|
||||
}
|
||||
|
||||
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
|
||||
if err != nil {
|
||||
return localVarReturnValue, nil, err
|
||||
|
||||
@@ -33,8 +33,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
jsonCheck = regexp.MustCompile("(?i:[application|text]/json)")
|
||||
xmlCheck = regexp.MustCompile("(?i:[application|text]/xml)")
|
||||
jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`)
|
||||
xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`)
|
||||
)
|
||||
|
||||
// APIClient manages communication with the OpenAPI Petstore API v1.0.0
|
||||
@@ -331,17 +331,18 @@ func (c *APIClient) prepareRequest(
|
||||
}
|
||||
|
||||
func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
|
||||
if strings.Contains(contentType, "application/xml") {
|
||||
if err = xml.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
} else if strings.Contains(contentType, "application/json") {
|
||||
if err = json.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
if xmlCheck.MatchString(contentType) {
|
||||
if err = xml.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if jsonCheck.MatchString(contentType) {
|
||||
if err = json.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New("undefined response type")
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,9 @@ Name | Type | Description | Notes
|
||||
**EnumInteger** | **int32** | | [optional]
|
||||
**EnumNumber** | **float64** | | [optional]
|
||||
**OuterEnum** | [**OuterEnum**](OuterEnum.md) | | [optional]
|
||||
**OuterEnumInteger** | [**OuterEnumInteger**](OuterEnumInteger.md) | | [optional]
|
||||
**OuterEnumDefaultValue** | [**OuterEnumDefaultValue**](OuterEnumDefaultValue.md) | | [optional]
|
||||
**OuterEnumIntegerDefaultValue** | [**OuterEnumIntegerDefaultValue**](OuterEnumIntegerDefaultValue.md) | | [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)
|
||||
|
||||
|
||||
@@ -392,7 +392,7 @@ Name | Type | Description | Notes
|
||||
|
||||
### Authorization
|
||||
|
||||
No authorization required
|
||||
[bearer_test](../README.md#bearer_test)
|
||||
|
||||
### HTTP request headers
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# OuterEnumDefaultValue
|
||||
|
||||
## Properties
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
|
||||
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# OuterEnumInteger
|
||||
|
||||
## Properties
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
|
||||
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# OuterEnumIntegerDefaultValue
|
||||
|
||||
## Properties
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
|
||||
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
|
||||
|
||||
|
||||
@@ -15,4 +15,7 @@ type EnumTest struct {
|
||||
EnumInteger int32 `json:"enum_integer,omitempty"`
|
||||
EnumNumber float64 `json:"enum_number,omitempty"`
|
||||
OuterEnum OuterEnum `json:"outerEnum,omitempty"`
|
||||
OuterEnumInteger OuterEnumInteger `json:"outerEnumInteger,omitempty"`
|
||||
OuterEnumDefaultValue OuterEnumDefaultValue `json:"outerEnumDefaultValue,omitempty"`
|
||||
OuterEnumIntegerDefaultValue OuterEnumIntegerDefaultValue `json:"outerEnumIntegerDefaultValue,omitempty"`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* 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: \" \\
|
||||
*
|
||||
* API version: 1.0.0
|
||||
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
|
||||
*/
|
||||
|
||||
package petstore
|
||||
type OuterEnumDefaultValue string
|
||||
|
||||
// List of OuterEnumDefaultValue
|
||||
const (
|
||||
PLACED OuterEnumDefaultValue = "placed"
|
||||
APPROVED OuterEnumDefaultValue = "approved"
|
||||
DELIVERED OuterEnumDefaultValue = "delivered"
|
||||
)
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* 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: \" \\
|
||||
*
|
||||
* API version: 1.0.0
|
||||
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
|
||||
*/
|
||||
|
||||
package petstore
|
||||
type OuterEnumInteger int32
|
||||
|
||||
// List of OuterEnumInteger
|
||||
const (
|
||||
_0 OuterEnumInteger = "0"
|
||||
_1 OuterEnumInteger = "1"
|
||||
_2 OuterEnumInteger = "2"
|
||||
)
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* 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: \" \\
|
||||
*
|
||||
* API version: 1.0.0
|
||||
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
|
||||
*/
|
||||
|
||||
package petstore
|
||||
type OuterEnumIntegerDefaultValue int32
|
||||
|
||||
// List of OuterEnumIntegerDefaultValue
|
||||
const (
|
||||
_0 OuterEnumIntegerDefaultValue = "0"
|
||||
_1 OuterEnumIntegerDefaultValue = "1"
|
||||
_2 OuterEnumIntegerDefaultValue = "2"
|
||||
)
|
||||
Reference in New Issue
Block a user