-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
[go-experimental] Add support for AnyType and FreeForm type #6212
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,8 +92,8 @@ public AbstractGoCodegen() { | |
| "complex128", | ||
| "rune", | ||
| "byte", | ||
| "map[string]interface{}", | ||
| "interface{}" | ||
| "ObjectType", | ||
| "AnyType" | ||
| ) | ||
| ); | ||
|
|
||
|
|
@@ -128,9 +128,15 @@ public AbstractGoCodegen() { | |
| // map[string]interface{}, whereas an arbitrary type is implemented | ||
| // in golang as interface{}. | ||
| // See issue #5387 for more details. | ||
| typeMapping.put("object", "map[string]interface{}"); | ||
| typeMapping.put("interface{}", "interface{}"); | ||
| typeMapping.put("AnyType", "interface{}"); | ||
| typeMapping.put("object", "ObjectType"); | ||
| // A schema that does not specify the 'type' attribute. The value | ||
| // can be anything. | ||
| typeMapping.put("AnyType", "AnyType"); | ||
| // Below is the entry to map the JSON 'null' type to golang. | ||
| // Note there is no built-in 'null' type in golang, and it's not possible | ||
| // to declare a variable whose only possible value is 'nil'. | ||
| // For example `a := nil` is not a valid go statement. | ||
| typeMapping.put("null", "NullType"); | ||
|
|
||
| numberTypes = new HashSet<String>( | ||
| Arrays.asList( | ||
|
|
@@ -391,7 +397,7 @@ public String getSchemaType(Schema p) { | |
| type = openAPIType; | ||
| } else if ("object".equals(openAPIType) && ModelUtils.isAnyTypeSchema(p)) { | ||
| // Arbitrary type. Note this is not the same thing as free-form object. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should remove/revise this comment (not added by you) as it's not entirely true. |
||
| type = "interface{}"; | ||
| type = "AnyType"; | ||
| } else if (typeMapping.containsKey(openAPIType)) { | ||
| type = typeMapping.get(openAPIType); | ||
| if (languageSpecificPrimitives.contains(type)) | ||
|
|
@@ -598,14 +604,58 @@ private void setExportParameterName(List<CodegenParameter> codegenParameters) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * OAS type mapping to golang implementation: | ||
| * | ||
| * AnyType interface{} Any type, e.g. null, string, integer, number, boolean, map, array. | ||
| * ObjectType map[string]interface{} Free-form object with no constraint on additional properties. | ||
| * map[string]Animal map[string]Animal Free-form object. The value of undeclared properties must be 'Animal'. | ||
| * map[string]ObjectType map[string]map[string]interface{} Free-form object. The value of undeclared properties must be an object (map). | ||
| * map[string][]interface{} Free-form object. The value of undeclared properties must be an array. | ||
| * | ||
| */ | ||
| @Override | ||
| public void postProcessModelProperty(CodegenModel model, CodegenProperty property) { | ||
| // The 'go-experimental/model.mustache' template conditionally generates accessor methods. | ||
| // For primitive types and custom types (e.g. interface{}, map[string]interface{}...), | ||
| // the generated code has a wrapper type and a Get() function to access the underlying type. | ||
| // For containers (e.g. Array, Map), the generated code returns the type directly. | ||
| if (property.isContainer || property.isFreeFormObject || property.isAnyType) { | ||
| property.vendorExtensions.put("x-golang-is-container", true); | ||
| // The 'go-experimental/model.mustache' template generates accessor methods. | ||
| // For containers (e.g. Array, Map) and non-nullable primitive types, the go struct field | ||
| // is the underlying type, e.g. *int32, *string, *bool... | ||
| // In other cases (nullable primitive types, free-form objects...), the generated code has | ||
| // a wrapper type and a Get() function to access the underlying type. | ||
| boolean hasWrapper = false; | ||
| if (property.isAnyType) { | ||
| // The wrapper type is the 'AnyType' struct and the wrapped type is 'interface{}'. | ||
| hasWrapper = true; | ||
| } else if (property.isFreeFormObject && !property.isNullable) { // && | ||
| // Undeclared properties use case. | ||
| // The undeclared properties are implemented using a go map. | ||
| // The map key is always a string. | ||
| // The map value is interface{} when there are no constraints, but the | ||
| // 'additionalProperties' attribute may require a specific type. | ||
| if (property.items == null) { | ||
| // The 'additionalProperties' attribute is not set, so it is a free-form object | ||
| // with no constraint on the type of undeclared properties. | ||
| // The wrapper type is 'ObjectType'. | ||
| // The wrapped type is 'map[string]interface{}'. | ||
| hasWrapper = true; | ||
| } else if (property.items.isFreeFormObject && !property.items.isNullable) { | ||
| // The 'additionalProperties' attribute is set, and the value of the undeclared | ||
| // properties must be a free-form object. Hence it's a map of map. | ||
| // The wrapper type is map[string]ObjectType. | ||
| // The wrapped type is 'map[string]map[string]interface{}'. | ||
| hasWrapper = true; | ||
| } else { | ||
| // The 'additionalProperties' attribute is set, and the value of the undeclared | ||
| // properties is not free-form. For example, if the undeclared properties must | ||
| // be of type 'Animal', then: | ||
| // The wrapper type is map[string]Animal. | ||
| // The wrapped type is 'map[string]Animal'. | ||
| } | ||
| } else if (property.isNullable && !property.isContainer) { | ||
| // Primitive types. | ||
| hasWrapper = true; | ||
| } | ||
| if (hasWrapper) { | ||
| property.vendorExtensions.put("x-golang-has-wrapper", true); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There were some discussions before on what
objectshould map to in Go: