Skip to content
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

fix(type-safe-api): fix named composite models and composite model types #878

Merged
merged 2 commits into from
Oct 30, 2024
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */
import * as fs from "fs";
import * as util from "util";
import SwaggerParser from "@apidevtools/swagger-parser";
import { parse } from "ts-command-line-args";
import * as ejs from "ejs";
Expand Down Expand Up @@ -359,6 +360,10 @@ const toTypeScriptType = (property: parseOpenapi.Model): string => {
return `Array<${property.link && property.link.export !== "enum" ? toTypeScriptType(property.link) : property.type}>`;
case "dictionary":
return `{ [key: string]: ${property.link && property.link.export !== "enum" ? toTypeScriptType(property.link) : property.type}; }`;
case "one-of":
case "any-of":
case "all-of":
return property.name;
default:
return property.type;
}
Expand Down Expand Up @@ -412,6 +417,10 @@ const toJavaType = (property: parseOpenapi.Model): string => {
return `${property.uniqueItems ? 'Set' : 'List'}<${property.link && property.link.export !== "enum" ? toJavaType(property.link) : property.type}>`;
case "dictionary":
return `Map<String, ${property.link && property.link.export !== "enum" ? toJavaType(property.link) : property.type}>`;
case "one-of":
case "any-of":
case "all-of":
return property.name;
default:
// "any" has export = interface
if (PRIMITIVE_TYPES.has(property.type)) {
Expand Down Expand Up @@ -461,6 +470,10 @@ const toPythonType = (property: parseOpenapi.Model): string => {
return `List[${property.link && property.link.export !== "enum" ? toPythonType(property.link) : property.type}]`;
case "dictionary":
return `Dict[str, ${property.link && property.link.export !== "enum" ? toPythonType(property.link) : property.type}]`;
case "one-of":
case "any-of":
case "all-of":
return property.name;
default:
// "any" has export = interface
if (PRIMITIVE_TYPES.has(property.type)) {
Expand Down Expand Up @@ -649,7 +662,7 @@ const filterInlineCompositeSchemas = (schemas: (OpenAPIV3.SchemaObject | OpenAPI
let inlineSchemaIndex = 0;
return schemas.flatMap((s, i) => {
if (hasSubSchemasToVisit(s)) {
const subSchema: SubSchema = { nameParts: [...nameParts, `${namePartPrefix}${inlineSchemaIndex === 0 ? '' : inlineSchemaIndex}`], schema: s, prop: `${prop}.[${i}]` };
const subSchema: SubSchema = { nameParts: s.title ? [_upperFirst(_camelCase(s.title))] : [...nameParts, `${namePartPrefix}${inlineSchemaIndex === 0 ? '' : inlineSchemaIndex}`], schema: s, prop: `${prop}.[${i}]` };
inlineSchemaIndex++;
return [subSchema];
}
Expand Down Expand Up @@ -1034,7 +1047,7 @@ export default async (argv: string[], rootScriptDir: string) => {
const data = await buildData(spec, JSON.parse(args.metadata ?? '{}'));

if (args.printData) {
console.log(JSON.stringify(data, null, 2));
console.log(util.inspect(data, { depth: 100 }));
}

// Read all .ejs files in each template directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export type <%- model.name %> =<% model.properties.forEach((composedType, i) =>
<%_ } _%>
*/
export interface <%= model.name %> {
<%_ if (model.additionalProperties) { _%>
[key: string]: <%- model.additionalProperties.typescriptType %>;
<%_ if (model.export === "dictionary" && model.link) { _%>
[key: string]: <%- model.link.typescriptType %>;
<%_ } _%>
<%_ model.properties.forEach((property) => { _%>
/**
Expand Down Expand Up @@ -126,7 +126,7 @@ export function <%= model.name %>FromJSONTyped(json: any, ignoreDiscriminator: b
<%_ } else { _%>
return {

<%_ if (model.additionalProperties) { _%>
<%_ if (model.export === "dictionary") { _%>
...json,
<%_ } _%>
<%_ model.properties.forEach((property) => { _%>
Expand Down Expand Up @@ -173,7 +173,7 @@ export function <%= model.name %>ToJSON(value?: <%= model.name %> | null): any {
<%_ } else { _%>
return {

<%_ if (model.additionalProperties) { _%>
<%_ if (model.export === "dictionary") { _%>
...value,
<%_ } _%>
<%_ model.properties.forEach((property) => { _%>
Expand Down
71 changes: 70 additions & 1 deletion packages/type-safe-api/test/resources/specs/edge-cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,80 @@ paths:
string
required:
- someProperty
/named-one-of:
post:
operationId: namedOneOf
responses:
200:
description: ok
content:
application/json:
schema:
$ref: "#/components/schemas/NamedOneOfUnion"
/array-of-one-ofs:
post:
operationId: arrayOfOneOfs
responses:
200:
description: ok
content:
application/json:
schema:
$ref: "#/components/schemas/ArrayOfOneOfs"
/additional-properties:
post:
operationId: dictionary
responses:
200:
description: ok
content:
application/json:
schema:
$ref: "#/components/schemas/AdditionalPropertiesResponse"
components:
schemas:
MyEnum:
type: string
enum:
- one
- two
- three
- three
NamedOneOfUnion:
oneOf:
- type: object
title: namedOneOf
properties:
foo:
type: string
- type: object
title: anotherNamedOneOf
properties:
bar:
type: string
ArrayOfOneOfs:
type: object
properties:
oneOfs:
type: array
items:
$ref: "#/components/schemas/NamedOneOfUnion"
AdditionalPropertiesResponse:
type: object
properties:
dictionaryOfObjects:
$ref: "#/components/schemas/Dictionary"
dictionaryOfPrimitives:
type: object
additionalProperties:
type: string
Dictionary:
type: object
additionalProperties:
type: array
items:
$ref: "#/components/schemas/SomeObject"
SomeObject:
type: object
properties:
a:
type: string
Loading
Loading