@@ -613,7 +613,7 @@ object OpenAPI31JSONFactory extends MdcLoggable {
613613 description = Some (" Request body" ),
614614 content = Map (
615615 " application/json" -> MediaTypeJson (
616- schema = Some (inferSchemaFromExample (doc.typed_request_body)),
616+ schema = Some (convertJValueSchemaToSchemaJson (doc.typed_request_body)),
617617 example = Some (doc.typed_request_body)
618618 )
619619 ),
@@ -627,7 +627,7 @@ object OpenAPI31JSONFactory extends MdcLoggable {
627627 content = if (doc.typed_success_response_body != JNothing ) {
628628 Some (Map (
629629 " application/json" -> MediaTypeJson (
630- schema = Some (inferSchemaFromExample (doc.typed_success_response_body)),
630+ schema = Some (convertJValueSchemaToSchemaJson (doc.typed_success_response_body)),
631631 example = Some (doc.typed_success_response_body)
632632 )
633633 ))
@@ -677,41 +677,48 @@ object OpenAPI31JSONFactory extends MdcLoggable {
677677 }
678678
679679 /**
680- * Infers a JSON Schema from an example JSON value
680+ * Converts a JValue that is already a JSON Schema into a SchemaJson case class.
681+ *
682+ * The typed_request_body and typed_success_response_body fields from ResourceDocJson
683+ * are already JSON Schemas produced by JSONFactory1_4_0.translateEntity(),
684+ * so we convert them directly rather than inferring a schema from example data.
681685 */
682- private def inferSchemaFromExample ( example : JValue ): SchemaJson = {
683- example match {
686+ private def convertJValueSchemaToSchemaJson ( schema : JValue ): SchemaJson = {
687+ schema match {
684688 case JObject (fields) =>
685- val properties = fields.map { case JField (name, value) =>
686- name -> inferSchemaFromExample(value)
687- }.toMap
688-
689- val required = fields.collect {
690- case JField (name, value) if value != JNothing && value != JNull => name
689+ val fieldMap = fields.map(f => f.name -> f.value).toMap
690+
691+ val schemaType = fieldMap.get(" type" ).collect { case JString (t) => t }
692+ val format = fieldMap.get(" format" ).collect { case JString (f) => f }
693+
694+ val enum = fieldMap.get(" enum" ).collect {
695+ case JArray (values) => values
691696 }
692697
693- SchemaJson (
694- `type` = Some (" object" ),
695- properties = Some (properties),
696- required = if (required.nonEmpty) Some (required) else None
697- )
698+ val properties = fieldMap.get(" properties" ).collect {
699+ case JObject (props) =>
700+ props.map { case JField (name, value) =>
701+ name -> convertJValueSchemaToSchemaJson(value)
702+ }.toMap
703+ }
704+
705+ val items = fieldMap.get(" items" ).map(convertJValueSchemaToSchemaJson)
706+
707+ val required = fieldMap.get(" required" ).collect {
708+ case JArray (values) => values.collect { case JString (v) => v }
709+ }
698710
699- case JArray (values) =>
700- val itemSchema = values.headOption.map(inferSchemaFromExample)
701- .getOrElse(SchemaJson (`type` = Some (" object" )))
702-
703711 SchemaJson (
704- `type` = Some (" array" ),
705- items = Some (itemSchema)
712+ `type` = schemaType,
713+ format = format,
714+ properties = properties,
715+ items = items,
716+ required = required,
717+ enum = enum
706718 )
707719
708- case JString (_) => SchemaJson (`type` = Some (" string" ))
709- case JInt (_) => SchemaJson (`type` = Some (" integer" ))
710- case JDouble (_) => SchemaJson (`type` = Some (" number" ))
711- case JBool (_) => SchemaJson (`type` = Some (" boolean" ))
712- case JNull => SchemaJson (`type` = Some (" null" ))
713- case JNothing => SchemaJson (`type` = Some (" object" ))
714- case _ => SchemaJson (`type` = Some (" object" ))
720+ case _ =>
721+ SchemaJson (`type` = Some (" object" ))
715722 }
716723 }
717724
0 commit comments