Skip to content

Commit

Permalink
Introduced a HeadersParent interface to the data model
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Jan 10, 2025
1 parent 89200f3 commit 741a296
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 140 deletions.
10 changes: 7 additions & 3 deletions src/main/java/io/apicurio/datamodels/cmd/CommandFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io.apicurio.datamodels.cmd.commands.DeleteExtensionCommand;
import io.apicurio.datamodels.cmd.commands.DeleteLicenseCommand;
import io.apicurio.datamodels.cmd.commands.DeleteMediaTypeCommand;
import io.apicurio.datamodels.models.Document;
import io.apicurio.datamodels.models.Extensible;
import io.apicurio.datamodels.models.Info;
import io.apicurio.datamodels.models.Node;
Expand All @@ -38,8 +37,10 @@
import io.apicurio.datamodels.models.SecurityRequirementsParent;
import io.apicurio.datamodels.models.asyncapi.AsyncApiServer;
import io.apicurio.datamodels.models.openapi.OpenApiDocument;
import io.apicurio.datamodels.models.openapi.OpenApiEncoding;
import io.apicurio.datamodels.models.openapi.OpenApiExamplesParent;
import io.apicurio.datamodels.models.openapi.OpenApiHeader;
import io.apicurio.datamodels.models.openapi.OpenApiHeadersParent;
import io.apicurio.datamodels.models.openapi.OpenApiMediaType;
import io.apicurio.datamodels.models.openapi.OpenApiOperation;
import io.apicurio.datamodels.models.openapi.OpenApiParameter;
Expand Down Expand Up @@ -157,8 +158,11 @@ public static ICommand createDeleteAllHeaderExamplesCommand(OpenApiHeader header
return new DeleteAllExamplesCommand((OpenApiExamplesParent) header);
}

public static ICommand createDeleteAllHeadersCommand(OpenApiResponse header) {
return new DeleteAllHeadersCommand(header);
public static ICommand createDeleteAllResponseHeadersCommand(OpenApiResponse header) {
return new DeleteAllHeadersCommand((OpenApiHeadersParent) header);
}
public static ICommand createDeleteAllEncodingHeadersCommand(OpenApiEncoding header) {
return new DeleteAllHeadersCommand((OpenApiHeadersParent) header);
}

public static ICommand createDeleteAllPathItemOperationsCommand(OpenApiPathItem pathItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@
import io.apicurio.datamodels.cmd.AbstractCommand;
import io.apicurio.datamodels.models.Document;
import io.apicurio.datamodels.models.Node;
import io.apicurio.datamodels.models.openapi.OpenApiResponse;
import io.apicurio.datamodels.models.openapi.v20.OpenApi20Header;
import io.apicurio.datamodels.models.openapi.v20.OpenApi20Headers;
import io.apicurio.datamodels.models.openapi.v20.OpenApi20Response;
import io.apicurio.datamodels.models.openapi.v30.OpenApi30Header;
import io.apicurio.datamodels.models.openapi.v30.OpenApi30Response;
import io.apicurio.datamodels.models.openapi.v31.OpenApi31Header;
import io.apicurio.datamodels.models.openapi.v31.OpenApi31Response;
import io.apicurio.datamodels.models.openapi.OpenApiHeader;
import io.apicurio.datamodels.models.openapi.OpenApiHeadersParent;
import io.apicurio.datamodels.paths.NodePath;
import io.apicurio.datamodels.paths.NodePathUtil;
import io.apicurio.datamodels.util.LoggerUtil;
import io.apicurio.datamodels.util.ModelTypeUtil;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -35,8 +27,8 @@ public class DeleteAllHeadersCommand extends AbstractCommand {
public DeleteAllHeadersCommand() {
}

public DeleteAllHeadersCommand(OpenApiResponse parent) {
this._parentPath = NodePathUtil.createNodePath(parent);
public DeleteAllHeadersCommand(OpenApiHeadersParent parent) {
this._parentPath = NodePathUtil.createNodePath((Node) parent);
}

/**
Expand All @@ -46,12 +38,12 @@ public DeleteAllHeadersCommand(OpenApiResponse parent) {
public void execute(Document document) {
LoggerUtil.info("[DeleteAllHeadersCommand] Executing.");

Node parent = NodePathUtil.resolveNodePath(this._parentPath, document);
OpenApiHeadersParent parent = (OpenApiHeadersParent) NodePathUtil.resolveNodePath(this._parentPath, document);
if (this.isNullOrUndefined(parent)) {
return;
}

Map<String, ? extends Node> headers = getHeaders(parent);
Map<String, ? extends Node> headers = parent.getHeaders();
if (this.isNullOrUndefined(headers) || headers.size() == 0) {
return;
}
Expand All @@ -65,7 +57,7 @@ public void execute(Document document) {
this._oldHeaders.put(name, Library.writeNode(header));
});

clearHeaders(parent);
parent.clearHeaders();
}

/**
Expand All @@ -79,101 +71,17 @@ public void undo(Document document) {
return;
}

Node parent = NodePathUtil.resolveNodePath(this._parentPath, document);
OpenApiHeadersParent parent = (OpenApiHeadersParent) NodePathUtil.resolveNodePath(this._parentPath, document);
if (this.isNullOrUndefined(parent)) {
return;
}

for (String k : this._oldHeaders.keySet()) {
Node header = createHeader(parent, k);
OpenApiHeader header = parent.createHeader();
Library.readNode(this._oldHeaders.get(k), header);
addHeader(parent, k, header);
parent.addHeader(k, header);
}

}

private Map<String, ? extends Node> getHeaders(Node parent) {
if (ModelTypeUtil.isOpenApi2Model(parent)) {
OpenApi20Response response = (OpenApi20Response) parent;
OpenApi20Headers headers = response.getHeaders();
if (headers == null) {
return null;
}
Map<String, Node> rval = new LinkedHashMap<>();
List<String> headerNames = headers.getItemNames();
for (String headerName : headerNames) {
OpenApi20Header header = headers.getItem(headerName);
rval.put(headerName, header);
}
return rval;
}
if (ModelTypeUtil.isOpenApi30Model(parent)) {
OpenApi30Response response = (OpenApi30Response) parent;
return response.getHeaders();
}
if (ModelTypeUtil.isOpenApi31Model(parent)) {
OpenApi31Response response = (OpenApi31Response) parent;
return response.getHeaders();
}
return null;
}

private void clearHeaders(Node parent) {
if (ModelTypeUtil.isOpenApi2Model(parent)) {
OpenApi20Response response = (OpenApi20Response) parent;
OpenApi20Headers headers = response.getHeaders();
if (headers == null) {
return;
}
headers.clearItems();
}
if (ModelTypeUtil.isOpenApi30Model(parent)) {
OpenApi30Response response = (OpenApi30Response) parent;
response.clearHeaders();
}
if (ModelTypeUtil.isOpenApi31Model(parent)) {
OpenApi31Response response = (OpenApi31Response) parent;
response.clearHeaders();
}
}

private Node createHeader(Node parent, String k) {
if (ModelTypeUtil.isOpenApi2Model(parent)) {
OpenApi20Response response = (OpenApi20Response) parent;
OpenApi20Headers headers = response.getHeaders();
if (headers == null) {
return null;
}
return headers.createHeader();
}
if (ModelTypeUtil.isOpenApi30Model(parent)) {
OpenApi30Response response = (OpenApi30Response) parent;
return response.createHeader();
}
if (ModelTypeUtil.isOpenApi31Model(parent)) {
OpenApi31Response response = (OpenApi31Response) parent;
return response.createHeader();
}
return null;
}

private void addHeader(Node parent, String name, Node header) {
if (ModelTypeUtil.isOpenApi2Model(parent)) {
OpenApi20Response response = (OpenApi20Response) parent;
OpenApi20Headers headers = response.getHeaders();
if (headers == null) {
return;
}
headers.addItem(name, (OpenApi20Header) header);
}
if (ModelTypeUtil.isOpenApi30Model(parent)) {
OpenApi30Response response = (OpenApi30Response) parent;
response.addHeader(name, (OpenApi30Header) header);
}
if (ModelTypeUtil.isOpenApi31Model(parent)) {
OpenApi31Response response = (OpenApi31Response) parent;
response.addHeader(name, (OpenApi31Header) header);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* Copyright 2019 Red Hat
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.apicurio.datamodels.transform;

import java.util.ArrayList;
Expand Down Expand Up @@ -60,7 +44,6 @@
import io.apicurio.datamodels.models.openapi.v20.OpenApi20Document;
import io.apicurio.datamodels.models.openapi.v20.OpenApi20ExternalDocumentation;
import io.apicurio.datamodels.models.openapi.v20.OpenApi20Header;
import io.apicurio.datamodels.models.openapi.v20.OpenApi20Headers;
import io.apicurio.datamodels.models.openapi.v20.OpenApi20Info;
import io.apicurio.datamodels.models.openapi.v20.OpenApi20Items;
import io.apicurio.datamodels.models.openapi.v20.OpenApi20License;
Expand Down Expand Up @@ -621,15 +604,6 @@ public void visitSchema(Schema node) {
}
}

@Override
public void visitHeaders(OpenApi20Headers node) {
OpenApi30Response parent30 = (OpenApi30Response) this.lookup(node.parent());
// No processing to do here, because 3.0 doesn't have a "headers" node. So instead
// we'll map the headers node to the 3.0 response node, because that will be the
// effective parent for any 3.0 Header nodes.
this.mapNode(node, parent30);
}

@Override
public void visitHeader(OpenApiHeader node) {
OpenApi20Header header20 = (OpenApi20Header) node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.apicurio.datamodels.validation.rules.invalid.value;

import io.apicurio.datamodels.models.openapi.OpenApiEncoding;
import io.apicurio.datamodels.models.openapi.OpenApiHeadersParent;
import io.apicurio.datamodels.models.openapi.OpenApiMediaType;
import io.apicurio.datamodels.validation.ValidationRuleMetaData;

Expand Down Expand Up @@ -49,7 +50,8 @@ public void visitMediaType(OpenApiMediaType node) {
*/
@Override
public void visitEncoding(OpenApiEncoding node) {
if (isDefined(node.getHeaders()) && node.getHeaders().size() > 0) {
OpenApiHeadersParent headersParent = (OpenApiHeadersParent) node;
if (isDefined(headersParent.getHeaders()) && !headersParent.getHeaders().isEmpty()) {
this.reportIfInvalid(mediaTypeName.indexOf("multipart") == 0, node, "headers",
map("name", mediaTypeName));
}
Expand Down
14 changes: 6 additions & 8 deletions src/main/resources/specs/openapi/openapi-2.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ traits:
properties:
- name: security
type: '[SecurityRequirement]'
- name: HeadersParent
properties:
- name: headers
type: '{Header}'
- name: SchemaLike
transparent: true
properties:
Expand Down Expand Up @@ -288,27 +292,21 @@ entities:
traits:
- Extensible
- Referenceable
- HeadersParent
properties:
- name: description
type: string
- name: schema
type: Schema
- name: headers
type: Headers
type: '{Header}'
- name: examples
type: Example
propertyOrder:
- $Referenceable
- $this
- $Extensible

- name: Headers
properties:
- name: '*'
type: Header
propertyOrder:
- $this

- name: Example
properties:
- name: '*'
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/specs/openapi/openapi-3.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ traits:
properties:
- name: security
type: '[SecurityRequirement]'
- name: HeadersParent
properties:
- name: headers
type: '{Header}'


entities:
Expand Down Expand Up @@ -334,6 +338,7 @@ entities:
- name: Encoding
traits:
- Extensible
- HeadersParent
properties:
- name: contentType
type: string
Expand Down Expand Up @@ -367,6 +372,7 @@ entities:
traits:
- Extensible
- Referenceable
- HeadersParent
properties:
- name: description
type: string
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/specs/openapi/openapi-3.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ traits:
properties:
- name: security
type: '[SecurityRequirement]'
- name: HeadersParent
properties:
- name: headers
type: '{Header}'

entities:
- name: Document
Expand Down Expand Up @@ -341,6 +345,7 @@ entities:
- name: Encoding
traits:
- Extensible
- HeadersParent
properties:
- name: contentType
type: string
Expand Down Expand Up @@ -374,6 +379,7 @@ entities:
traits:
- Extensible
- Referenceable
- HeadersParent
properties:
- name: description
type: string
Expand Down

0 comments on commit 741a296

Please sign in to comment.