Skip to content

Commit

Permalink
automatic propagation of schema name as schema title
Browse files Browse the repository at this point in the history
  • Loading branch information
ghik committed Mar 12, 2024
1 parent bd97fe2 commit 960291b
Show file tree
Hide file tree
Showing 89 changed files with 181 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SchemasForEndpoints(
val keysToIds: Map[SchemaKey, SchemaId] = calculateUniqueIds(keyedCombinedSchemas.map(_._1), (key: SchemaKey) => schemaName(key.name))

val toSchemaReference = new ToSchemaReference(keysToIds, keyedCombinedSchemas.toMap)
val tschemaToASchema = new TSchemaToASchema(toSchemaReference, markOptionsAsNullable)
val tschemaToASchema = new TSchemaToASchema(schemaName, toSchemaReference, markOptionsAsNullable)

val keysToSchemas: ListMap[SchemaKey, ASchema] =
keyedCombinedSchemas.map(td => (td._1, tschemaToASchema(td._2, allowReference = false))).toListMap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sttp.tapir.docs.apispec.schema

import sttp.apispec.{Schema => ASchema, _}
import sttp.tapir.Schema.Title
import sttp.tapir.Schema.{SName, Title}
import sttp.tapir.Validator.EncodeToRaw
import sttp.tapir.docs.apispec.DocsExtensionAttribute.RichSchema
import sttp.tapir.docs.apispec.schema.TSchemaToASchema.{tDefaultToADefault, tExampleToAExample}
Expand All @@ -10,7 +10,11 @@ import sttp.tapir.internal._
import sttp.tapir.{Codec, Validator, Schema => TSchema, SchemaType => TSchemaType}

/** Converts a tapir schema to an OpenAPI/AsyncAPI schema, using `toSchemaReference` to resolve references. */
private[docs] class TSchemaToASchema(toSchemaReference: ToSchemaReference, markOptionsAsNullable: Boolean) {
private[docs] class TSchemaToASchema(
fallbackSchemaTitle: SName => String,
toSchemaReference: ToSchemaReference,
markOptionsAsNullable: Boolean
) {

def apply[T](codec: Codec[T, _, _]): ASchema = apply(codec.schema, allowReference = true)

Expand Down Expand Up @@ -93,8 +97,11 @@ private[docs] class TSchemaToASchema(toSchemaReference: ToSchemaReference, markO
.toListMap
}

private def addTitle(oschema: ASchema, tschema: TSchema[_]): ASchema =
oschema.copy(title = tschema.attributes.get(Title.Attribute).map(_.value))
private def addTitle(oschema: ASchema, tschema: TSchema[_]): ASchema = {
val fromAttr = tschema.attributes.get(Title.Attribute).map(_.value)
def fallback = tschema.name.map(fallbackSchemaTitle)
oschema.copy(title = fromAttr orElse fallback)
}

private def addMetadata(oschema: ASchema, tschema: TSchema[_]): ASchema = {
oschema.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object TapirSchemaToJsonSchema {

val keysToIds = calculateUniqueIds(keyedSchemas.map(_._1), (key: SchemaKey) => schemaName(key.name))
val toSchemaReference = new ToSchemaReference(keysToIds, keyedSchemas.toMap, refRoot = "#/$defs/")
val tschemaToASchema = new TSchemaToASchema(toSchemaReference, markOptionsAsNullable)
val tschemaToASchema = new TSchemaToASchema(schemaName, toSchemaReference, markOptionsAsNullable)
val keysToSchemas = keyedSchemas.map(td => (td._1, tschemaToASchema(td._2, allowReference = false))).toListMap
val schemaIds = keysToSchemas.map { case (k, v) => k -> ((keysToIds(k), v)) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ paths:
components:
schemas:
Entity:
title: Entity
oneOf:
- $ref: '#/components/schemas/Organization'
- $ref: '#/components/schemas/Person'
Organization:
title: Organization
required:
- name
type: object
properties:
name:
type: string
Person:
title: Person
required:
- name
- age
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ paths:
components:
schemas:
Entity:
title: Entity
oneOf:
- $ref: '#/components/schemas/Organization'
- $ref: '#/components/schemas/Person'
Expand All @@ -25,13 +26,15 @@ components:
john: '#/components/schemas/Person'
sml: '#/components/schemas/Organization'
Organization:
title: Organization
required:
- name
type: object
properties:
name:
type: string
Person:
title: Person
required:
- name
- age
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ paths:
components:
schemas:
Entity:
title: Entity
oneOf:
- $ref: '#/components/schemas/Organization'
- $ref: '#/components/schemas/Person'
Expand All @@ -25,20 +26,23 @@ components:
john: '#/components/schemas/Person'
sml: '#/components/schemas/Organization'
NestedEntity:
title: NestedEntity
required:
- entity
type: object
properties:
entity:
$ref: '#/components/schemas/Entity'
Organization:
title: Organization
required:
- name
type: object
properties:
name:
type: string
Person:
title: Person
required:
- name
- age
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ paths:
components:
schemas:
Shape:
title: Shape
oneOf:
- $ref: '#/components/schemas/Square'
discriminator:
propertyName: shapeType
mapping:
Square: '#/components/schemas/Square'
Square:
title: Square
required:
- color
- shapeType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ paths:
components:
schemas:
Entity:
title: Entity
oneOf:
- $ref: '#/components/schemas/Organization'
- $ref: '#/components/schemas/Person1'
Expand All @@ -63,6 +64,7 @@ components:
organization: '#/components/schemas/Organization'
person: '#/components/schemas/Person1'
Organization:
title: Organization
required:
- name
- kind
Expand All @@ -73,6 +75,7 @@ components:
kind:
type: string
Person:
title: Person
required:
- name
- age
Expand All @@ -84,6 +87,7 @@ components:
type: integer
format: int32
Person1:
title: Person
required:
- name
- age
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,28 @@ paths:
components:
schemas:
Entity:
title: Entity
oneOf:
- $ref: '#/components/schemas/Organization'
- $ref: '#/components/schemas/Person'
NestedEntity:
title: NestedEntity
required:
- entity
type: object
properties:
entity:
$ref: '#/components/schemas/Entity'
Organization:
title: Organization
required:
- name
type: object
properties:
name:
type: string
Person:
title: Person
required:
- name
- age
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ paths:
components:
schemas:
Entity:
title: Entity
oneOf:
- required:
- Organization
Expand All @@ -44,13 +45,15 @@ components:
Person:
$ref: '#/components/schemas/Person'
Organization:
title: Organization
required:
- name
type: object
properties:
name:
type: string
Person:
title: Person
required:
- name
- age
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ paths:
components:
schemas:
GenericEntity_Int:
title: GenericEntity_Int
oneOf:
- $ref: '#/components/schemas/GenericPerson_Int'
GenericEntity_String:
title: GenericEntity_String
oneOf:
- $ref: '#/components/schemas/GenericPerson_String'
GenericPerson_Int:
title: GenericPerson_Int
required:
- data
type: object
Expand All @@ -56,6 +59,7 @@ components:
type: integer
format: int32
GenericPerson_String:
title: GenericPerson_String
required:
- data
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ paths:
components:
schemas:
Clause:
title: Clause
oneOf:
- $ref: '#/components/schemas/Expression'
- $ref: '#/components/schemas/Not'
Expression:
title: Expression
required:
- v
type: object
properties:
v:
type: string
Not:
title: Not
required:
- not
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ paths:
components:
schemas:
Entity:
title: Entity
oneOf:
- $ref: '#/components/schemas/Organization'
- $ref: '#/components/schemas/Person'
Organization:
title: Organization
required:
- name
type: object
properties:
name:
type: string
Person:
title: Person
required:
- name
- age
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ paths:
components:
schemas:
Fail:
title: Fail
required:
- msg
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ paths:
components:
schemas:
CornerStyle:
title: CornerStyle
type: string
enum:
- rounded
- straight
Square:
title: Square
type: object
properties:
cornerStyle:
Expand All @@ -30,6 +32,7 @@ components:
items:
$ref: '#/components/schemas/Tag'
Tag:
title: Tag
type: string
enum:
- tag1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ paths:
components:
schemas:
CornerStyle:
title: CornerStyle
type: string
enum:
- rounded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ paths:
components:
schemas:
CornerStyle:
title: CornerStyle
type: string
enum:
- rounded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ paths:
components:
schemas:
FruitQueryWithEncoded:
title: FruitQueryWithEncoded
type: object
properties:
fruitType:
$ref: '#/components/schemas/FruitType'
default: PEAR
FruitType:
title: FruitType
type: string
enum:
- APPLE
- PEAR
FruitWithEnum:
title: FruitWithEnum
required:
- fruit
- amount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ paths:
components:
schemas:
CountryCode:
title: CountryCode
type: string
enum:
- PL
- BE
- LU
CountryCollection:
title: CountryCollection
required:
- countryCode
type: object
Expand Down
Loading

0 comments on commit 960291b

Please sign in to comment.