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

v2 adds and uses CodegenRefInfo class to consolidate $ref info: ref/refClass/refModule #118

Merged
merged 20 commits into from
Jan 8, 2023
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,18 @@

package org.openapitools.codegen;

import org.openapitools.codegen.utils.ModelUtils;

import java.util.Map;
import java.util.Objects;

/**
* Describes a single operation parameter in the OAS specification.
* A unique parameter is defined by a combination of a name and location.
* Parameters may be located in a path, query, header or cookie.
*/
public class CodegenHeader extends CodegenRequestBody {
public boolean isExplode;
public String style;

public boolean isDeprecated;
protected CodegenSchema schema;

public CodegenSchema getSetSchema() {
if (schema != null) {
return schema;
}
if (content != null) {
for (CodegenMediaType codegenMediaType: content.values()) {
return codegenMediaType.getSchema();
}
}
return null;
}

public String getSetSchemaJsonPath(String jsonPath) {
if (schema != null) {
return jsonPath + "/schema";
}
if (content != null) {
for (Map.Entry<String, CodegenMediaType> entry: content.entrySet()) {
if (entry.getValue().getSchema() != null) {
String contentType = entry.getKey();
return jsonPath + "/content/" + ModelUtils.encodeSlashes(contentType) + "/schema";
}
}
}
return null;
}
public class CodegenHeader extends CodegenHeaderBase implements OpenApiLocation<CodegenHeader> {
protected CodegenRefInfo<CodegenHeader> refInfo;
@Override
public int hashCode() {
return Objects.hash(refClass, name, isExplode, description, unescapedDescription, style, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, ref, refModule, imports, componentModule);
return Objects.hash(name, isExplode, description, unescapedDescription, style, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, refInfo, imports, componentModule);
}

@Override
Expand All @@ -71,18 +37,12 @@ public boolean equals(Object o) {
if (!(o instanceof CodegenHeader)) return false;
if (! super.equals(o)) return false;
CodegenHeader that = (CodegenHeader) o;
return isExplode == that.isExplode &&
isDeprecated == that.isDeprecated &&
Objects.equals(schema, that.getSchema()) &&
Objects.equals(style, that.style);
return Objects.equals(refInfo, that.refInfo);
}

protected void addInstanceInfo(StringBuilder sb) {
super.addInstanceInfo(sb);
sb.append(", isExplode=").append(isExplode);
sb.append(", style='").append(style).append('\'');
sb.append(", isDeprecated=").append(isDeprecated);
sb.append(", schema=").append(schema);
sb.append(", refInfo=").append(refInfo);
}

@Override
Expand All @@ -93,15 +53,8 @@ public String toString() {
return sb.toString();
}

@Override
public CodegenHeader getRef() { return (CodegenHeader) ref; }
public CodegenRefInfo<CodegenHeader> getRefInfo() { return refInfo; }

public CodegenSchema getSchema() {
return schema;
}

public void setSchema(CodegenSchema schema) {
this.schema = schema;
}
public void setRefInfo(CodegenRefInfo<CodegenHeader> refInfo) { this.refInfo = refInfo; }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openapitools.codegen;

import org.openapitools.codegen.utils.ModelUtils;

import java.util.Map;
import java.util.Objects;

/**
* Describes a single operation parameter in the OAS specification.
* A unique parameter is defined by a combination of a name and location.
* Parameters may be located in a path, query, header or cookie.
*/
abstract class CodegenHeaderBase extends CodegenRequestBodyBase {
public boolean isExplode;
public String style;
public boolean isDeprecated;
protected CodegenSchema schema;

public CodegenSchema getSetSchema() {
if (schema != null) {
return schema;
}
if (content != null) {
for (CodegenMediaType codegenMediaType: content.values()) {
return codegenMediaType.getSchema();
}
}
return null;
}

public String getSetSchemaJsonPath(String jsonPath) {
if (schema != null) {
return jsonPath + "/schema";
}
if (content != null) {
for (Map.Entry<String, CodegenMediaType> entry: content.entrySet()) {
if (entry.getValue().getSchema() != null) {
String contentType = entry.getKey();
return jsonPath + "/content/" + ModelUtils.encodeSlashes(contentType) + "/schema";
}
}
}
return null;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CodegenHeaderBase)) return false;
if (! super.equals(o)) return false;
CodegenHeaderBase that = (CodegenHeaderBase) o;
return isExplode == that.isExplode &&
isDeprecated == that.isDeprecated &&
Objects.equals(schema, that.getSchema()) &&
Objects.equals(style, that.style);
}

protected void addInstanceInfo(StringBuilder sb) {
super.addInstanceInfo(sb);
sb.append(", isExplode=").append(isExplode);
sb.append(", style='").append(style).append('\'');
sb.append(", isDeprecated=").append(isDeprecated);
sb.append(", schema=").append(schema);
}

public CodegenSchema getSchema() {
return schema;
}

public void setSchema(CodegenSchema schema) {
this.schema = schema;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class CodegenOperation {
public List<CodegenParameter> headerParams = new ArrayList<CodegenParameter>();
public List<CodegenParameter> implicitHeadersParams = new ArrayList<CodegenParameter>();
public List<CodegenParameter> cookieParams = new ArrayList<CodegenParameter>();
public List<CodegenRequestBody> requiredParams = new ArrayList<CodegenRequestBody>();
public List<CodegenRequestBody> optionalParams = new ArrayList<CodegenRequestBody>();
public List<CodegenRequestBodyBase> requiredParams = new ArrayList<CodegenRequestBodyBase>();
public List<CodegenRequestBodyBase> optionalParams = new ArrayList<CodegenRequestBodyBase>();
public List<CodegenSecurity> authMethods;
public Map<String, CodegenTag> tags;
public TreeMap<String, CodegenResponse> responses = null;
Expand Down Expand Up @@ -176,9 +176,9 @@ public Map<String, CodegenOperation> getContentTypeToOperation() {
return null;
}
LinkedHashMap<String, CodegenMediaType> content;
CodegenRequestBody ref = (CodegenRequestBody) requestBody.getRef();
if (ref != null) {
content = ref.getContent();
CodegenRefInfo<CodegenRequestBody> refInfo = requestBody.getRefInfo();
if (refInfo != null) {
content = refInfo.getRef().getContent();
} else {
content = requestBody.getContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@
* A unique parameter is defined by a combination of a name and location.
* Parameters may be located in a path, query, header or cookie.
*/
public class CodegenParameter extends CodegenHeader {
public class CodegenParameter extends CodegenHeaderBase implements OpenApiLocation<CodegenParameter> {
public boolean isFormParam, isQueryParam, isPathParam, isHeaderParam,
isCookieParam, isBodyParam, isAllowEmptyValue, isDeepObject;
// stores the openapi name property
public String baseName;
protected CodegenRefInfo<CodegenParameter> refInfo;

public CodegenParameter getRef() { return (CodegenParameter) ref; }
public CodegenRefInfo<CodegenParameter> getRefInfo() { return refInfo; }

public void setRefInfo(CodegenRefInfo<CodegenParameter> refInfo) { this.refInfo = refInfo; }

@Override
public int hashCode() {
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);
return Objects.hash(name, isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isExplode, baseName, description, unescapedDescription, style, isDeepObject, isAllowEmptyValue, example, jsonSchema, vendorExtensions, isDeprecated, required, schema, content, refInfo, imports, componentModule);
}

@Override
Expand All @@ -49,6 +52,7 @@ public boolean equals(Object o) {
isHeaderParam == that.isHeaderParam &&
isCookieParam == that.isCookieParam &&
isBodyParam == that.isBodyParam &&
Objects.equals(refInfo, that.refInfo) &&
Objects.equals(baseName, that.baseName) &&
Objects.equals(isDeepObject, that.isDeepObject);
}
Expand All @@ -64,6 +68,7 @@ protected void addInstanceInfo(StringBuilder sb) {
sb.append(", deepObject='").append(isDeepObject).append('\'');
sb.append(", allowEmptyValue='").append(isAllowEmptyValue).append('\'');
sb.append(", baseName='").append(baseName).append('\'');
sb.append(", refInfo='").append(refInfo).append('\'');
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.openapitools.codegen;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.Objects;

@JsonIgnoreProperties({"ref"})
public class CodegenRefInfo<T> {
private T ref;
private String refClass;
private String refModule;

protected void addInstanceInfo(StringBuilder sb) {
sb.append("refModule=").append(refModule);
sb.append(", refClass=").append(refClass);
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("CodegenRefInfo{");
addInstanceInfo(sb);
sb.append('}');
return sb.toString();
}

@Override
public int hashCode() {
// ref must be omitted here for generation to work
return Objects.hash(refClass, refModule);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CodegenRefInfo)) return false;
CodegenRefInfo that = (CodegenRefInfo) o;
return Objects.equals(refModule, that.refModule) &&
Objects.equals(refClass, that.refClass) &&
Objects.equals(ref, that.ref);
}

public CodegenRefInfo(T ref, String refClass, String refModule) {
this.ref = ref;
this.refClass = refClass;
this.refModule = refModule ;
}

public T getRef() { return ref; }
public String getRefClass() { return refClass; }
public String getRefModule() { return refModule; }
}
Loading