Skip to content

feat: add readonly flag #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 17, 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
20 changes: 20 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# Contributing

## Build

```bash
pnpm run build
```

## Test

Snapshot Update

```bash
pnpm run update:snapshot
```

## Shortcut

```bash
pnpm run build && pnpm run test:code:gen && pnpm run update:snapshot
```
5 changes: 5 additions & 0 deletions src/code-templates/_shared/ApiClientArgument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const createHeaders = (factory: TsGenerator.Factory.Type, { convertedParams }: C
if (convertedParams.has2OrMoreRequestContentTypes) {
members.push(
factory.PropertySignature.create({
readOnly: false,
name: `"Content-Type"`,
optional: false,
type: factory.TypeReferenceNode.create({ name: "T" }),
Expand All @@ -67,6 +68,7 @@ const createHeaders = (factory: TsGenerator.Factory.Type, { convertedParams }: C
if (convertedParams.has2OrMoreSuccessResponseContentTypes) {
members.push(
factory.PropertySignature.create({
readOnly: false,
name: `Accept`,
optional: false,
type: factory.TypeReferenceNode.create({ name: "U" }),
Expand Down Expand Up @@ -117,6 +119,7 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.
const headerDeclaration = createHeaders(factory, params);
if (headerDeclaration) {
const extraHeader = factory.PropertySignature.create({
readOnly: false,
name: "headers",
optional: false,
type: headerDeclaration,
Expand All @@ -126,6 +129,7 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.

if (convertedParams.hasParameter) {
const parameter = factory.PropertySignature.create({
readOnly: false,
name: "parameter",
optional: false,
type: factory.TypeReferenceNode.create({
Expand All @@ -137,6 +141,7 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.

if (convertedParams.hasRequestBody) {
const requestBody = factory.PropertySignature.create({
readOnly: false,
name: "requestBody",
optional: false,
type: factory.IndexedAccessTypeNode.create({
Expand Down
15 changes: 15 additions & 0 deletions src/code-templates/_shared/ApiClientInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ const createQueryParamsDeclarations = (factory: TsGenerator.Factory.Type) => {
name: "QueryParameter",
members: [
factory.PropertySignature.create({
readOnly: false,
name: "value",
optional: false,
type: factory.TypeNode.create({ type: "any" }),
}),
factory.PropertySignature.create({
readOnly: false,
name: "style",
optional: true,
type: factory.TypeNode.create({ type: "string", enum: ["form", "spaceDelimited", "pipeDelimited", "deepObject"] }),
}),
factory.PropertySignature.create({
readOnly: false,
name: "explode",
optional: false,
type: factory.TypeNode.create({ type: "boolean" }),
Expand Down Expand Up @@ -113,32 +116,37 @@ const createEncodingInterface = (factory: TsGenerator.Factory.Type) => {
members: [
factory.PropertySignature.create({
name: "contentType",
readOnly: true,
optional: true,
type: factory.TypeReferenceNode.create({
name: "string",
}),
}),
factory.PropertySignature.create({
name: "headers",
readOnly: false,
optional: true,
type: factory.TypeReferenceNode.create({
name: "Record<string, any>",
}),
}),
factory.PropertySignature.create({
name: "style",
readOnly: true,
optional: true,
type: factory.TypeNode.create({ type: "string", enum: ["form", "spaceDelimited", "pipeDelimited", "deepObject"] }),
}),
factory.PropertySignature.create({
name: "explode",
readOnly: true,
optional: true,
type: factory.TypeReferenceNode.create({
name: "boolean",
}),
}),
factory.PropertySignature.create({
name: "allowReserved",
readOnly: true,
optional: true,
type: factory.TypeReferenceNode.create({
name: "boolean",
Expand Down Expand Up @@ -219,6 +227,7 @@ export const create = (
});

const requestFunction = factory.PropertySignature.create({
readOnly: false,
name: "request",
optional: false,
type: functionType,
Expand All @@ -230,32 +239,38 @@ export const create = (
members: [
factory.PropertySignature.create({
name: `httpMethod`,
readOnly: true,
optional: false,
type: factory.TypeReferenceNode.create({ name: "HttpMethod" }),
}),
factory.PropertySignature.create({
name: methodType === "currying-function" ? "uri" : "url",
readOnly: true,
optional: false,
type: factory.TypeReferenceNode.create({ name: "string" }),
}),
factory.PropertySignature.create({
name: `headers`,
readOnly: false,
optional: false,
type: objectLikeOrAnyType,
}),
factory.PropertySignature.create({
name: `requestBody`,
readOnly: false,
optional: true,
type: objectLikeOrAnyType,
}),
factory.PropertySignature.create({
name: `requestBodyEncoding`,
readOnly: false,
optional: true,
type: factory.TypeReferenceNode.create({ name: "Record<string, Encoding>" }),
}),
factory.PropertySignature.create({
name: `queryParameters`,
optional: true,
readOnly: false,
type: factory.UnionTypeNode.create({
typeNodes: [
factory.TypeReferenceNode.create({
Expand Down
3 changes: 3 additions & 0 deletions src/internal/OpenApiTools/components/Header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const generatePropertySignature = (
if (reference.type === "local") {
context.setReferenceHandler(currentPoint, reference);
return factory.PropertySignature.create({
readOnly: false,
name: converterContext.escapePropertySignatureName(name),
optional: false,
type: factory.TypeReferenceNode.create({
Expand All @@ -45,6 +46,7 @@ export const generatePropertySignature = (
});
}
return factory.PropertySignature.create({
readOnly: false,
name: converterContext.escapePropertySignatureName(name),
optional: false,
type: factory.TypeReferenceNode.create({
Expand All @@ -53,6 +55,7 @@ export const generatePropertySignature = (
});
}
return factory.PropertySignature.create({
readOnly: false,
name: converterContext.escapePropertySignatureName(name),
optional: false,
type: ToTypeNode.convert(entryPoint, currentPoint, factory, header.schema || { type: "null" }, context, converterContext),
Expand Down
1 change: 1 addition & 0 deletions src/internal/OpenApiTools/components/MediaType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const generatePropertySignature = (
converterContext: ConverterContext.Types,
): ts.PropertySignature => {
return factory.PropertySignature.create({
readOnly: false,
name: converterContext.escapePropertySignatureName(protocol),
optional: false,
type: ToTypeNode.convert(entryPoint, currentPoint, factory, schema, context, converterContext),
Expand Down
3 changes: 3 additions & 0 deletions src/internal/OpenApiTools/components/Parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const generatePropertySignatureObject = (
const isPathProperty = localRef.in === "path";
const name = converterContext.escapePropertySignatureName(localRef.name);
const typeElement = factory.PropertySignature.create({
readOnly: false,
name: name,
optional: isPathProperty ? false : !localRef.required,
comment: localRef.description,
Expand All @@ -68,6 +69,7 @@ export const generatePropertySignatureObject = (
const isPathProperty = reference.data.in === "path";
const name = converterContext.escapePropertySignatureName(reference.data.name);
const typeElement = factory.PropertySignature.create({
readOnly: false,
name: name,
optional: isPathProperty ? false : !reference.data.required,
comment: reference.data.description,
Expand All @@ -88,6 +90,7 @@ export const generatePropertySignatureObject = (
const isPathProperty = parameter.in === "path";
const name = converterContext.escapePropertySignatureName(parameter.name);
const typeElement = factory.PropertySignature.create({
readOnly: false,
name: name,
optional: isPathProperty ? false : !parameter.required,
type: ToTypeNode.convert(entryPoint, currentPoint, factory, parameter.schema || { type: "null" }, context, converterContext),
Expand Down
2 changes: 2 additions & 0 deletions src/internal/OpenApiTools/components/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const generatePropertySignatures = (
return Object.entries(schema.properties).map(([propertyName, property]) => {
if (!property) {
return factory.PropertySignature.create({
readOnly: false,
name: convertContext.escapePropertySignatureName(propertyName),
optional: !required.includes(propertyName),
comment: [schema.title, schema.description].filter(v => !!v).join("\n\n"),
Expand All @@ -48,6 +49,7 @@ export const generatePropertySignatures = (
});
}
return factory.PropertySignature.create({
readOnly: typeof property !== "boolean" ? !!property.readOnly : false,
name: convertContext.escapePropertySignatureName(propertyName),
optional: !required.includes(propertyName),
type: ToTypeNode.convert(entryPoint, currentPoint, factory, property, context, convertContext, { parent: schema }),
Expand Down
4 changes: 4 additions & 0 deletions src/internal/OpenApiTools/components/SecuritySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ export const generatePropertySignatures = (
): ts.PropertySignature[] => {
const signatures: ts.PropertySignature[] = [
factory.PropertySignature.create({
readOnly: false,
name: "type",
optional: false,
type: factory.LiteralTypeNode.create({ value: securitySchema.type }),
}),
factory.PropertySignature.create({
readOnly: false,
name: "name",
optional: false,
type: factory.LiteralTypeNode.create({ value: securitySchema.name }),
}),
factory.PropertySignature.create({
readOnly: false,
name: "in",
optional: false,
type: factory.LiteralTypeNode.create({ value: securitySchema.in }),
}),
factory.PropertySignature.create({
readOnly: false,
name: "openIdConnectUrl",
optional: false,
type: factory.LiteralTypeNode.create({ value: securitySchema.openIdConnectUrl }),
Expand Down
1 change: 1 addition & 0 deletions src/internal/OpenApiTools/toTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export const convert: Convert = (

const value: ts.PropertySignature[] = Object.entries(schema.properties || {}).map(([name, jsonSchema]) => {
return factory.PropertySignature.create({
readOnly: typeof jsonSchema !== "boolean" ? !!jsonSchema.readOnly : false,
name: converterContext.escapePropertySignatureName(name),
type: convert(entryPoint, currentPoint, factory, jsonSchema, context, converterContext, { parent: schema.properties }),
optional: !required.includes(name),
Expand Down
4 changes: 3 additions & 1 deletion src/internal/TsGenerator/factory/PropertySignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { generateComment } from "./utils";

export interface Params {
name: string;
readOnly: boolean;
optional: boolean;
type: ts.TypeNode;
comment?: string;
Expand All @@ -16,8 +17,9 @@ export interface Factory {
export const create =
({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] =>
(params: Params): ts.PropertySignature => {

const node = factory.createPropertySignature(
undefined,
params.readOnly ? [factory.createModifier(ts.SyntaxKind.ReadonlyKeyword)] : undefined,
params.name,
params.optional ? factory.createToken(ts.SyntaxKind.QuestionToken) : undefined,
params.type,
Expand Down
2 changes: 1 addition & 1 deletion src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const Name = "@himenon/openapi-typescript-code-generator";
export const Version = "0.25.0";
export const Version = "0.26.1";
12 changes: 6 additions & 6 deletions test/__tests__/class/__snapshots__/argo-rollout-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3827,15 +3827,15 @@ export namespace ErrorResponse {
export type RolloutService_Version = void;
}
export interface Encoding {
contentType?: string;
readonly contentType?: string;
headers?: Record<string, any>;
style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
explode?: boolean;
allowReserved?: boolean;
readonly style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
readonly explode?: boolean;
readonly allowReserved?: boolean;
}
export interface RequestArgs {
httpMethod: HttpMethod;
url: string;
readonly httpMethod: HttpMethod;
readonly url: string;
headers: ObjectLike | any;
requestBody?: ObjectLike | any;
requestBodyEncoding?: Record<string, Encoding>;
Expand Down
12 changes: 6 additions & 6 deletions test/__tests__/class/__snapshots__/format.domain.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ export interface QueryParameters {
export type SuccessResponses = void;
export namespace ErrorResponse { }
export interface Encoding {
contentType?: string;
readonly contentType?: string;
headers?: Record<string, any>;
style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
explode?: boolean;
allowReserved?: boolean;
readonly style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
readonly explode?: boolean;
readonly allowReserved?: boolean;
}
export interface RequestArgs {
httpMethod: HttpMethod;
url: string;
readonly httpMethod: HttpMethod;
readonly url: string;
headers: ObjectLike | any;
requestBody?: ObjectLike | any;
requestBodyEncoding?: Record<string, Encoding>;
Expand Down
12 changes: 6 additions & 6 deletions test/__tests__/class/__snapshots__/kubernetes-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41026,15 +41026,15 @@ export namespace ErrorResponse {
export type getCodeVersion = void;
}
export interface Encoding {
contentType?: string;
readonly contentType?: string;
headers?: Record<string, any>;
style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
explode?: boolean;
allowReserved?: boolean;
readonly style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
readonly explode?: boolean;
readonly allowReserved?: boolean;
}
export interface RequestArgs {
httpMethod: HttpMethod;
url: string;
readonly httpMethod: HttpMethod;
readonly url: string;
headers: ObjectLike | any;
requestBody?: ObjectLike | any;
requestBodyEncoding?: Record<string, Encoding>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ export namespace ErrorResponse {
export type patchOneOf = void;
}
export interface Encoding {
contentType?: string;
readonly contentType?: string;
headers?: Record<string, any>;
style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
explode?: boolean;
allowReserved?: boolean;
readonly style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
readonly explode?: boolean;
readonly allowReserved?: boolean;
}
export interface RequestArgs {
httpMethod: HttpMethod;
url: string;
readonly httpMethod: HttpMethod;
readonly url: string;
headers: ObjectLike | any;
requestBody?: ObjectLike | any;
requestBodyEncoding?: Record<string, Encoding>;
Expand Down
Loading