Skip to content

Commit

Permalink
fix(openapi): avoid conflicts with the predefined Subscription type…
Browse files Browse the repository at this point in the history
… definitions (#4792)
  • Loading branch information
ardatan authored Nov 7, 2022
1 parent 27d2612 commit d63bd93
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/late-jeans-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'json-machete': patch
'@omnigraph/json-schema': patch
'@omnigraph/openapi': patch
---

Add suffix to the existing "Subscription" types to avoid conflicts
3 changes: 3 additions & 0 deletions packages/json-machete/src/healUntitledDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export function handleUntitledDefinitions(schemaDocument: any) {
if (!definition.$ref) {
if (!definition.title) {
definition.title = definitionName;
if (definition.title === 'Subscription') {
definition.title += '_';
}
} else {
const seenDefinition = seen.get(definition.title);
if (seenDefinition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ export function getComposerFromJSONSchema(schema: JSONSchema, logger: Logger): P
...subSchema,
};
case 'Subscription':
if (path.startsWith('/properties/subscription')) {
if (path === '/properties/subscription') {
return {
output: schemaComposer.Subscription,
...subSchema,
Expand Down
311 changes: 311 additions & 0 deletions packages/loaders/openapi/tests/__snapshots__/schemas.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -689996,6 +689996,317 @@ enum mutationInput_PostWebhookEndpointsWebhookEndpoint_input_enabled_events_item
scalar mutationInput_PostWebhookEndpointsWebhookEndpoint_input_expand_items"
`;

exports[`Schemas SubscriptionType should generate the correct bundle: SubscriptionType 1`] = `
{
"baseUrl": "http://api.example.com/v1",
"name": "SubscriptionType",
"operationHeaders": {},
"operations": [
{
"argTypeMap": {
"company_id": {
"description": "Company ID",
"format": "int64",
"name": "company_id",
"nullable": false,
"type": "integer",
},
},
"description": undefined,
"field": "a",
"method": "GET",
"operationHeaders": undefined,
"path": "/a/{args.company_id}/a",
"responseByStatusCode": {},
"schemaHeaders": undefined,
"type": "query",
},
{
"description": undefined,
"field": "b",
"headers": {
"Content-Type": "application/json",
},
"method": "POST",
"operationHeaders": undefined,
"path": "/b",
"requestSchema": {
"format": "int64",
"title": "b_request",
"type": "integer",
},
"responseByStatusCode": {},
"schemaHeaders": undefined,
"type": "mutation",
},
{
"description": undefined,
"field": "bFailed",
"pubsubTopic": "webhook:put:https://url",
"responseSchema": {
"properties": {
"uuid": {
"format": "uuid",
"type": "string",
},
},
"required": [
"uuid",
],
"title": "subscription_bFailed",
"type": "object",
},
"type": "subscription",
},
{
"argTypeMap": {
"company_id": {
"description": "Company ID",
"format": "int64",
"name": "company_id",
"nullable": false,
"type": "integer",
},
},
"description": "Get a list of subscriptions",
"field": "listSubscriptions",
"headers": {
"accept": "application/json",
},
"method": "GET",
"operationHeaders": undefined,
"path": "/subscriptions",
"responseByStatusCode": {
"200": {
"responseSchema": {
"$resolvedRef": "/components/schemas/Subscription",
"properties": {
"id": {
"type": "string",
},
},
"required": [
"id",
],
"title": "Subscription_",
"type": "object",
},
},
},
"schemaHeaders": undefined,
"type": "query",
},
],
"referencedSchema": {
"$ref": "#/definitions/_schema",
"definitions": {
"Any": {
"oneOf": [
{
"type": "string",
},
{
"type": "integer",
},
{
"type": "boolean",
},
{
"type": "number",
},
{
"$ref": "#/definitions/query_repos_by_owner_by_repo_by_archive_format_by_path_oneOf_4",
},
],
"title": "Any",
},
"Mutation": {
"properties": {
"b": {
"$ref": "#/definitions/Any",
},
},
"readOnly": true,
"title": "Mutation",
"type": "object",
},
"MutationInput": {
"properties": {
"b": {
"$ref": "#/definitions/mutationInput_b",
},
},
"title": "MutationInput",
"type": "object",
"writeOnly": true,
},
"Query": {
"properties": {
"a": {
"$ref": "#/definitions/Any",
},
"listSubscriptions": {
"$ref": "#/definitions/Subscription_",
},
},
"readOnly": true,
"title": "Query",
"type": "object",
},
"QueryInput": {
"properties": {
"a": {
"$ref": "#/definitions/queryInput_a",
},
},
"title": "QueryInput",
"type": "object",
"writeOnly": true,
},
"Subscription": {
"properties": {
"bFailed": {
"$ref": "#/definitions/subscription_bFailed",
},
},
"readOnly": true,
"title": "Subscription",
"type": "object",
},
"SubscriptionInput": {
"additionalProperties": true,
"title": "SubscriptionInput",
"type": "object",
"writeOnly": true,
},
"Subscription_": {
"$resolvedRef": "/components/schemas/Subscription",
"properties": {
"id": {
"type": "string",
},
},
"required": [
"id",
],
"title": "Subscription_",
"type": "object",
},
"_schema": {
"properties": {
"mutation": {
"$ref": "#/definitions/Mutation",
},
"mutationInput": {
"$ref": "#/definitions/MutationInput",
},
"query": {
"$ref": "#/definitions/Query",
},
"queryInput": {
"$ref": "#/definitions/QueryInput",
},
"subscription": {
"$ref": "#/definitions/Subscription",
},
"subscriptionInput": {
"$ref": "#/definitions/SubscriptionInput",
},
},
"required": [
"query",
],
"title": "_schema",
"type": "object",
},
"b_request": {
"format": "int64",
"title": "b_request",
"type": "integer",
},
"mutationInput_b": {
"properties": {
"input": {
"$ref": "#/definitions/b_request",
},
},
"title": "mutationInput_b",
"type": "object",
},
"queryInput_a": {
"properties": {
"company_id": {
"description": "Company ID",
"format": "int64",
"name": "company_id",
"nullable": false,
"type": "integer",
},
},
"title": "queryInput_a",
"type": "object",
},
"subscription_bFailed": {
"properties": {
"uuid": {
"format": "uuid",
"type": "string",
},
},
"required": [
"uuid",
],
"title": "subscription_bFailed",
"type": "object",
},
},
},
}
`;

exports[`Schemas SubscriptionType should generate the correct schema: SubscriptionType 1`] = `
"schema {
query: Query
mutation: Mutation
subscription: Subscription
}

type Query {
a(
"Company ID"
company_id: BigInt!
): JSON
"Get a list of subscriptions"
listSubscriptions: Subscription_
}

"The \`JSON\` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf)."
scalar JSON @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")

"The \`BigInt\` scalar type represents non-fractional signed whole numeric values."
scalar BigInt

type Subscription_ {
id: String!
}

type Mutation {
b(input: BigInt): JSON
}

type Subscription {
"PubSub Topic: webhook:put:https://url"
bFailed: subscription_bFailed
}

type subscription_bFailed {
uuid: UUID!
}

"A field whose value is a generic Universally Unique Identifier: https://en.wikipedia.org/wiki/Universally_unique_identifier."
scalar UUID"
`;

exports[`Schemas Toto should generate the correct bundle: Toto 1`] = `
{
"baseUrl": "https://dev-api.test.com/api/v1",
Expand Down
Loading

0 comments on commit d63bd93

Please sign in to comment.