Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.
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
Expand Up @@ -102,11 +102,15 @@ class DataTypeConverter(
return filtered.first()
}

val constraints = DataTypeConstraints(
required = items.flatMap { it.constraints?.required ?: emptyList() }
)

objectType = AllOfObjectDataType(
DataTypeName(schemaInfo.getName(), getTypeNameWithSuffix(schemaInfo.getName())),
listOf(options.packageName, "model").joinToString ("."),
items,
null,
constraints,
schemaInfo.getDeprecated()
)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright © 2020 https://github.com/openapi-processor/openapi-processor-core
* PDX-License-Identifier: Apache-2.0
*/

package io.openapiprocessor.core.converter

import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.collections.shouldContainAll
import io.kotest.matchers.types.shouldBeInstanceOf
import io.openapiprocessor.core.model.DataTypes
import io.openapiprocessor.core.model.HttpMethod
import io.openapiprocessor.core.model.datatypes.AllOfObjectDataType
import io.openapiprocessor.core.support.getBodySchemaInfo
import io.openapiprocessor.core.support.parse

class DataTypeConverterAllOfRequiredSpec: StringSpec({

val dataTypes = DataTypes()

"applies required to allOf composition" {
val openApi = parse("""
openapi: 3.0.2
info:
title: API
version: 1.0.0

paths:
/foo:
patch:
requestBody:
content:
application/json:
schema:
${'$'}ref: '#/components/schemas/Foo'
responses:
'204':
description: empty

components:
schemas:

Foo:
description: a Foo
type: object
allOf:
- type: object
required: [ foo, bar ]
properties:
foo:
type: string
bar:
type: string
- type: object
required: [ baz, qux ]
properties:
baz:
type: string
qux:
type: string

""".trimIndent())


val options = ApiOptions()

val schemaInfo = openApi.getBodySchemaInfo("Foo",
"/foo", HttpMethod.PATCH, "application/json")

// when:
val converter = DataTypeConverter(options)
val datatype = converter.convert(schemaInfo, dataTypes)

// then:
datatype.shouldBeInstanceOf<AllOfObjectDataType>()
datatype.constraints!!.required shouldContainAll listOf("foo", "bar", "baz", "qux")
}

})
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TestSets {
static def ALL = [
'bean-validation',
'bean-validation-iterable',
'bean-validation-allOf-required',
'deprecated',
'endpoint-exclude',
'endpoint-http-mapping', // framework specific
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
items:
- generated/api/Api.java
- generated/model/QueryGetResponse200.java
# - generated/model/QueryResponse200_AllOf_0.java
# - generated/model/QueryResponse200_AllOf_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* This class is auto generated by https://github.com/hauner/openapi-processor-core.
* TEST ONLY.
*/

package generated.api;

import annotation.Mapping;
import generated.model.QueryGetResponse200;

public interface Api {

@Mapping("/query")
QueryGetResponse200 getQuery();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This class is auto generated by https://github.com/hauner/openapi-processor-core.
* TEST ONLY.
*/

package generated.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import javax.validation.constraints.NotNull;

public class QueryGetResponse200 {

@NotNull
@JsonProperty("prop1")
private String prop1;

@JsonProperty("prop2")
private String prop2;

public String getProp1() {
return prop1;
}

public void setProp1(String prop1) {
this.prop1 = prop1;
}

public String getProp2() {
return prop2;
}

public void setProp2(String prop2) {
this.prop2 = prop2;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* This class is auto generated by https://github.com/hauner/openapi-processor-core.
* TEST ONLY.
*/

package generated.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import javax.validation.constraints.NotNull;

public class QueryGetResponse200_AllOf_0 {

@NotNull
@JsonProperty("prop1")
private String prop1;

public String getProp1() {
return prop1;
}

public void setProp1(String prop1) {
this.prop1 = prop1;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* This class is auto generated by https://github.com/hauner/openapi-processor-core.
* TEST ONLY.
*/

package generated.model;

import com.fasterxml.jackson.annotation.JsonProperty;

public class QueryGetResponse200_AllOf_1 {

@JsonProperty("prop2")
private String prop2;

public String getProp2() {
return prop2;
}

public void setProp2(String prop2) {
this.prop2 = prop2;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
items:
- inputs/openapi.yaml
- inputs/mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
openapi-processor-spring: v2

options:
package-name: generated
bean-validation: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
openapi: 3.0.2
info:
title: merge allOf into same object
version: 1.0.0

paths:
/query:
get:
responses:
'200':
description: create result from allOff object
content:
application/json:
schema:
allOf:
- type: object
properties:
prop1:
type: string
required:
- prop1
- type: object
properties:
prop2:
type: string