Skip to content
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 @@ -255,6 +255,15 @@ public String getTypeDeclaration(Schema p) {
// Not using the supertype invocation, because we want to UpperCamelize
// the type.
String openAPIType = getSchemaType(p);
String ref = p.get$ref();
if(ref != null && !ref.isEmpty()) {
String tryRefV2 = "#/definitions/" + openAPIType;
String tryRefV3 = "#/components/schemas/" + openAPIType;
if(ref.equals(tryRefV2) || ref.equals(tryRefV3)) {
return toModelName(openAPIType);
}
}

if (typeMapping.containsKey(openAPIType)) {
return typeMapping.get(openAPIType);
}
Expand All @@ -273,8 +282,12 @@ public String getTypeDeclaration(Schema p) {
@Override
public String getSchemaType(Schema p) {
String openAPIType = super.getSchemaType(p);
String ref = p.get$ref();
String type = null;
if (typeMapping.containsKey(openAPIType)) {

if(ref != null && !ref.isEmpty()) {
type = openAPIType;
} else if (typeMapping.containsKey(openAPIType)) {
type = typeMapping.get(openAPIType);
if (languageSpecificPrimitives.contains(type))
return (type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,22 @@ public void mapModelTest() {
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
}

@Test(description = "convert file type and file schema models")
public void filePropertyTest() {
final DefaultCodegen codegen = new GoClientCodegen();
final Schema model1 = new Schema().type("file");
Assert.assertEquals(codegen.getSchemaType(model1), "*os.File");
Assert.assertEquals(codegen.getTypeDeclaration(model1), "*os.File");

final Schema model2 = new Schema().$ref("#/definitions/File");
Assert.assertEquals(codegen.getSchemaType(model2), "File");
Assert.assertEquals(codegen.getTypeDeclaration(model2), "File");

final Schema model3 = new Schema().$ref("#/components/schemas/File");
Assert.assertEquals(codegen.getSchemaType(model3), "File");
Assert.assertEquals(codegen.getTypeDeclaration(model3), "File");
}

@DataProvider(name = "modelNames")
public static Object[][] primeNumbers() {
return new Object[][] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,23 @@ paths:
description: successful operation
schema:
$ref: '#/definitions/Client'
/fake/body-with-file-schema:
put:
tags:
- fake
description: 'For this test, the body for this request much reference a schema named `File`.'
operationId: testBodyWithFileSchema
parameters:
- name: body
in: body
required: true
schema:
$ref: '#/definitions/FileSchemaTestClass'
consumes:
- application/json
responses:
'200':
description: Success
'/fake/{petId}/uploadImageWithRequiredFile':
post:
tags:
Expand Down Expand Up @@ -1474,7 +1491,7 @@ definitions:
# - Cat
# - Dog
OuterEnum:
type: "string"
type: string
enum:
- "placed"
- "approved"
Expand All @@ -1498,3 +1515,19 @@ definitions:
StringBooleanMap:
additionalProperties:
type: boolean
FileSchemaTestClass:
type: object
properties:
file:
$ref: "#/definitions/File"
files:
type: array
items:
$ref: "#/definitions/File"
File:
type: object
desription: 'Must be named `File` for test.'
properties:
sourceURI:
description: 'Test capitalization'
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,23 @@ paths:
$ref: '#/components/schemas/Client'
requestBody:
$ref: '#/components/requestBodies/Client'
/fake/body-with-file-schema:
put:
tags:
- fake
description: >-
For this test, the body for this request much reference a schema named
`File`.
operationId: testBodyWithFileSchema
responses:
'200':
description: Success
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/FileSchemaTestClass'
required: true
'/fake/{petId}/uploadImageWithRequiredFile':
post:
tags:
Expand Down Expand Up @@ -1375,6 +1392,12 @@ components:
enum:
- UPPER
- lower
direct_map:
type: object
additionalProperties:
type: boolean
indirect_map:
$ref: '#/components/schemas/StringBooleanMap'
ArrayTest:
type: object
properties:
Expand Down Expand Up @@ -1453,6 +1476,25 @@ components:
OuterBoolean:
type: boolean
x-codegen-body-parameter-name: boolean_post_body
StringBooleanMap:
additionalProperties:
type: boolean
FileSchemaTestClass:
type: object
properties:
file:
$ref: '#/components/schemas/File'
files:
type: array
items:
$ref: '#/components/schemas/File'
File:
type: object
description: Must be named `File` for test.
properties:
sourceURI:
description: Test capitalization
type: string
_special_model.name_:
properties:
'$special[property.name]':
Expand Down
29 changes: 29 additions & 0 deletions samples/client/petstore/go/fake_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"testing"

sw "./go-petstore"
"golang.org/x/net/context"
)

// TestPutBodyWithFileSchema ensures a model with the name 'File'
// gets converted properly to the petstore.File struct vs. *os.File
// as specified in typeMapping for 'File'.
func TestPutBodyWithFileSchema(t *testing.T) {
return // early return to test compilation

schema := sw.FileSchemaTestClass{
File: sw.File{SourceURI: "https://example.com/image.png"},
Files: []sw.File{{SourceURI: "https://example.com/image.png"}}}

r, err := client.FakeApi.TestBodyWithFileSchema(context.Background(), schema)

if err != nil {
t.Errorf("Error while adding pet")
t.Log(err)
}
if r.StatusCode != 200 {
t.Log(r)
}
}
3 changes: 3 additions & 0 deletions samples/client/petstore/go/go-petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Class | Method | HTTP request | Description
*FakeApi* | [**FakeOuterCompositeSerialize**](docs/FakeApi.md#fakeoutercompositeserialize) | **Post** /fake/outer/composite |
*FakeApi* | [**FakeOuterNumberSerialize**](docs/FakeApi.md#fakeouternumberserialize) | **Post** /fake/outer/number |
*FakeApi* | [**FakeOuterStringSerialize**](docs/FakeApi.md#fakeouterstringserialize) | **Post** /fake/outer/string |
*FakeApi* | [**TestBodyWithFileSchema**](docs/FakeApi.md#testbodywithfileschema) | **Put** /fake/body-with-file-schema |
*FakeApi* | [**TestBodyWithQueryParams**](docs/FakeApi.md#testbodywithqueryparams) | **Put** /fake/body-with-query-params |
*FakeApi* | [**TestClientModel**](docs/FakeApi.md#testclientmodel) | **Patch** /fake | To test \"client\" model
*FakeApi* | [**TestEndpointParameters**](docs/FakeApi.md#testendpointparameters) | **Post** /fake | Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
Expand Down Expand Up @@ -83,6 +84,8 @@ Class | Method | HTTP request | Description
- [EnumArrays](docs/EnumArrays.md)
- [EnumClass](docs/EnumClass.md)
- [EnumTest](docs/EnumTest.md)
- [File](docs/File.md)
- [FileSchemaTestClass](docs/FileSchemaTestClass.md)
- [FormatTest](docs/FormatTest.md)
- [HasOnlyReadOnly](docs/HasOnlyReadOnly.md)
- [List](docs/List.md)
Expand Down
39 changes: 39 additions & 0 deletions samples/client/petstore/go/go-petstore/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,22 @@ paths:
summary: To test special tags
tags:
- $another-fake?
/fake/body-with-file-schema:
put:
description: For this test, the body for this request much reference a schema named `File`.
operationId: testBodyWithFileSchema
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/FileSchemaTestClass'
required: true
responses:
200:
content: {}
description: Success
tags:
- fake
/fake/{petId}/uploadImageWithRequiredFile:
post:
operationId: uploadFileWithRequiredFile
Expand Down Expand Up @@ -1413,6 +1429,21 @@ components:
OuterBoolean:
type: boolean
x-codegen-body-parameter-name: boolean_post_body
FileSchemaTestClass:
example:
file:
sourceURI: sourceURI
files:
- sourceURI: sourceURI
- sourceURI: sourceURI
properties:
file:
$ref: '#/components/schemas/File'
files:
items:
$ref: '#/components/schemas/File'
type: array
type: object
Animal:
discriminator:
propertyName: className
Expand Down Expand Up @@ -1475,6 +1506,14 @@ components:
items:
$ref: '#/components/schemas/Animal'
type: array
File:
example:
sourceURI: sourceURI
properties:
sourceURI:
description: Test capitalization
type: string
type: object
Pet:
example:
photoUrls:
Expand Down
67 changes: 67 additions & 0 deletions samples/client/petstore/go/go-petstore/api_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,73 @@ func (a *FakeApiService) FakeOuterStringSerialize(ctx context.Context, localVarO
return localVarReturnValue, localVarHttpResponse, nil
}

/*
FakeApiService
For this test, the body for this request much reference a schema named `File`.
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
* @param fileSchemaTestClass
*/
func (a *FakeApiService) TestBodyWithFileSchema(ctx context.Context, fileSchemaTestClass FileSchemaTestClass) (*http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Put")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte
)

// create path and map variables
localVarPath := a.client.cfg.BasePath + "/fake/body-with-file-schema"

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}

// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json"}

// set Content-Type header
localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
if localVarHttpContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHttpContentType
}

// to determine the Accept header
localVarHttpHeaderAccepts := []string{}

// set Accept header
localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
if localVarHttpHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
}
// body params
localVarPostBody = &fileSchemaTestClass
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return nil, err
}

localVarHttpResponse, err := a.client.callAPI(r)
if err != nil || localVarHttpResponse == nil {
return localVarHttpResponse, err
}

localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
localVarHttpResponse.Body.Close()
if err != nil {
return localVarHttpResponse, err
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericOpenAPIError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
return localVarHttpResponse, newErr
}

return localVarHttpResponse, nil
}

/*
FakeApiService
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
Expand Down
Loading