Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

v2 use python classes to hold non-schema components #110

Merged
merged 15 commits into from
Dec 29, 2022
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public interface CodegenConfig {

boolean getAddSuffixToDuplicateOperationNicknames();

String toRefClass(String ref, String sourceJsonPath);
String toRefClass(String ref, String sourceJsonPath, String expectedComponentType);

CodegenRequestBody fromRequestBody(RequestBody body, String sourceJsonPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,6 @@ public class CodegenHeader extends CodegenRequestBody {
public boolean isDeprecated;
protected CodegenSchema schema;

public CodegenHeader copy() {
CodegenHeader output = new CodegenHeader();
output.description = this.description;
output.unescapedDescription = this.unescapedDescription;
output.required = this.required;
output.jsonSchema = this.jsonSchema;
output.example = this.example;

if (this.name != null) {
output.setName(this.name);
}
if (this.content != null) {
output.setContent(this.content);
}
if (this.schema != null) {
output.setSchema(this.schema);
}
if (this.vendorExtensions != null) {
output.vendorExtensions = new HashMap<String, Object>(this.vendorExtensions);
}
if (this.ref != null) {
output.setRef(this.ref);
}
if (this.refModule != null) {
output.setRefModule(this.refModule);
}
if (this.imports != null) {
output.imports = imports;
}
if (this.componentModule != null) {
output.componentModule = componentModule;
}
output.isDeprecated = this.isDeprecated;
output.isExplode = this.isExplode;
output.style = this.style;

return output;
}

public CodegenSchema getSetSchema() {
if (schema != null) {
return schema;
Expand Down Expand Up @@ -105,7 +66,7 @@ public String getSetSchemaJsonPath(String jsonPath) {
}
@Override
public int hashCode() {
return Objects.hash(name, isExplode, description, unescapedDescription, style, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, ref, refModule, imports, componentModule);
return Objects.hash(refClass, name, isExplode, description, unescapedDescription, style, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, ref, refModule, imports, componentModule);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,57 +30,9 @@ public class CodegenParameter extends CodegenHeader {
// stores the openapi name property
public String baseName;

public CodegenParameter copy() {
CodegenParameter output = new CodegenParameter();
output.baseName = this.baseName;
output.description = this.description;
output.unescapedDescription = this.unescapedDescription;
output.isFormParam = this.isFormParam;
output.isQueryParam = this.isQueryParam;
output.isPathParam = this.isPathParam;
output.isHeaderParam = this.isHeaderParam;
output.isCookieParam = this.isCookieParam;
output.isBodyParam = this.isBodyParam;
output.required = this.required;
output.jsonSchema = this.jsonSchema;
output.example = this.example;

if (this.name != null) {
output.name = this.name;
}
if (this.content != null) {
output.setContent(this.content);
}
if (this.schema != null) {
output.setSchema(this.schema);
}
if (this.vendorExtensions != null) {
output.vendorExtensions = new HashMap<>(this.vendorExtensions);
}
if (this.ref != null) {
output.setRef(this.ref);
}
if (this.refModule != null) {
output.setRefModule(this.refModule);
}
if (this.imports != null) {
output.imports = imports;
}
if (this.componentModule != null) {
output.componentModule = componentModule;
}
output.isDeprecated = this.isDeprecated;
output.isExplode = this.isExplode;
output.style = this.style;
output.isDeepObject = this.isDeepObject;
output.isAllowEmptyValue = this.isAllowEmptyValue;

return output;
}

@Override
public int hashCode() {
return Objects.hash(name, isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isExplode, baseName, description, unescapedDescription, style, isDeepObject, isAllowEmptyValue, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, ref, refModule, imports, componentModule);
return Objects.hash(refClass, name, isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isExplode, baseName, description, unescapedDescription, style, isDeepObject, isAllowEmptyValue, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, ref, refModule, imports, componentModule);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class CodegenRequestBody implements OpenapiComponent {
protected String refModule;
protected Set<String> imports = new HashSet<String>();
protected String componentModule;
protected String refClass;

public String getComponentModule() {
return componentModule;
Expand All @@ -58,9 +59,17 @@ public void setComponentModule(String componentModule) {
this.componentModule = componentModule;
}

public String getRefClass() {
return refClass;
}

public void setRefClass(String refClass) {
this.refClass = refClass;
}

@Override
public int hashCode() {
return Objects.hash(description, unescapedDescription, name, example, jsonSchema, vendorExtensions, required, content, ref, refModule, imports, componentModule);
return Objects.hash(refClass, description, unescapedDescription, name, example, jsonSchema, vendorExtensions, required, content, ref, refModule, imports, componentModule);
}

@Override
Expand All @@ -69,6 +78,7 @@ public boolean equals(Object o) {
if (!(o instanceof CodegenRequestBody)) return false;
CodegenRequestBody that = (CodegenRequestBody) o;
return required == that.required &&
Objects.equals(refClass, that.refClass) &&
Objects.equals(name, that.name) &&
Objects.equals(componentModule, that.componentModule) &&
Objects.equals(ref, that.getRef()) &&
Expand All @@ -93,6 +103,7 @@ protected void addInstanceInfo(StringBuilder sb) {
sb.append(", content=").append(content);
sb.append(", ref=").append(ref);
sb.append(", refModule=").append(refModule);
sb.append(", refClass=").append(refClass);
sb.append(", imports=").append(imports);
sb.append(", componentModule=").append(componentModule);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ public class CodegenResponse implements OpenapiComponent {
private String ref;
public Set<String> imports = new TreeSet<>();
private String refModule;
private String refClass;
private String componentModule;

@Override
public int hashCode() {
return Objects.hash(name, message, examples,
return Objects.hash(refClass, name, message, examples,
jsonSchema, vendorExtensions,
headers, content,
ref, imports, refModule, componentModule);
Expand All @@ -46,6 +47,7 @@ public boolean equals(Object o) {
if (!(o instanceof CodegenResponse)) return false;
CodegenResponse that = (CodegenResponse) o;
return Objects.equals(name, that.name) &&
Objects.equals(refClass, that.refClass) &&
Objects.equals(imports, that.imports) &&
Objects.equals(ref, that.getRef()) &&
Objects.equals(content, that.getContent()) &&
Expand Down Expand Up @@ -94,6 +96,7 @@ public String toString() {
sb.append(", content=").append(content);
sb.append(", ref=").append(ref);
sb.append(", refModule=").append(refModule);
sb.append(", refClass=").append(refClass);
sb.append(", imports=").append(imports);
sb.append(", componentModule=").append(componentModule);
sb.append('}');
Expand All @@ -108,6 +111,14 @@ public String toString() {

public void setRefModule(String refModule) { this.refModule=refModule; }

public String getRefClass() {
return refClass;
}

public void setRefClass(String refClass) {
this.refClass = refClass;
}

public CodegenKey getName() { return name; }

public void setName(CodegenKey name) { this.name=name; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2458,7 +2458,7 @@ protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName,
LOGGER.warn("'{}' defines discriminator '{}', but the referenced schema '{}' is incorrect. {}",
composedSchemaName, discPropName, modelName, msgSuffix);
}
MappedModel mm = new MappedModel(modelName, getRefClassWithModule("#/components/schemas/" + modelName, sourceJsonPath));
MappedModel mm = new MappedModel(modelName, getRefClassWithModule("#/components/schemas/" + modelName, sourceJsonPath, "schemas"));
descendentSchemas.add(mm);
Schema cs = ModelUtils.getSchema(openAPI, modelName);
if (cs == null) { // cannot lookup the model based on the name
Expand All @@ -2467,7 +2467,7 @@ protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName,
Map<String, Object> vendorExtensions = cs.getExtensions();
if (vendorExtensions != null && !vendorExtensions.isEmpty() && vendorExtensions.containsKey("x-discriminator-value")) {
String xDiscriminatorValue = (String) vendorExtensions.get("x-discriminator-value");
mm = new MappedModel(xDiscriminatorValue, getRefClassWithModule("#/components/schemas/" + modelName, sourceJsonPath));
mm = new MappedModel(xDiscriminatorValue, getRefClassWithModule("#/components/schemas/" + modelName, sourceJsonPath, "schemas"));
descendentSchemas.add(mm);
}
}
Expand Down Expand Up @@ -2520,21 +2520,21 @@ protected List<MappedModel> getAllOfDescendants(String thisSchemaName, OpenAPI o
break;
}
currentSchemaName = queue.remove(0);
MappedModel mm = new MappedModel(currentSchemaName, getRefClassWithModule("#/components/schemas/" + currentSchemaName, sourceJsonPath));
MappedModel mm = new MappedModel(currentSchemaName, getRefClassWithModule("#/components/schemas/" + currentSchemaName, sourceJsonPath, "schemas"));
descendentSchemas.add(mm);
Schema cs = schemas.get(currentSchemaName);
Map<String, Object> vendorExtensions = cs.getExtensions();
if (vendorExtensions != null && !vendorExtensions.isEmpty() && vendorExtensions.containsKey("x-discriminator-value")) {
String xDiscriminatorValue = (String) vendorExtensions.get("x-discriminator-value");
mm = new MappedModel(xDiscriminatorValue, getRefClassWithModule("#/components/schemas/" + currentSchemaName, sourceJsonPath));
mm = new MappedModel(xDiscriminatorValue, getRefClassWithModule("#/components/schemas/" + currentSchemaName, sourceJsonPath, "schemas"));
descendentSchemas.add(mm);
}
}
return descendentSchemas;
}

protected String getRefClassWithModule(String ref, String sourceJsonPath) {
String refClass = toRefClass(ref, sourceJsonPath);
protected String getRefClassWithModule(String ref, String sourceJsonPath, String expectedComponentType) {
String refClass = toRefClass(ref, sourceJsonPath, expectedComponentType);
return refClass;
}

Expand Down Expand Up @@ -2572,11 +2572,11 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
if (ModelUtils.getSchema(openAPI, name) == null) {
LOGGER.error("Failed to lookup the schema '{}' when processing the discriminator mapping of oneOf/anyOf. Please check to ensure it's defined properly.", name);
} else {
modelName = getRefClassWithModule(e.getValue(), sourceJsonPath);
modelName = getRefClassWithModule(e.getValue(), sourceJsonPath, "schemas");
}
} else {
String ref = "#/components/schemas/" + value;
modelName = getRefClassWithModule(ref, sourceJsonPath);
modelName = getRefClassWithModule(ref, sourceJsonPath, "schemas");
}
if (modelName != null) {
uniqueDescendants.add(new MappedModel(e.getKey(), modelName));
Expand Down Expand Up @@ -3087,9 +3087,10 @@ public CodegenSchema fromSchema(Schema p, String sourceJsonPath, String currentJ
property.setRef(ref);
property.setRefClass(toRefClass(
ref,
sourceJsonPath
sourceJsonPath,
"schemas"
));
property.setRefModule(toRefModule(ref, "schemas", sourceJsonPath));
property.setRefModule(toRefModule(ref, sourceJsonPath, "schemas"));
}
String example = toExampleValue(p);
property.setExample(example);
Expand All @@ -3103,7 +3104,7 @@ public CodegenSchema fromSchema(Schema p, String sourceJsonPath, String currentJ
return property;
}

public String toRefClass(String ref, String sourceJsonPath) {
public String toRefClass(String ref, String sourceJsonPath, String expectedComponentType) {
String[] refPieces = ref.split("/");
return toModelName(refPieces[refPieces.length-1]);
}
Expand Down Expand Up @@ -3324,13 +3325,13 @@ public CodegenOperation fromOperation(String path,
i++;

if (p.isQueryParam) {
queryParams.add(p.copy());
queryParams.add(p);
} else if (p.isPathParam) {
pathParams.add(p.copy());
pathParams.add(p);
} else if (p.isHeaderParam) {
headerParams.add(p.copy());
headerParams.add(p);
} else if (p.isCookieParam) {
cookieParams.add(p.copy());
cookieParams.add(p);
} else {
LOGGER.warn("Unknown parameter type for {}", p.baseName);
}
Expand All @@ -3341,9 +3342,9 @@ public CodegenOperation fromOperation(String path,
// create optional, required parameters
for (CodegenParameter cp : allParams) {
if (cp.required) { //required parameters
requiredParams.add(cp.copy());
requiredParams.add(cp);
} else { // optional parameters
optionalParams.add(cp.copy());
optionalParams.add(cp);
}
}

Expand Down Expand Up @@ -5024,7 +5025,7 @@ public String toRequestBodyFilename(String componentName) {
return toModuleFilename(componentName);
}

protected String toRefModule(String ref, String expectedComponentType, String sourceJsonPath) {
protected String toRefModule(String ref, String sourceJsonPath, String expectedComponentType) {
// ref #/components/schemas/SomeModel -> some_model
// ref #/components/requestBodies/SomeBody -> some_body
// ref #/components/parameters/SomeParam -> some_param
Expand Down Expand Up @@ -5062,8 +5063,10 @@ protected String toRefModule(String ref, String expectedComponentType, String so
private void setLocationInfo(String ref, OpenapiComponent instance, String sourceJsonPath, String expectedComponentType) {
if (ref != null) {
instance.setRef(ref);
String refModule = toRefModule(ref, expectedComponentType, sourceJsonPath);
String refModule = toRefModule(ref, sourceJsonPath, expectedComponentType);
instance.setRefModule(refModule);
String refClass = toRefClass(ref, sourceJsonPath, expectedComponentType);
instance.setRefClass(refClass);
}
String[] refPieces = sourceJsonPath.split("/");
if (sourceJsonPath.startsWith("#/components/") && refPieces.length == 4) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ public interface OpenapiComponent {

void setRefModule(String refModule);

// TODO add refClass here when all components use classes in their python definition
String getRefClass();

void setRefClass(String refClass);
}
Loading