Skip to content

feat(openapi): an object type with discriminator reflected as an interface #4378

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 1 commit into from
Aug 25, 2022
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
6 changes: 6 additions & 0 deletions .changeset/beige-timers-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@omnigraph/json-schema": minor
"@omnigraph/openapi": minor
---

If an object type has a discriminator, it becomes an interface type and any other allOf references with that implements that interface
40 changes: 26 additions & 14 deletions packages/loaders/json-schema/src/getComposerFromJSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ComposeOutputType,
ComposeInputType,
EnumTypeComposer,
InterfaceTypeComposer,
} from 'graphql-compose';
import {
getNamedType,
Expand Down Expand Up @@ -498,6 +499,20 @@ export function getComposerFromJSONSchema(schema: JSONSchema, logger: Logger): P
default: subSchema.default,
};
}
const config = {
name: getValidTypeName({
schemaComposer,
isInput: false,
subSchema,
}),
description: subSchema.description,
fields: {},
extensions: {
validateWithJSONSchema,
examples: subSchema.examples,
default: subSchema.default,
},
};
return {
input: schemaComposer.createInputTC({
name: getValidTypeName({
Expand All @@ -512,20 +527,14 @@ export function getComposerFromJSONSchema(schema: JSONSchema, logger: Logger): P
default: subSchema.default,
},
}),
output: schemaComposer.createObjectTC({
name: getValidTypeName({
schemaComposer,
isInput: false,
subSchema,
}),
description: subSchema.description,
fields: {},
extensions: {
validateWithJSONSchema,
examples: subSchema.examples,
default: subSchema.default,
},
}),
output: subSchema.discriminator
? schemaComposer.createInterfaceTC({
...config,
resolveType(root: any) {
return root[subSchema.discriminator];
},
})
: schemaComposer.createObjectTC(config),
...subSchema,
...(subSchema.properties ? { properties: { ...subSchema.properties } } : {}),
...(subSchema.allOf ? { allOf: [...subSchema.allOf] } : {}),
Expand Down Expand Up @@ -615,6 +624,9 @@ export function getComposerFromJSONSchema(schema: JSONSchema, logger: Logger): P
}
}
} else {
if (outputTypeComposer instanceof InterfaceTypeComposer) {
(subSchemaAndTypeComposers.output as ObjectTypeComposer).addInterface(outputTypeComposer);
}
const typeElemFieldMap = outputTypeComposer.getFields();
for (const fieldName in typeElemFieldMap) {
const field = typeElemFieldMap[fieldName];
Expand Down
30 changes: 30 additions & 0 deletions packages/loaders/openapi/tests/__snapshots__/pet.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Pet should generate the correct schema 1`] = `
"schema {
query: Query
}

type Query {
pets_by_id(id: String!): Pet
dogs_by_id(id: String!): Dog
cats_by_id(id: String!): Cat
}

interface Pet {
name: String!
petType: String
}

type Dog implements Pet {
name: String!
petType: String
dog_exclusive: String
}

type Cat implements Pet {
name: String!
petType: String
cat_exclusive: String
}"
`;
93 changes: 93 additions & 0 deletions packages/loaders/openapi/tests/fixtures/pet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
openapi: 3.0.0
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
paths:
/pets/{id}:
get:
parameters:
- name: id
required: true
in: path
schema:
type: string
responses:
200:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
/dogs/{id}:
get:
parameters:
- name: id
required: true
in: path
schema:
type: string
responses:
200:
content:
application/json:
schema:
$ref: '#/components/schemas/Dog'
/cats/{id}:
get:
parameters:
- name: id
required: true
in: path
schema:
type: string
responses:
200:
content:
application/json:
schema:
$ref: '#/components/schemas/Cat'

components:
schemas:
Pet:
required:
- name
type: object
discriminator:
propertyName: petType
mapping:
Dog: '#/components/schemas/Dog'
Cat: '#/components/schemas/Cat'
properties:
name:
type: string
petType:
type: string
Cat:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
cat_exclusive:
type: string
Dog:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
dog_exclusive:
type: string
requestBodies:
Pet:
required: true
content:
application/vnd.api+json:
schema:
type: object
properties:
data:
type: object
properties:
attributes:
$ref: '#/components/schemas/Pet'
12 changes: 12 additions & 0 deletions packages/loaders/openapi/tests/pet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { printSchemaWithDirectives } from '@graphql-tools/utils';
import loadGraphQLSchemaFromOpenAPI from '../src';

describe('Pet', () => {
it('should generate the correct schema', async () => {
const schema = await loadGraphQLSchemaFromOpenAPI('toto', {
source: './fixtures/pet.yml',
cwd: __dirname,
});
expect(printSchemaWithDirectives(schema)).toMatchSnapshot();
});
});