Skip to content

Commit 88e18f0

Browse files
kota65535zapodot
authored andcommitted
[typescript-fetch] Omit readOnly properties from request models (OpenAPITools#18065)
* omit readOnly properties on requests * update the sample
1 parent f21f1f0 commit 88e18f0

File tree

10 files changed

+20
-10
lines changed

10 files changed

+20
-10
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
import io.swagger.v3.oas.models.responses.ApiResponse;
2727
import io.swagger.v3.oas.models.servers.Server;
2828
import io.swagger.v3.parser.util.SchemaTypeUtil;
29+
import java.util.stream.Collectors;
2930
import org.openapitools.codegen.*;
3031
import org.openapitools.codegen.meta.features.DocumentationFeature;
3132
import org.openapitools.codegen.meta.features.SecurityFeature;
3233
import org.openapitools.codegen.model.ModelMap;
3334
import org.openapitools.codegen.model.ModelsMap;
34-
import org.openapitools.codegen.model.OperationMap;
3535
import org.openapitools.codegen.model.OperationsMap;
3636
import org.openapitools.codegen.templating.mustache.IndentedLambda;
3737
import org.openapitools.codegen.utils.ModelUtils;
@@ -986,6 +986,8 @@ private static String getItemsDataType(CodegenProperty items) {
986986
class ExtendedCodegenParameter extends CodegenParameter {
987987
public String dataTypeAlternate;
988988
public boolean isUniqueId; // this parameter represents a unique id (x-isUniqueId: true)
989+
public List<CodegenProperty> readOnlyVars; // a list of read-only properties
990+
public boolean hasReadOnly = false; // indicates the type has at least one read-only property
989991

990992
public boolean itemsAreUniqueId() {
991993
return TypeScriptFetchClientCodegen.itemsAreUniqueId(this.items);
@@ -1081,8 +1083,11 @@ public ExtendedCodegenParameter(CodegenParameter cp) {
10811083
this.minItems = cp.minItems;
10821084
this.uniqueItems = cp.uniqueItems;
10831085
this.multipleOf = cp.multipleOf;
1086+
this.setHasVars(cp.getHasVars());
1087+
this.setHasRequired(cp.getHasRequired());
10841088
this.setMaxProperties(cp.getMaxProperties());
10851089
this.setMinProperties(cp.getMinProperties());
1090+
setReadOnlyVars();
10861091
}
10871092

10881093
@Override
@@ -1123,6 +1128,11 @@ public String toString() {
11231128
sb.append(", dataTypeAlternate='").append(dataTypeAlternate).append('\'');
11241129
return sb.toString();
11251130
}
1131+
1132+
private void setReadOnlyVars() {
1133+
readOnlyVars = vars.stream().filter(v -> v.isReadOnly).collect(Collectors.toList());
1134+
hasReadOnly = !readOnlyVars.isEmpty();
1135+
}
11261136
}
11271137

11281138
class ExtendedCodegenProperty extends CodegenProperty {

modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
{{#allParams.0}}
2525
export interface {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request {
2626
{{#allParams}}
27-
{{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}}{{#required}} | null{{/required}}{{/isNullable}}{{/isEnum}};
27+
{{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}};
2828
{{/allParams}}
2929
}
3030

modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
100100
{{/hasVars}}
101101
}
102102

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

samples/client/petstore/typescript-fetch/builds/allOf-readonly/models/Club.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function ClubFromJSONTyped(json: any, ignoreDiscriminator: boolean): Club
5555
};
5656
}
5757

58-
export function ClubToJSON(value?: Club | null): any {
58+
export function ClubToJSON(value?: Omit<Club, 'owner'> | null): any {
5959
if (value == null) {
6060
return value;
6161
}

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/HasOnlyReadOnly.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function HasOnlyReadOnlyFromJSONTyped(json: any, ignoreDiscriminator: boo
5555
};
5656
}
5757

58-
export function HasOnlyReadOnlyToJSON(value?: HasOnlyReadOnly | null): any {
58+
export function HasOnlyReadOnlyToJSON(value?: Omit<HasOnlyReadOnly, 'bar'|'foo'> | null): any {
5959
if (value == null) {
6060
return value;
6161
}

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/Name.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function NameFromJSONTyped(json: any, ignoreDiscriminator: boolean): Name
7070
};
7171
}
7272

73-
export function NameToJSON(value?: Name | null): any {
73+
export function NameToJSON(value?: Omit<Name, 'snake_case'|'123Number'> | null): any {
7474
if (value == null) {
7575
return value;
7676
}

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/ReadOnlyFirst.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function ReadOnlyFirstFromJSONTyped(json: any, ignoreDiscriminator: boole
5555
};
5656
}
5757

58-
export function ReadOnlyFirstToJSON(value?: ReadOnlyFirst | null): any {
58+
export function ReadOnlyFirstToJSON(value?: Omit<ReadOnlyFirst, 'bar'> | null): any {
5959
if (value == null) {
6060
return value;
6161
}

samples/client/petstore/typescript-fetch/builds/snakecase-discriminator/models/HasOnlyReadOnly.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function HasOnlyReadOnlyFromJSONTyped(json: any, ignoreDiscriminator: boo
5555
};
5656
}
5757

58-
export function HasOnlyReadOnlyToJSON(value?: HasOnlyReadOnly | null): any {
58+
export function HasOnlyReadOnlyToJSON(value?: Omit<HasOnlyReadOnly, 'bar'|'foo'> | null): any {
5959
if (value == null) {
6060
return value;
6161
}

samples/client/petstore/typescript-fetch/builds/snakecase-discriminator/models/Name.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function NameFromJSONTyped(json: any, ignoreDiscriminator: boolean): Name
7070
};
7171
}
7272

73-
export function NameToJSON(value?: Name | null): any {
73+
export function NameToJSON(value?: Omit<Name, 'snake_case'|'123Number'> | null): any {
7474
if (value == null) {
7575
return value;
7676
}

samples/client/petstore/typescript-fetch/builds/snakecase-discriminator/models/ReadOnlyFirst.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function ReadOnlyFirstFromJSONTyped(json: any, ignoreDiscriminator: boole
5555
};
5656
}
5757

58-
export function ReadOnlyFirstToJSON(value?: ReadOnlyFirst | null): any {
58+
export function ReadOnlyFirstToJSON(value?: Omit<ReadOnlyFirst, 'bar'> | null): any {
5959
if (value == null) {
6060
return value;
6161
}

0 commit comments

Comments
 (0)