Skip to content

Commit b8bd779

Browse files
ctreatmakota65535
authored andcommitted
[Go] fix unused bytes import for anyOf and oneOf models (OpenAPITools#17775)
* add sample spec for oneof and anyof with required properties * omit bytes import for oneOf and anyOf models * generate sample client for new spec
1 parent 49b86fc commit b8bd779

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+5062
-1
lines changed
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
generatorName: go
2+
outputDir: samples/client/others/go/oneof-anyof-required
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/go/spec-with-oneof-anyof-required.yaml
4+
additionalProperties:
5+
hideGenerationTimestamp: "true"

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,9 @@ public ModelsMap postProcessModels(ModelsMap objs) {
500500
}
501501

502502
if (model.hasRequired) {
503-
if (!model.isAdditionalPropertiesTrue) {
503+
if (!model.isAdditionalPropertiesTrue &&
504+
(model.oneOf == null || model.oneOf.isEmpty()) &&
505+
(model.anyOf == null || model.anyOf.isEmpty())) {
504506
imports.add(createMapping("import", "bytes"));
505507
}
506508

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Test
4+
version: 1.0.0
5+
paths: {}
6+
components:
7+
schemas:
8+
NestedObject1:
9+
required:
10+
- field1
11+
properties:
12+
field1:
13+
description: Specifies an action name to be used with the Android Intent class.
14+
type: string
15+
NestedObject2:
16+
required:
17+
- field2
18+
properties:
19+
field2:
20+
description: Specifies an action name to be used with the Android Intent class.
21+
type: string
22+
Object:
23+
oneOf:
24+
- $ref: '#/components/schemas/NestedObject1'
25+
- $ref: '#/components/schemas/NestedObject2'
26+
Object2:
27+
anyOf:
28+
- $ref: '#/components/schemas/NestedObject1'
29+
- $ref: '#/components/schemas/NestedObject2'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
2+
*.o
3+
*.a
4+
*.so
5+
6+
# Folders
7+
_obj
8+
_test
9+
10+
# Architecture specific extensions/prefixes
11+
*.[568vq]
12+
[568vq].out
13+
14+
*.cgo1.go
15+
*.cgo2.c
16+
_cgo_defun.c
17+
_cgo_gotypes.go
18+
_cgo_export.*
19+
20+
_testmain.go
21+
22+
*.exe
23+
*.test
24+
*.prof
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.gitignore
2+
.travis.yml
3+
README.md
4+
api/openapi.yaml
5+
client.go
6+
configuration.go
7+
docs/NestedObject1.md
8+
docs/NestedObject2.md
9+
docs/Object.md
10+
docs/Object2.md
11+
git_push.sh
12+
go.mod
13+
go.sum
14+
model_nested_object1.go
15+
model_nested_object2.go
16+
model_object.go
17+
model_object2.go
18+
response.go
19+
utils.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.3.0-SNAPSHOT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: go
2+
3+
install:
4+
- go get -d -v .
5+
6+
script:
7+
- go build -v ./
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Go API client for openapi
2+
3+
No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
4+
5+
## Overview
6+
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [OpenAPI-spec](https://www.openapis.org/) from a remote server, you can easily generate an API client.
7+
8+
- API version: 1.0.0
9+
- Package version: 1.0.0
10+
- Build package: org.openapitools.codegen.languages.GoClientCodegen
11+
12+
## Installation
13+
14+
Install the following dependencies:
15+
16+
```sh
17+
go get github.com/stretchr/testify/assert
18+
go get golang.org/x/net/context
19+
```
20+
21+
Put the package under your project folder and add the following in import:
22+
23+
```go
24+
import openapi "github.com/GIT_USER_ID/GIT_REPO_ID"
25+
```
26+
27+
To use a proxy, set the environment variable `HTTP_PROXY`:
28+
29+
```go
30+
os.Setenv("HTTP_PROXY", "http://proxy_name:proxy_port")
31+
```
32+
33+
## Configuration of Server URL
34+
35+
Default configuration comes with `Servers` field that contains server objects as defined in the OpenAPI specification.
36+
37+
### Select Server Configuration
38+
39+
For using other server than the one defined on index 0 set context value `openapi.ContextServerIndex` of type `int`.
40+
41+
```go
42+
ctx := context.WithValue(context.Background(), openapi.ContextServerIndex, 1)
43+
```
44+
45+
### Templated Server URL
46+
47+
Templated server URL is formatted using default variables from configuration or from context value `openapi.ContextServerVariables` of type `map[string]string`.
48+
49+
```go
50+
ctx := context.WithValue(context.Background(), openapi.ContextServerVariables, map[string]string{
51+
"basePath": "v2",
52+
})
53+
```
54+
55+
Note, enum values are always validated and all unused variables are silently ignored.
56+
57+
### URLs Configuration per Operation
58+
59+
Each operation can use different server URL defined using `OperationServers` map in the `Configuration`.
60+
An operation is uniquely identified by `"{classname}Service.{nickname}"` string.
61+
Similar rules for overriding default operation server index and variables applies by using `openapi.ContextOperationServerIndices` and `openapi.ContextOperationServerVariables` context maps.
62+
63+
```go
64+
ctx := context.WithValue(context.Background(), openapi.ContextOperationServerIndices, map[string]int{
65+
"{classname}Service.{nickname}": 2,
66+
})
67+
ctx = context.WithValue(context.Background(), openapi.ContextOperationServerVariables, map[string]map[string]string{
68+
"{classname}Service.{nickname}": {
69+
"port": "8443",
70+
},
71+
})
72+
```
73+
74+
## Documentation for API Endpoints
75+
76+
All URIs are relative to *http://localhost*
77+
78+
Class | Method | HTTP request | Description
79+
------------ | ------------- | ------------- | -------------
80+
81+
82+
## Documentation For Models
83+
84+
- [NestedObject1](docs/NestedObject1.md)
85+
- [NestedObject2](docs/NestedObject2.md)
86+
- [Object](docs/Object.md)
87+
- [Object2](docs/Object2.md)
88+
89+
90+
## Documentation For Authorization
91+
92+
Endpoints do not require authorization.
93+
94+
95+
## Documentation for Utility Methods
96+
97+
Due to the fact that model structure members are all pointers, this package contains
98+
a number of utility functions to easily obtain pointers to values of basic types.
99+
Each of these functions takes a value of the given basic type and returns a pointer to it:
100+
101+
* `PtrBool`
102+
* `PtrInt`
103+
* `PtrInt32`
104+
* `PtrInt64`
105+
* `PtrFloat`
106+
* `PtrFloat32`
107+
* `PtrFloat64`
108+
* `PtrString`
109+
* `PtrTime`
110+
111+
## Author
112+
113+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Test
4+
version: 1.0.0
5+
servers:
6+
- url: /
7+
paths: {}
8+
components:
9+
schemas:
10+
NestedObject1:
11+
properties:
12+
field1:
13+
description: Specifies an action name to be used with the Android Intent
14+
class.
15+
type: string
16+
required:
17+
- field1
18+
NestedObject2:
19+
properties:
20+
field2:
21+
description: Specifies an action name to be used with the Android Intent
22+
class.
23+
type: string
24+
required:
25+
- field2
26+
Object:
27+
oneOf:
28+
- $ref: '#/components/schemas/NestedObject1'
29+
- $ref: '#/components/schemas/NestedObject2'
30+
Object2:
31+
anyOf:
32+
- $ref: '#/components/schemas/NestedObject1'
33+
- $ref: '#/components/schemas/NestedObject2'

0 commit comments

Comments
 (0)