Skip to content

Commit

Permalink
[go-server] Partially reverts and fix #15185 (#16258)
Browse files Browse the repository at this point in the history
* Partitally reverts #15185

* Remove unused import

* Set zero value if param is empty

* Refactor samples, add test config

* Add tests

* Clean up

* Fix test
  • Loading branch information
lwj5 authored Aug 7, 2023
1 parent 4e5bd8a commit 2b44d4e
Show file tree
Hide file tree
Showing 78 changed files with 5,072 additions and 392 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generatorName: go-server
outputDir: samples/server/petstore/go-server-required
inputSpec: modules/openapi-generator/src/test/resources/3_0/server-required.yaml
outputDir: samples/openapi3/server/petstore/go/go-petstore
inputSpec: modules/openapi-generator/src/test/resources/3_0/go-server/petstore_with_test_endpoint.yaml
templateDir: modules/openapi-generator/src/main/resources/go-server
additionalProperties:
hideGenerationTimestamp: "true"
Expand Down
2 changes: 1 addition & 1 deletion bin/configs/go-server-chi-api-server.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generatorName: go-server
outputDir: samples/server/petstore/go-chi-server
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
inputSpec: modules/openapi-generator/src/test/resources/3_0/go-server/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/go-server
additionalProperties:
hideGenerationTimestamp: "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,19 @@

package org.openapitools.codegen.languages;

import java.io.File;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Iterables;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.meta.features.DocumentationFeature;
import org.openapitools.codegen.meta.features.GlobalFeature;
import org.openapitools.codegen.meta.features.ParameterFeature;
import org.openapitools.codegen.meta.features.SchemaSupportFeature;
import org.openapitools.codegen.meta.features.SecurityFeature;
import org.openapitools.codegen.meta.features.WireFormatFeature;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.features.*;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Iterables;

import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Schema;
import java.io.File;
import java.util.*;

public class GoServerCodegen extends AbstractGoCodegen {

Expand Down Expand Up @@ -318,7 +297,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
List<Map<String, String>> imports = objs.getImports();

for (ModelMap m : objs.getModels()) {
imports.add(createMapping("import", "encoding/json"));
// imports.add(createMapping("import", "encoding/json"));

CodegenModel model = m.getModel();
if (model.isEnum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,6 @@ type {{classname}} struct {
{{/vars}}
}{{/isEnum}}

// UnmarshalJSON sets *m to a copy of data while respecting defaults if specified.
func (m *{{classname}}) UnmarshalJSON(data []byte) error {
{{#vars}}
{{#defaultValue}}
{{^isArray}}
{{#isBoolean}}
m.{{name}} = {{{.}}}
{{/isBoolean}}
{{#isNumeric}}
m.{{name}} = {{{.}}}
{{/isNumeric}}
{{^isBoolean}}
{{^isNumeric}}
m.{{name}} = "{{{.}}}"
{{/isNumeric}}
{{/isBoolean}}
{{/isArray}}
{{/defaultValue}}
{{/vars}}

type Alias {{classname}} // To avoid infinite recursion
return json.Unmarshal(data, (*Alias)(m))
}

// Assert{{classname}}Required checks if the required fields are not zero-ed
func Assert{{classname}}Required(obj {{classname}}) error {
{{#hasRequired}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,28 +178,48 @@ type ParseString[T Number | string | bool] func(v string) (T, error)

// parseFloat64 parses a string parameter to an float64.
func parseFloat64(param string) (float64, error) {
if param == "" {
return 0, nil
}

return strconv.ParseFloat(param, 64)
}

// parseFloat32 parses a string parameter to an float32.
func parseFloat32(param string) (float32, error) {
if param == "" {
return 0, nil
}

v, err := strconv.ParseFloat(param, 32)
return float32(v), err
}

// parseInt64 parses a string parameter to an int64.
func parseInt64(param string) (int64, error) {
if param == "" {
return 0, nil
}

return strconv.ParseInt(param, 10, 64)
}

// parseInt32 parses a string parameter to an int32.
func parseInt32(param string) (int32, error) {
if param == "" {
return 0, nil
}

val, err := strconv.ParseInt(param, 10, 32)
return int32(val), err
}

// parseBool parses a string parameter to an bool.
func parseBool(param string) (bool, error) {
if param == "" {
return false, nil
}

return strconv.ParseBool(param)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,41 @@ components:
name: api_key
in: header
schemas:
OrderInfo:
title: Pet Order Info
description: An order info for a pets from the pet store
type: object
properties:
petId:
type: integer
format: int64
quantity:
type: integer
format: int32
shipDate:
type: string
format: date-time
xml:
name: OrderInfo
SpecialInfo:
title: Pet Order Info
description: An order info for a pets from the pet store
discriminator:
propertyName: type
type: object
properties:
promotion:
type: boolean
type:
type: string
xml:
name: OrderInfo
Order:
title: Pet Order
description: An order for a pets from the pet store
allOf:
- $ref: '#/components/schemas/OrderInfo'
- $ref: '#/components/schemas/SpecialInfo'
type: object
properties:
id:
Expand All @@ -644,12 +676,18 @@ components:
complete:
type: boolean
default: false
comment:
type: string
nullable: true
xml:
name: Order
required:
- comment
Category:
title: Pet category
description: A category for a pet
type: object
nullable: true
properties:
id:
type: integer
Expand Down Expand Up @@ -679,12 +717,45 @@ components:
type: string
phone:
type: string
nullable: true
userStatus:
type: integer
format: int32
description: User Status
deepSliceModel:
nullable: true
type: array
description: An array 1-deep.
items:
type: array
description: An array 2-deep.
items:
type: array
description: An array 3-deep.
items:
$ref: '#/components/schemas/Tag'
deepSliceMap:
type: array
description: An array 1-deep.
items:
type: array
description: An array 2-deep.
items:
title: an Object
type: object
description: An array 3-deep.
properties:
tag:
$ref: '#/components/schemas/Tag'
Pet:
type: array
description: An array of pet.
items:
$ref: '#/components/schemas/Pet'
xml:
name: User
required:
- deepSliceModel
Tag:
title: Pet Tag
description: A tag for a pet
Expand Down Expand Up @@ -714,13 +785,15 @@ components:
type: string
example: doggie
photoUrls:
nullable: true
type: array
xml:
name: photoUrl
wrapped: true
items:
type: string
tags:
nullable: true
type: array
xml:
name: tag
Expand Down
Loading

0 comments on commit 2b44d4e

Please sign in to comment.