Skip to content

Commit edb94f7

Browse files
authored
[Go][Experimental] better oneOf and anyOf implementation (#6166)
* diff oneOf implementation * update oas3 samples * fix imports * remove commented code
1 parent d22bea2 commit edb94f7

File tree

126 files changed

+1109
-880
lines changed

Some content is hidden

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

126 files changed

+1109
-880
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public GoClientExperimentalCodegen() {
4242
embeddedTemplateDir = templateDir = "go-experimental";
4343

4444
usesOptionals = false;
45-
useOneOfInterfaces = true;
4645

4746
generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata).stability(Stability.EXPERIMENTAL).build();
4847
}
@@ -167,6 +166,8 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
167166
// must be invoked at the beginning of this method.
168167
objs = super.postProcessModels(objs);
169168

169+
List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports");
170+
170171
List<Map<String, Object>> models = (List<Map<String, Object>>) objs.get("models");
171172
for (Map<String, Object> m : models) {
172173
Object v = m.get("model");
@@ -193,21 +194,22 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
193194
+ param.dataType.substring(1);
194195
}
195196
}
196-
}
197-
}
198-
return objs;
199-
}
200197

201-
@Override
202-
public void addImportsToOneOfInterface(List<Map<String, String>> imports) {
203-
for (String i : Arrays.asList("fmt")) {
204-
Map<String, String> oneImport = new HashMap<String, String>() {{
205-
put("import", i);
206-
}};
207-
if (!imports.contains(oneImport)) {
208-
imports.add(oneImport);
198+
// additional import for different cases
199+
// oneOf
200+
if (model.oneOf != null && !model.oneOf.isEmpty()) {
201+
imports.add(createMapping("import", "fmt"));
202+
}
203+
204+
// anyOf
205+
if (model.anyOf != null && !model.anyOf.isEmpty()) {
206+
imports.add(createMapping("import", "fmt"));
207+
}
209208
}
209+
210+
210211
}
212+
return objs;
211213
}
212214

213215
@Override
@@ -262,7 +264,7 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S
262264
return "URL(string: \"https://example.com\")!";
263265
} else if (codegenParameter.isDateTime || codegenParameter.isDate) { // datetime or date
264266
return "Get-Date";
265-
} else{ // numeric
267+
} else { // numeric
266268
if (StringUtils.isEmpty(codegenParameter.example)) {
267269
return codegenParameter.example;
268270
} else {
@@ -302,7 +304,7 @@ private String constructExampleCode(CodegenProperty codegenProperty, HashMap<Str
302304
return "\"https://example.com\")!";
303305
} else if (codegenProperty.isDateTime || codegenProperty.isDate) { // datetime or date
304306
return "time.Now()";
305-
} else{ // numeric
307+
} else { // numeric
306308
String example;
307309
if (StringUtils.isEmpty(codegenProperty.example)) {
308310
example = codegenProperty.example;

modules/openapi-generator/src/main/resources/go-experimental/client.mustache

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
169169
return nil, err
170170
}
171171
log.Printf("\n%s\n", string(dump))
172-
}
172+
}
173173

174174
resp, err := c.cfg.HTTPClient.Do(request)
175175
if err != nil {
@@ -381,7 +381,15 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err
381381
return nil
382382
}
383383
if jsonCheck.MatchString(contentType) {
384-
if err = json.Unmarshal(b, v); err != nil {
384+
if actualObj, ok := v.(interface{GetActualInstance() interface{}}); ok { // oneOf, anyOf schemas
385+
if unmarshalObj, ok := actualObj.(interface{UnmarshalJSON([]byte) error}); ok { // make sure it has UnmarshalJSON defined
386+
if err = unmarshalObj.UnmarshalJSON(b); err!= nil {
387+
return err
388+
}
389+
} else {
390+
errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined")
391+
}
392+
} else if err = json.Unmarshal(b, v); err != nil { // simple model
385393
return err
386394
}
387395
return nil

0 commit comments

Comments
 (0)