[typescript-fetch] Omit readOnly properties from request models (#18065)

* omit readOnly properties on requests

* update the sample
This commit is contained in:
Tomohiko Ozawa
2024-03-16 17:27:39 +09:00
committed by GitHub
parent ae9598dcfc
commit 0b4cf0a027
10 changed files with 20 additions and 10 deletions

View File

@@ -26,12 +26,12 @@ import io.swagger.v3.oas.models.parameters.RequestBody;
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;
@@ -986,6 +986,8 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
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);
@@ -1081,8 +1083,11 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
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
@@ -1123,6 +1128,11 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
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 {

View File

@@ -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}}
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}