Skip to content

Commit

Permalink
omit readOnly properties on requests
Browse files Browse the repository at this point in the history
  • Loading branch information
kota65535 committed Feb 27, 2024
1 parent a8efb8e commit 7d446c6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import java.util.stream.Collectors;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.features.DocumentationFeature;
import org.openapitools.codegen.meta.features.SecurityFeature;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.templating.mustache.IndentedLambda;
import org.openapitools.codegen.utils.ModelUtils;
Expand Down Expand Up @@ -986,6 +986,8 @@ private static String getItemsDataType(CodegenProperty items) {
class ExtendedCodegenParameter extends CodegenParameter {
public String dataTypeAlternate;
public boolean isUniqueId; // this parameter represents a unique id (x-isUniqueId: true)
public List<CodegenProperty> readOnlyVars; // a list of read-only properties
public boolean hasReadOnly = false; // indicates the type has at least one read-only property

public boolean itemsAreUniqueId() {
return TypeScriptFetchClientCodegen.itemsAreUniqueId(this.items);
Expand Down Expand Up @@ -1081,8 +1083,11 @@ public ExtendedCodegenParameter(CodegenParameter cp) {
this.minItems = cp.minItems;
this.uniqueItems = cp.uniqueItems;
this.multipleOf = cp.multipleOf;
this.setHasVars(cp.getHasVars());
this.setHasRequired(cp.getHasRequired());
this.setMaxProperties(cp.getMaxProperties());
this.setMinProperties(cp.getMinProperties());
setReadOnlyVars();
}

@Override
Expand Down Expand Up @@ -1123,6 +1128,11 @@ public String toString() {
sb.append(", dataTypeAlternate='").append(dataTypeAlternate).append('\'');
return sb.toString();
}

private void setReadOnlyVars() {
readOnlyVars = vars.stream().filter(v -> v.isReadOnly).collect(Collectors.toList());
hasReadOnly = !readOnlyVars.isEmpty();
}
}

class ExtendedCodegenProperty extends CodegenProperty {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
{{#allParams.0}}
export interface {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request {
{{#allParams}}
{{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}}{{#required}} | null{{/required}}{{/isNullable}}{{/isEnum}};
{{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{#hasReadOnly}}Omit<{{{dataType}}}, {{#readOnlyVars}}'{{baseName}}'{{^-last}}|{{/-last}}{{/readOnlyVars}}>{{/hasReadOnly}}{{^hasReadOnly}}{{{dataType}}}{{/hasReadOnly}}{{#isNullable}}{{#required}} | null{{/required}}{{/isNullable}}{{/isEnum}};
{{/allParams}}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
{{/hasVars}}
}

export function {{classname}}ToJSON(value?: {{classname}} | null): any {
export function {{classname}}ToJSON(value?: {{#hasReadOnly}}Omit<{{classname}}, {{#readOnlyVars}}'{{baseName}}'{{^-last}}|{{/-last}}{{/readOnlyVars}}>{{/hasReadOnly}}{{^hasReadOnly}}{{classname}}{{/hasReadOnly}} | null): any {
{{#hasVars}}
if (value === undefined) {
return undefined;
Expand Down

0 comments on commit 7d446c6

Please sign in to comment.