Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Go] skip validator import if oneOf discriminator lookup is enabled #20497

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions bin/configs/go-oneof-discriminator-lookup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
generatorName: go
outputDir: samples/client/others/go/oneof-discriminator-lookup
inputSpec: modules/openapi-generator/src/test/resources/3_0/go/spec-with-oneof-discriminator.yaml
additionalProperties:
useOneOfDiscriminatorLookup: "true"
hideGenerationTimestamp: "true"
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,6 @@ public ModelsMap postProcessModels(ModelsMap objs) {
for (ModelMap m : objs.getModels()) {
boolean addedTimeImport = false;
boolean addedOSImport = false;
boolean addedValidator = false;
CodegenModel model = m.getModel();

List<CodegenProperty> inheritedProperties = new ArrayList<>();
Expand Down Expand Up @@ -830,11 +829,6 @@ public ModelsMap postProcessModels(ModelsMap objs) {
imports.add(createMapping("import", "fmt"));
}

if (model.oneOf != null && !model.oneOf.isEmpty() && !addedValidator && generateUnmarshalJSON) {
imports.add(createMapping("import", "gopkg.in/validator.v2"));
addedValidator = true;
}

// if oneOf contains "null" type
if (model.oneOf != null && !model.oneOf.isEmpty() && model.oneOf.contains("nil")) {
model.isNullable = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ public ModelsMap postProcessModels(ModelsMap objs) {
if (model.oneOf != null && !model.oneOf.isEmpty()) {
imports.add(createMapping("import", "fmt"));
addedFmtImport = true;

if (generateUnmarshalJSON && !useOneOfDiscriminatorLookup) {
imports.add(createMapping("import", "gopkg.in/validator.v2"));
}
}

// anyOf
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
openapi: 3.0.0
info:
title: Test
version: 1.0.0
paths: {}
components:
schemas:
NestedObject1:
required:
- field1
properties:
field1:
description: Specifies an action name to be used with the Android Intent class.
type: string
type:
type: string
NestedObject2:
required:
- field2
properties:
field1:
description: Specifies an action name to be used with the Android Intent class.
type: string
type:
type: string
Object:
oneOf:
- $ref: '#/components/schemas/NestedObject1'
- $ref: '#/components/schemas/NestedObject2'
discriminator:
propertyName: type
mapping:
ONE: '#/components/schemas/NestedObject1'
TWO: '#/components/schemas/NestedObject2'

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions samples/client/others/go/oneof-discriminator-lookup/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.gitignore
.travis.yml
README.md
api/openapi.yaml
client.go
configuration.go
docs/NestedObject1.md
docs/NestedObject2.md
docs/Object.md
git_push.sh
go.mod
go.sum
model_nested_object1.go
model_nested_object2.go
model_object.go
response.go
utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.11.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: go

install:
- go get -d -v .

script:
- go build -v ./

113 changes: 113 additions & 0 deletions samples/client/others/go/oneof-discriminator-lookup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Go API client for openapi

No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

## Overview
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.

- API version: 1.0.0
- Package version: 1.0.0
- Generator version: 7.11.0-SNAPSHOT
- Build package: org.openapitools.codegen.languages.GoClientCodegen

## Installation

Install the following dependencies:

```sh
go get github.com/stretchr/testify/assert
go get golang.org/x/net/context
```

Put the package under your project folder and add the following in import:

```go
import openapi "github.com/GIT_USER_ID/GIT_REPO_ID"
```

To use a proxy, set the environment variable `HTTP_PROXY`:

```go
os.Setenv("HTTP_PROXY", "http://proxy_name:proxy_port")
```

## Configuration of Server URL

Default configuration comes with `Servers` field that contains server objects as defined in the OpenAPI specification.

### Select Server Configuration

For using other server than the one defined on index 0 set context value `openapi.ContextServerIndex` of type `int`.

```go
ctx := context.WithValue(context.Background(), openapi.ContextServerIndex, 1)
```

### Templated Server URL

Templated server URL is formatted using default variables from configuration or from context value `openapi.ContextServerVariables` of type `map[string]string`.

```go
ctx := context.WithValue(context.Background(), openapi.ContextServerVariables, map[string]string{
"basePath": "v2",
})
```

Note, enum values are always validated and all unused variables are silently ignored.

### URLs Configuration per Operation

Each operation can use different server URL defined using `OperationServers` map in the `Configuration`.
An operation is uniquely identified by `"{classname}Service.{nickname}"` string.
Similar rules for overriding default operation server index and variables applies by using `openapi.ContextOperationServerIndices` and `openapi.ContextOperationServerVariables` context maps.

```go
ctx := context.WithValue(context.Background(), openapi.ContextOperationServerIndices, map[string]int{
"{classname}Service.{nickname}": 2,
})
ctx = context.WithValue(context.Background(), openapi.ContextOperationServerVariables, map[string]map[string]string{
"{classname}Service.{nickname}": {
"port": "8443",
},
})
```

## Documentation for API Endpoints

All URIs are relative to *http://localhost*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------


## Documentation For Models

- [NestedObject1](docs/NestedObject1.md)
- [NestedObject2](docs/NestedObject2.md)
- [Object](docs/Object.md)


## Documentation For Authorization

Endpoints do not require authorization.


## Documentation for Utility Methods

Due to the fact that model structure members are all pointers, this package contains
a number of utility functions to easily obtain pointers to values of basic types.
Each of these functions takes a value of the given basic type and returns a pointer to it:

* `PtrBool`
* `PtrInt`
* `PtrInt32`
* `PtrInt64`
* `PtrFloat`
* `PtrFloat32`
* `PtrFloat64`
* `PtrString`
* `PtrTime`

## Author


Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
openapi: 3.0.0
info:
title: Test
version: 1.0.0
servers:
- url: /
paths: {}
components:
schemas:
NestedObject1:
properties:
field1:
description: Specifies an action name to be used with the Android Intent
class.
type: string
type:
type: string
required:
- field1
NestedObject2:
properties:
field1:
description: Specifies an action name to be used with the Android Intent
class.
type: string
type:
type: string
required:
- field2
Object:
discriminator:
mapping:
ONE: '#/components/schemas/NestedObject1'
TWO: '#/components/schemas/NestedObject2'
propertyName: type
oneOf:
- $ref: '#/components/schemas/NestedObject1'
- $ref: '#/components/schemas/NestedObject2'
Loading
Loading