Skip to content

Commit

Permalink
Better OpenAPI spec v3 support: allOf, anyOf, oneOf (#1360)
Browse files Browse the repository at this point in the history
* add oneOf support to Ruby

* add anyOf support to ruby client

* add discriminator support to ruby client

* fix typo

* update samples, fix NPE

* better format in ruby generator

* fix test cases, disable mapping test

* fix update script, update samples

* add test, fix mapping

* update exit code

* reenabled discriminator test

* remove duplicated properties

* add test for duplicated properties

* update samples, add new spec

* fix ruby test cases

* fix hasMore after removing duplicates

* refactor method, comment out haskell client test

* fix hasMore and update samples

* fix parent detection

* fix discriminator check

* [haskell-http-client] need to use {{vars}}{{required}} instead of {{requiredVars}}

* remove deprecated methods in default codegen (#1031)

* regenerate samples

* remove commented code
  • Loading branch information
wing328 committed Dec 6, 2018
1 parent c0634ac commit 774013c
Show file tree
Hide file tree
Showing 125 changed files with 1,108 additions and 327 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ samples/server/petstore/kotlin-server/ktor/build
.stack-work
.cabal-sandbox
cabal.project.local
samples/client/petstore/haskell-http-client/docs/haddock-bundle.min.js
samples/client/petstore/haskell-http-client/docs/meta.json
samples/client/petstore/haskell-http-client/docs/quick-jump.css

# R
.Rproj.user
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openapitools.codegen;

import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.*;

public class CodegenDiscriminator {
Expand Down Expand Up @@ -85,4 +87,13 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(propertyName, mapping, mappedModels);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.append("propertyName", propertyName)
.append("mapping", mapping)
.append("mappedModels", mappedModels)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@

import io.swagger.v3.oas.models.ExternalDocumentation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

Expand All @@ -40,6 +33,11 @@ public class CodegenModel {
public List<CodegenModel> interfaceModels;
public List<CodegenModel> children;

// anyOf, oneOf, allOf
public Set<String> anyOf = new TreeSet<String>();
public Set<String> oneOf = new TreeSet<String>();
public Set<String> allOf = new TreeSet<String>();

public String name, classname, title, description, classVarName, modelJson, dataType, xmlPrefix, xmlNamespace, xmlName;
public String classFilename; // store the class file name, mainly used for import
public String unescapedDescription;
Expand All @@ -53,8 +51,8 @@ public class CodegenModel {
public List<CodegenProperty> optionalVars = new ArrayList<CodegenProperty>(); // a list of optional properties
public List<CodegenProperty> readOnlyVars = new ArrayList<CodegenProperty>(); // a list of read-only properties
public List<CodegenProperty> readWriteVars = new ArrayList<CodegenProperty>(); // a list of properties for read, write
public List<CodegenProperty> allVars;
public List<CodegenProperty> parentVars = new ArrayList<>();
public List<CodegenProperty> allVars = new ArrayList<CodegenProperty>();
public List<CodegenProperty> parentVars = new ArrayList<CodegenProperty>();
public Map<String, Object> allowableValues;

// Sorted sets of required parameters.
Expand Down Expand Up @@ -195,11 +193,11 @@ public int hashCode() {
result = 31 * result + (mandatory != null ? mandatory.hashCode() : 0);
result = 31 * result + (allMandatory != null ? allMandatory.hashCode() : 0);
result = 31 * result + (imports != null ? imports.hashCode() : 0);
result = 31 * result + (hasVars ? 13:31);
result = 31 * result + (emptyVars ? 13:31);
result = 31 * result + (hasMoreModels ? 13:31);
result = 31 * result + (hasEnums ? 13:31);
result = 31 * result + (isEnum ? 13:31);
result = 31 * result + (hasVars ? 13 : 31);
result = 31 * result + (emptyVars ? 13 : 31);
result = 31 * result + (hasMoreModels ? 13 : 31);
result = 31 * result + (hasEnums ? 13 : 31);
result = 31 * result + (isEnum ? 13 : 31);
result = 31 * result + (externalDocumentation != null ? externalDocumentation.hashCode() : 0);
result = 31 * result + (vendorExtensions != null ? vendorExtensions.hashCode() : 0);
result = 31 * result + Objects.hash(hasOnlyReadOnly);
Expand Down Expand Up @@ -499,4 +497,67 @@ public String getAdditionalPropertiesType() {
public void setAdditionalPropertiesType(String additionalPropertiesType) {
this.additionalPropertiesType = additionalPropertiesType;
}

/**
* Remove duplicated properties in all variable list and update "hasMore"
*/
public void removeAllDuplicatedProperty() {
// remove duplicated properties
vars = removeDuplicatedProperty(vars);
optionalVars = removeDuplicatedProperty(optionalVars);
requiredVars = removeDuplicatedProperty(requiredVars);
parentVars = removeDuplicatedProperty(parentVars);
allVars = removeDuplicatedProperty(allVars);
readOnlyVars = removeDuplicatedProperty(readOnlyVars);
readWriteVars = removeDuplicatedProperty(readWriteVars);

// update property list's "hasMore"
updatePropertyListHasMore(vars);
updatePropertyListHasMore(optionalVars);
updatePropertyListHasMore(requiredVars);
updatePropertyListHasMore(parentVars);
updatePropertyListHasMore(allVars);
updatePropertyListHasMore(readOnlyVars);
updatePropertyListHasMore(readWriteVars);
}

private List<CodegenProperty> removeDuplicatedProperty(List<CodegenProperty> vars) {
// clone the list first
List<CodegenProperty> newList = new ArrayList<CodegenProperty>();
for (CodegenProperty cp : vars) {
newList.add(cp.clone());
}

Set<String> propertyNames = new TreeSet<String>();
Set<String> duplicatedNames = new TreeSet<String>();

ListIterator<CodegenProperty> iterator = newList.listIterator();
while (iterator.hasNext()) {
CodegenProperty element = iterator.next();

if (propertyNames.contains(element.baseName)) {
duplicatedNames.add(element.baseName);
iterator.remove();
} else {
propertyNames.add(element.baseName);
}
}

return newList;
}

/**
* Clone the element and update "hasMore" in the list of codegen properties
*/
private void updatePropertyListHasMore(List<CodegenProperty> vars) {
if (vars != null) {
for (int i = 0; i < vars.size(); i++) {
if (i < vars.size() - 1) {
vars.get(i).hasMore = true;
} else { // last element
vars.get(i).hasMore = false;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@

package org.openapitools.codegen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;

public class CodegenProperty implements Cloneable {
public String baseName, complexType, getter, setter, description, dataType,
Expand Down Expand Up @@ -733,6 +729,7 @@ public CodegenProperty clone() {
if (this.vendorExtensions != null) {
cp.vendorExtensions = new HashMap<String, Object>(this.vendorExtensions);
}

return cp;
} catch (CloneNotSupportedException e) {
throw new IllegalStateException(e);
Expand Down Expand Up @@ -817,4 +814,6 @@ public java.lang.String toString() {
", isXmlWrapped=" + isXmlWrapped +
'}';
}


}
Loading

0 comments on commit 774013c

Please sign in to comment.