Skip to content

Commit

Permalink
[typescript-fetch] Omit readOnly properties from request models (Open…
Browse files Browse the repository at this point in the history
…APITools#18065)

* omit readOnly properties on requests

* update the sample
  • Loading branch information
kota65535 authored Mar 16, 2024
1 parent ae9598d commit 0b4cf0a
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 10 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 == null) {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function ClubFromJSONTyped(json: any, ignoreDiscriminator: boolean): Club
};
}

export function ClubToJSON(value?: Club | null): any {
export function ClubToJSON(value?: Omit<Club, 'owner'> | null): any {
if (value == null) {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function HasOnlyReadOnlyFromJSONTyped(json: any, ignoreDiscriminator: boo
};
}

export function HasOnlyReadOnlyToJSON(value?: HasOnlyReadOnly | null): any {
export function HasOnlyReadOnlyToJSON(value?: Omit<HasOnlyReadOnly, 'bar'|'foo'> | null): any {
if (value == null) {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function NameFromJSONTyped(json: any, ignoreDiscriminator: boolean): Name
};
}

export function NameToJSON(value?: Name | null): any {
export function NameToJSON(value?: Omit<Name, 'snake_case'|'123Number'> | null): any {
if (value == null) {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function ReadOnlyFirstFromJSONTyped(json: any, ignoreDiscriminator: boole
};
}

export function ReadOnlyFirstToJSON(value?: ReadOnlyFirst | null): any {
export function ReadOnlyFirstToJSON(value?: Omit<ReadOnlyFirst, 'bar'> | null): any {
if (value == null) {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function HasOnlyReadOnlyFromJSONTyped(json: any, ignoreDiscriminator: boo
};
}

export function HasOnlyReadOnlyToJSON(value?: HasOnlyReadOnly | null): any {
export function HasOnlyReadOnlyToJSON(value?: Omit<HasOnlyReadOnly, 'bar'|'foo'> | null): any {
if (value == null) {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function NameFromJSONTyped(json: any, ignoreDiscriminator: boolean): Name
};
}

export function NameToJSON(value?: Name | null): any {
export function NameToJSON(value?: Omit<Name, 'snake_case'|'123Number'> | null): any {
if (value == null) {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function ReadOnlyFirstFromJSONTyped(json: any, ignoreDiscriminator: boole
};
}

export function ReadOnlyFirstToJSON(value?: ReadOnlyFirst | null): any {
export function ReadOnlyFirstToJSON(value?: Omit<ReadOnlyFirst, 'bar'> | null): any {
if (value == null) {
return value;
}
Expand Down

0 comments on commit 0b4cf0a

Please sign in to comment.